Personal Git forge.

Code git clone ssh://git.poy.ooo:6969/~poyo/farga ZIP
app/gists.go / 3KiB #e7da219f
Raw
package appimport (	md "farga/app/middleware"	"farga/db/types"	"farga/lib"	"farga/templating"	"fmt"	"net/http"	"github.com/google/uuid"	"github.com/gorilla/mux")type GistEditForm struct {	Title   string `form:"title"   validate:"min=1,max=100"`	Content string `form:"content"    validate:"required,max=30000"`}func (a *App) NewGist(w http.ResponseWriter, r *http.Request) {	user, ok := r.Context().Value(lib.UserKey).(*types.User)	if !ok {		http.Error(w, "Only authenticated users can write gists.", http.StatusForbidden)		return	}	switch r.Method {	case http.MethodGet:		templating.Render(w, r, "base", "gists/write")	case http.MethodPost:		form, ok := md.Form[GistEditForm](r) // r being the request		if !ok {			http.Error(w, "Invalid form.", http.StatusBadRequest)			return		}		gist, err := a.Store.NewGist(user.ID, form.Title, form.Content)		if err != nil {			http.Error(w, "Internal Server Error", http.StatusInternalServerError)			return		}		http.Redirect(w, r, "/gist/"+gist.ID.String(), http.StatusSeeOther)	default:		http.NotFound(w, r)	}}func (a *App) GetGist(w http.ResponseWriter, r *http.Request) {	vars := mux.Vars(r)	id, err := uuid.Parse(vars["id"])	if err != nil {		http.Error(w, "Malformed UUID.", http.StatusBadRequest) // it's malformed		return	}	gist, err := a.Store.GetGist(id)	if err != nil {		http.NotFound(w, r) // it doesn't exist		return	}	templating.Render(w, r, "base", "gists/view", map[string]any{		"Gist":         gist,		"CreatedAtUTC": gist.CreatedAt.String(),	})}func (a *App) DeleteGist(w http.ResponseWriter, r *http.Request) {	vars := mux.Vars(r)	id, err := uuid.Parse(vars["id"])	user, ok := r.Context().Value(lib.UserKey).(*types.User)	if err != nil || !ok {		http.Error(w, "Malformed UUID.", http.StatusBadRequest) // it's malformed		return	}	gist, err := a.Store.GetGist(id)	if err != nil {		http.NotFound(w, r) // it doesn't exist		return	}	if gist.UserID != user.ID {		http.Error(w, "Forbidden. You cannot delete someone else's gists.", http.StatusForbidden)		return	}	err = a.Store.DeleteGist(id)	if err != nil {		http.Error(w, "500 Internal Server Error.", http.StatusInternalServerError) // whoops.		return	}	http.Redirect(w, r, "/?msg=GeneralSuccess", http.StatusSeeOther)}func (a *App) EditGist(w http.ResponseWriter, r *http.Request) {	user, ok := r.Context().Value(lib.UserKey).(*types.User)	if !ok {		http.Error(w, "Only authenticated users can edit gists.", http.StatusForbidden)		return	}	vars := mux.Vars(r)	id, err := uuid.Parse(vars["id"])	if err != nil {		http.Error(w, "Malformed UUID.", http.StatusBadRequest) // it's malformed		return	}	gist, err := a.Store.GetGist(id)	if err != nil {		http.NotFound(w, r) // it doesn't exist		return	}	if gist.UserID != user.ID {		http.Error(w, "Forbidden. You cannot edit someone else's gists.", http.StatusForbidden)		return	}	switch r.Method {	case http.MethodGet:		templating.Render(w, r, "base", "gists/write", map[string]any{"Gist": gist}) // edit it	case http.MethodPost:		form, ok := md.Form[GistEditForm](r) // r being the request		if !ok {			http.Error(w, "Invalid form.", http.StatusBadRequest)			return		}		gist, err := a.Store.UpdateGist(gist.ID, form.Title, form.Content)		if err != nil {			fmt.Println(err)			http.Error(w, "Internal Server Error", http.StatusInternalServerError)			return		}		http.Redirect(w, r, "/gist/"+gist.ID.String(), http.StatusSeeOther)	default:		http.NotFound(w, r)	}}