Something went wrong. Try again.
Lightweight and easily self-hostable Git forge.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145package app
import ( 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) }}