Lightweight and easily self-hostable Git forge. git.poy.ooo
git-forge selfhosting homebrew
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106package app
import ( md "farga/app/middleware" "farga/lib" "net/http" "strings"
"github.com/gorilla/csrf" "github.com/gorilla/mux")
type App struct { Store Store Handler http.Handler}
func New(store Store) *App { fs := http.FileServer(http.Dir("./static"))
r := mux.NewRouter() r.StrictSlash(true) // treat "hello" and "hello/" the same
a := App{ Store: store, Handler: r, }
csrfMiddleware := csrf.Protect( []byte(lib.CSRFKey), csrf.Secure(false), // todo: production flag csrf.SameSite(csrf.SameSiteLaxMode), // send cookies on top-level safe navigations, block on cross-site subrequests csrf.TrustedOrigins([]string{"localhost:3333", lib.DOMAIN}), )
r.Use(csrfMiddleware) r.Use(md.GeneralAuth(a.Store)) r.Use(md.Messages) // normal website
r.HandleFunc("/", a.Index)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static", fs)) r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK")) // lol funny health check })
r.Handle("/new", md.RequireAuth(md.ValidateForm[CreateRepoForm](a.NewRepo)))
// auth routes auth := r.PathPrefix("/auth").Subrouter()
auth.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/auth/login", http.StatusSeeOther) })
auth.Handle("/login", md.ExcludeAuth(md.ValidateForm[LoginForm](a.login))) auth.Handle("/register", md.ExcludeAuth(md.ValidateForm[RegisterForm](a.register))) auth.Handle("/logout", md.RequireAuth(http.HandlerFunc(a.logout)))
auth.Handle("/hackatime", md.RequireAuth(http.HandlerFunc(a.HackatimeLogin))) auth.Handle("/hackatime/callback", md.RequireAuth(http.HandlerFunc(a.HackatimeCallback)))
// config config := r.PathPrefix("/config").Subrouter() config.Use(md.RequireAuth) config.HandleFunc("/", a.UserConfigHandler) config.Handle("/default_branch", md.ValidateForm[BranchForm](a.branchHandler)).Methods("POST") config.Handle("/add_ssh_key", md.ValidateForm[AddSSHKeyForm](a.newSSHKeyHandler)).Methods("POST") config.Handle("/delete_ssh_key", md.ValidateForm[DeleteSSHKeyForm](a.deleteSSHKeyHandler)).Methods("POST")
// repos repo := r.PathPrefix("/~{user}/{repo}").Subrouter() repo.Use(md.RepoMiddleware(a.Store))
repo.HandleFunc("", func(w http.ResponseWriter, r *http.Request) { if strings.HasSuffix(mux.Vars(r)["repo"], ".git") { http.Error(w, "Repos are not accessible via HTTP and you should clone them using SSH.", http.StatusMethodNotAllowed) return } withMode("tree", a.RepoHandler).ServeHTTP(w, r) })
repo.HandleFunc("/issues", a.RepoIssuesIndex) repo.Handle("/issues/report", md.ValidateForm[repoNewIssueForm](a.repoIssueReport)) repo.Handle("/issues/{issueID}", md.ValidateForm[createIssueCommentForm](a.repoIssueView))
repo.HandleFunc("/tree", withMode("tree", a.RepoHandler)) repo.HandleFunc("/tree/{branch}", withMode("tree", a.RepoHandler)) repo.HandleFunc("/tree/{branch}/{path:.+}", withMode("tree", a.RepoHandler))
repo.HandleFunc("/blob/{branch}/{path:.+}", withMode("blob", a.RepoHandler))
repo.HandleFunc("/raw/{branch}/{path:.+}", withMode("raw", a.RepoHandler))
repo.HandleFunc("/log", withMode("log", a.RepoHandler)) repo.HandleFunc("/log/{branch}", withMode("log", a.RepoHandler))
repoConfig := repo.PathPrefix("/config").Subrouter() repoConfig.Use(md.RequireAuth)
repoConfig.Handle("/", md.ValidateForm[repoConfigForm](a.RepoConfigHandler)).Methods("GET", "POST") // GET: page, POST: form.
return &a}