-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
57 lines (47 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/chi/v5"
"github.com/saintmalik/delta/handlers"
)
func main() {
r := chi.NewRouter()
r.Group(func(r chi.Router) {
r.Get("/", handlers.HandleMain)
r.Post("/signup", handlers.HandleSignup)
r.Get("/callback", handlers.HandleGitHubCallback)
})
r.Group(func(r chi.Router) {
r.Use(middleware.Logger)
r.Use(handlers.IsAuthenticated)
r.Get("/package", handlers.AddPackage)
r.Post("/package", handlers.AddPackage)
r.Get("/dash", handlers.ListPackage)
r.Post("/logout", handlers.HandleUserLogout)
r.Get("/check", handlers.CheckReleasesHandler)
})
workDir, _ := os.Getwd()
filesDir := http.Dir(filepath.Join(workDir, "public"))
FileServer(r, "/assets", filesDir)
http.ListenAndServe(":4000", r)
}
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}