2024-04-09 14:30:54 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
2024-04-27 20:00:30 +07:00
|
|
|
"os"
|
2024-04-09 14:30:54 +07:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
chimiddleware "github.com/go-chi/chi/v5/middleware"
|
2024-04-13 00:15:31 +07:00
|
|
|
"github.com/riandyrn/otelchi"
|
2024-04-09 14:30:54 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api"
|
|
|
|
"github.com/tigorlazuardi/redmage/config"
|
|
|
|
"github.com/tigorlazuardi/redmage/server/routes/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Routes struct {
|
|
|
|
API *api.API
|
|
|
|
Config *config.Config
|
|
|
|
PublicDir fs.FS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (routes *Routes) Register(router chi.Router) {
|
2024-04-27 20:00:30 +07:00
|
|
|
router.Use(chimiddleware.Compress(5, "text/html", "text/css", "application/javascript", "application/json", "image/svg+xml", "image/x-icon"))
|
2024-04-24 21:57:13 +07:00
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
router.HandleFunc("/ping", routes.HealthCheck)
|
|
|
|
router.HandleFunc("/health", routes.HealthCheck)
|
|
|
|
if routes.Config.Bool("http.hotreload") {
|
|
|
|
router.Get("/hot_reload", routes.CreateHotReloadRoute())
|
|
|
|
}
|
|
|
|
|
2024-05-02 20:59:32 +07:00
|
|
|
router.Route("/htmx", routes.registerHTMXRoutes)
|
2024-04-09 14:30:54 +07:00
|
|
|
router.Route("/api/v1", routes.registerV1APIRoutes)
|
2024-05-02 20:59:32 +07:00
|
|
|
|
|
|
|
router.Group(routes.registerWWWRoutes)
|
2024-04-09 14:30:54 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (routes *Routes) registerV1APIRoutes(router chi.Router) {
|
2024-04-13 00:15:31 +07:00
|
|
|
router.Use(otelchi.Middleware("redmage"))
|
2024-04-09 14:30:54 +07:00
|
|
|
router.Use(chimiddleware.RequestLogger(middleware.ChiLogger{}))
|
|
|
|
router.Use(chimiddleware.SetHeader("Content-Type", "application/json"))
|
|
|
|
|
2024-04-25 20:22:05 +07:00
|
|
|
router.Post("/subreddits/start", routes.SubredditStartDownloadAPI)
|
2024-04-09 14:30:54 +07:00
|
|
|
router.Get("/subreddits", routes.SubredditsListAPI)
|
2024-04-25 20:22:05 +07:00
|
|
|
router.Post("/subreddits", routes.SubredditsCreateAPI)
|
2024-04-26 10:51:09 +07:00
|
|
|
router.Post("/subreddits/check", routes.SubredditsCheckAPI)
|
2024-04-25 20:22:05 +07:00
|
|
|
|
2024-04-14 17:30:04 +07:00
|
|
|
router.Get("/devices", routes.APIDeviceList)
|
2024-04-14 20:21:45 +07:00
|
|
|
router.Post("/devices", routes.APIDeviceCreate)
|
2024-04-30 14:12:33 +07:00
|
|
|
router.Patch("/devices/{slug}", routes.APIDeviceUpdate)
|
2024-04-26 15:13:29 +07:00
|
|
|
|
2024-04-27 20:00:30 +07:00
|
|
|
router.Get("/images", routes.ImagesListAPI)
|
|
|
|
|
2024-04-26 15:13:29 +07:00
|
|
|
router.Get("/events", routes.EventsAPI)
|
2024-04-09 14:30:54 +07:00
|
|
|
}
|
|
|
|
|
2024-05-02 20:59:32 +07:00
|
|
|
func (routes *Routes) registerHTMXRoutes(router chi.Router) {
|
|
|
|
router.Use(otelchi.Middleware("redmage"))
|
|
|
|
router.Use(chimiddleware.RequestLogger(middleware.ChiLogger{}))
|
|
|
|
router.Use(chimiddleware.SetHeader("Content-Type", "text/html; charset=utf-8"))
|
|
|
|
|
2024-05-03 19:44:25 +07:00
|
|
|
router.Post("/subreddits/add", routes.SubredditsCreateHTMX)
|
2024-05-02 20:59:32 +07:00
|
|
|
router.Post("/subreddits/start", routes.SubredditStartDownloadHTMX)
|
2024-05-02 23:16:28 +07:00
|
|
|
router.Post("/subreddits/check", routes.SubredditCheckHTMX)
|
2024-05-03 11:59:44 +07:00
|
|
|
router.Get("/subreddits/validate/schedule", routes.SubredditValidateScheduleHTMX)
|
2024-05-07 16:48:15 +07:00
|
|
|
|
2024-05-08 11:19:14 +07:00
|
|
|
router.Get("/devices/add/validate/slug", routes.DevicesValidateSlugHTMX)
|
|
|
|
router.Get("/devices/add/validate/name", routes.DevicesValidateNameHTMX)
|
2024-05-02 20:59:32 +07:00
|
|
|
}
|
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
func (routes *Routes) registerWWWRoutes(router chi.Router) {
|
|
|
|
router.Mount("/public", http.StripPrefix("/public", http.FileServer(http.FS(routes.PublicDir))))
|
|
|
|
|
2024-04-27 20:00:30 +07:00
|
|
|
imagesDir := http.FS(os.DirFS(routes.Config.String("download.directory")))
|
|
|
|
|
|
|
|
router.Mount("/img", http.StripPrefix("/img", http.FileServer(imagesDir)))
|
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
router.Group(func(r chi.Router) {
|
2024-04-13 00:15:31 +07:00
|
|
|
r.Use(otelchi.Middleware("redmage"))
|
2024-04-09 16:09:08 +07:00
|
|
|
r.Use(chimiddleware.RequestID)
|
2024-04-09 14:30:54 +07:00
|
|
|
r.Use(chimiddleware.RequestLogger(middleware.ChiLogger{}))
|
2024-04-09 16:09:08 +07:00
|
|
|
r.Use(chimiddleware.SetHeader("Content-Type", "text/html; charset=utf-8"))
|
2024-04-09 14:30:54 +07:00
|
|
|
r.Get("/", routes.PageHome)
|
2024-04-30 15:28:04 +07:00
|
|
|
r.Get("/subreddits", routes.PageSubreddits)
|
2024-05-02 15:42:10 +07:00
|
|
|
r.Get("/subreddits/details/{name}", routes.PageSubredditsDetails)
|
2024-05-02 23:16:28 +07:00
|
|
|
r.Get("/subreddits/add", routes.PageSubredditsAdd)
|
2024-05-02 09:25:06 +07:00
|
|
|
r.Get("/config", routes.PageConfig)
|
2024-05-06 18:50:43 +07:00
|
|
|
r.Get("/devices", routes.PageDevices)
|
2024-05-07 10:50:40 +07:00
|
|
|
r.Get("/devices/add", routes.PageDevicesAdd)
|
2024-05-08 11:19:14 +07:00
|
|
|
r.Post("/devices/add", routes.DevicesCreateHTMX)
|
2024-05-06 21:09:52 +07:00
|
|
|
r.Get("/devices/details/{slug}", routes.PageDeviceDetails)
|
2024-05-08 19:32:14 +07:00
|
|
|
r.Get("/devices/edit/{slug}", routes.PageDevicesEdit)
|
|
|
|
r.Post("/devices/edit/{slug}", routes.DevicesUpdateHTMX)
|
2024-05-04 22:17:50 +07:00
|
|
|
r.Get("/schedules", routes.PageScheduleHistory)
|
2024-04-09 14:30:54 +07:00
|
|
|
})
|
|
|
|
}
|