hibiscus/serve.go

48 lines
1.6 KiB
Go
Raw Normal View History

2024-03-15 18:34:24 +03:00
package main
import (
"fmt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"net/http"
"strconv"
)
func Serve() {
r := chi.NewRouter()
r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes)
r.Use(basicAuth) // TODO: ..duh!
2024-03-17 16:44:05 +03:00
r.NotFound(NotFound)
2024-03-15 18:34:24 +03:00
// Home page
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./pages/index.html")
})
// API =============
apiRouter := chi.NewRouter()
apiRouter.Get("/readme", func(w http.ResponseWriter, r *http.Request) { GetFile("readme", w, r) })
apiRouter.Post("/readme", func(w http.ResponseWriter, r *http.Request) { PostFile("readme", w, r) })
2024-03-15 20:20:33 +03:00
apiRouter.Get("/log", func(w http.ResponseWriter, r *http.Request) { GetFile("log", w, r) })
apiRouter.Post("/log", func(w http.ResponseWriter, r *http.Request) { PostLog(w, r) })
2024-03-15 18:34:24 +03:00
apiRouter.Get("/day", func(w http.ResponseWriter, r *http.Request) { ListFiles("day", w, r) })
apiRouter.Get("/day/{day}", func(w http.ResponseWriter, r *http.Request) { GetDay(w, r) })
apiRouter.Get("/notes", func(w http.ResponseWriter, r *http.Request) { ListFiles("notes", w, r) })
apiRouter.Get("/notes/{note}", func(w http.ResponseWriter, r *http.Request) { GetNote(w, r) })
2024-03-15 20:20:33 +03:00
apiRouter.Get("/today", func(w http.ResponseWriter, r *http.Request) { GetToday(w, r) })
apiRouter.Post("/today", func(w http.ResponseWriter, r *http.Request) { PostToday(w, r) })
2024-03-15 18:34:24 +03:00
r.Mount("/api", apiRouter)
// Static files
fs := http.FileServer(http.Dir("public"))
r.Handle("/public/*", http.StripPrefix("/public/", fs))
2024-03-17 17:12:41 +03:00
fmt.Println("Website working on port: ", Cfg.Port)
_ = http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r)
2024-03-15 18:34:24 +03:00
}