2024-03-15 18:34:24 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2024-03-18 19:33:57 +03:00
|
|
|
"log"
|
2024-03-20 16:18:23 +03:00
|
|
|
"log/slog"
|
2024-03-15 18:34:24 +03:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2024-06-17 00:54:55 +03:00
|
|
|
// Serve starts the app's web server.
|
2024-03-15 18:34:24 +03:00
|
|
|
func Serve() {
|
|
|
|
r := chi.NewRouter()
|
2024-06-02 07:59:11 +03:00
|
|
|
r.Use(middleware.RealIP)
|
2024-03-15 18:34:24 +03:00
|
|
|
r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes)
|
2024-03-28 10:21:04 +03:00
|
|
|
r.NotFound(NotFound)
|
2024-03-15 18:34:24 +03:00
|
|
|
|
2024-03-28 10:21:04 +03:00
|
|
|
// Routes ==========
|
2024-05-05 13:06:20 +03:00
|
|
|
userRouter := chi.NewRouter()
|
|
|
|
userRouter.Use(BasicAuth)
|
2024-06-17 00:54:55 +03:00
|
|
|
userRouter.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
GetEntry(w, r, TranslatableText("title.today"), DataFile("day/"+TodayDate()), true)
|
|
|
|
})
|
|
|
|
userRouter.Post("/", func(w http.ResponseWriter, r *http.Request) { PostEntry(DataFile("day/"+TodayDate()), w, r) })
|
2024-05-05 13:06:20 +03:00
|
|
|
userRouter.Get("/day", GetDays)
|
|
|
|
userRouter.Get("/day/{day}", GetDay)
|
|
|
|
userRouter.Get("/notes", GetNotes)
|
|
|
|
userRouter.Get("/notes/{note}", GetNote)
|
|
|
|
userRouter.Post("/notes/{note}", PostNote)
|
2024-05-07 13:27:11 +03:00
|
|
|
userRouter.Get("/info", GetInfo)
|
2024-06-17 00:54:55 +03:00
|
|
|
userRouter.Get("/readme", func(w http.ResponseWriter, r *http.Request) { GetEntry(w, r, "readme.txt", DataFile("readme"), true) })
|
|
|
|
userRouter.Post("/readme", func(w http.ResponseWriter, r *http.Request) { PostEntry(DataFile("readme"), w, r) })
|
|
|
|
userRouter.Get("/config", func(w http.ResponseWriter, r *http.Request) { GetEntry(w, r, "config.txt", ConfigFile, true) })
|
2024-05-09 23:44:37 +03:00
|
|
|
userRouter.Post("/config", PostConfig)
|
2024-05-05 13:06:20 +03:00
|
|
|
r.Mount("/", userRouter)
|
2024-03-15 18:34:24 +03:00
|
|
|
|
|
|
|
// API =============
|
|
|
|
apiRouter := chi.NewRouter()
|
2024-05-05 13:06:20 +03:00
|
|
|
apiRouter.Use(BasicAuth)
|
2024-06-17 00:54:55 +03:00
|
|
|
apiRouter.Get("/readme", func(w http.ResponseWriter, r *http.Request) { GetFileApi("readme", w) })
|
|
|
|
apiRouter.Post("/readme", func(w http.ResponseWriter, r *http.Request) { PostFileApi("readme", w, r) })
|
2024-03-28 10:21:04 +03:00
|
|
|
apiRouter.Get("/day", func(w http.ResponseWriter, r *http.Request) { GetFileList("day", w) })
|
|
|
|
apiRouter.Get("/day/{day}", GetDayApi)
|
|
|
|
apiRouter.Get("/notes", func(w http.ResponseWriter, r *http.Request) { GetFileList("notes", w) })
|
2024-03-30 14:22:03 +03:00
|
|
|
apiRouter.Get("/notes/{note}", GetNoteApi)
|
|
|
|
apiRouter.Post("/notes/{note}", PostNoteApi)
|
2024-06-17 00:54:55 +03:00
|
|
|
apiRouter.Get("/today", func(w http.ResponseWriter, r *http.Request) { GetFileApi(DataFile("day/"+TodayDate()), w) })
|
|
|
|
apiRouter.Post("/today", func(w http.ResponseWriter, r *http.Request) { PostEntry(DataFile("day/"+TodayDate()), w, r) })
|
2024-03-23 15:36:01 +03:00
|
|
|
apiRouter.Get("/export", GetExport)
|
2024-05-06 14:53:18 +03:00
|
|
|
apiRouter.Get("/grace", GraceActiveApi)
|
2024-05-07 13:27:11 +03:00
|
|
|
apiRouter.Get("/version", GetVersionApi)
|
2024-05-07 14:55:11 +03:00
|
|
|
apiRouter.Get("/reload", ConfigReloadApi)
|
2024-03-15 18:34:24 +03:00
|
|
|
r.Mount("/api", apiRouter)
|
|
|
|
|
|
|
|
// Static files
|
2024-05-18 16:15:20 +03:00
|
|
|
fs := http.FileServer(http.FS(Public))
|
|
|
|
r.Handle("/public/*", fs)
|
2024-03-15 18:34:24 +03:00
|
|
|
|
2024-03-20 16:18:23 +03:00
|
|
|
slog.Info("🌺 Website working", "port", Cfg.Port)
|
2024-05-04 14:29:49 +03:00
|
|
|
slog.Debug("Debug mode enabled")
|
2024-03-18 19:33:57 +03:00
|
|
|
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r))
|
2024-03-15 18:34:24 +03:00
|
|
|
}
|