block logic
This commit is contained in:
@@ -2,14 +2,16 @@ package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"git.oblat.lv/alex/triggerssmith/api/invoke"
|
||||
"git.oblat.lv/alex/triggerssmith/api/block"
|
||||
"git.oblat.lv/alex/triggerssmith/internal/config"
|
||||
"git.oblat.lv/alex/triggerssmith/internal/vars"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
@@ -26,12 +28,31 @@ func NewRouter(cfg *config.Config) *Router {
|
||||
}
|
||||
}
|
||||
|
||||
// RouteHandler sets up the routes and middleware for the router.
|
||||
// TODO: implement hot reload for static files enabled/disabled
|
||||
func (r *Router) RouteHandler() chi.Router {
|
||||
r.r.Get("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
http.ServeFile(w, req, filepath.Join(r.cfg.Server.StaticFilesPath, "index.html"))
|
||||
r.r.Use(middleware.Logger)
|
||||
r.r.Use(middleware.Recoverer)
|
||||
r.r.Use(middleware.Timeout(r.cfg.Server.TimeoutSeconds))
|
||||
|
||||
if r.cfg.Server.StaticConfig.Enabled {
|
||||
slog.Debug("Static file serving is enabled",
|
||||
slog.String("dir", r.cfg.Server.StaticConfig.Dir),
|
||||
slog.String("index_file", r.cfg.Server.StaticConfig.IndexFile),
|
||||
)
|
||||
r.r.Get("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
http.ServeFile(w, req, filepath.Join(r.cfg.Server.StaticConfig.Dir, r.cfg.Server.StaticConfig.IndexFile))
|
||||
})
|
||||
fs := http.FileServer(http.Dir(r.cfg.Server.StaticConfig.Dir))
|
||||
r.r.Handle("/static/*", http.StripPrefix("/static/", fs))
|
||||
} else {
|
||||
slog.Info("Static file serving is disabled")
|
||||
}
|
||||
|
||||
r.r.Route("/api", func(api chi.Router) {
|
||||
api.Get("/block/*", block.LoadHtmlBlock(r.cfg))
|
||||
})
|
||||
fs := http.FileServer(http.Dir("static"))
|
||||
r.r.Handle("/static/*", http.StripPrefix("/static/", fs))
|
||||
|
||||
r.r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := json.Marshal(struct {
|
||||
Status string `json:"status"`
|
||||
@@ -42,6 +63,6 @@ func (r *Router) RouteHandler() chi.Router {
|
||||
})
|
||||
w.Write([]byte(b))
|
||||
})
|
||||
r.r.Handle("/invoke/function/{function_id}/{function_version}", invoke.InvokeHandler(r.cfg))
|
||||
//r.r.Handle("/invoke/function/{function_id}/{function_version}", invoke.InvokeHandler(r.cfg))
|
||||
return r.r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user