Compare commits

..

10 Commits

Author SHA1 Message Date
d6859d8cf9 fmt 2025-12-17 14:24:29 +02:00
99ffa05c61 add doc 2025-12-17 14:24:19 +02:00
c6d5fa02d1 add base for auth package 2025-12-17 14:24:07 +02:00
b92682177c back to "old" way to parse url because it dont work with more than 2 "/" 2025-12-17 14:22:40 +02:00
4be8faaa67 change error status 2025-12-17 14:15:24 +02:00
e78fd22f51 add message for disabled static 2025-12-17 14:15:09 +02:00
7a189d56ea add auth api router, change block api router, fix ussue with "*" 2025-12-17 14:12:29 +02:00
441253351d use modern way to parse URL 2025-12-17 14:10:40 +02:00
ffec908ca4 isolate handlers 2025-12-17 14:10:16 +02:00
26052db142 add package header documentation 2025-12-17 13:49:04 +02:00
3 changed files with 79 additions and 16 deletions

34
api/auth/handle.go Normal file
View File

@@ -0,0 +1,34 @@
// Package auth provides authentication-related API endpoints for the Triggersmith application.
// It handles login, logout, and user management operations.
package auth
import (
"net/http"
"git.oblat.lv/alex/triggerssmith/internal/config"
"github.com/go-chi/chi/v5"
)
type authHandler struct {
cfg *config.Config
}
func Route(config *config.Config) func(chi.Router) {
h := &authHandler{
cfg: config,
}
return func(r chi.Router) {
r.Get("/login", h.handleLogin)
r.Get("/logout", h.handleLogout)
r.Get("/me", h.handleMe)
r.Get("/revoke", h.handleRevoke)
}
}
func (h *authHandler) handleLogin(w http.ResponseWriter, r *http.Request) {}
func (h *authHandler) handleLogout(w http.ResponseWriter, r *http.Request) {}
func (h *authHandler) handleMe(w http.ResponseWriter, r *http.Request) {}
func (h *authHandler) handleRevoke(w http.ResponseWriter, r *http.Request) {}

View File

@@ -1,3 +1,11 @@
// Package block provides functionality to load HTML blocks with associated content, JavaScript, and CSS from the filesystem.
// API Endpoint:
//
// /api/block/{blockPath}
//
// Example:
//
// /api/block/header would load the block located at {BlockDir}/header/
package block
import (
@@ -8,6 +16,7 @@ import (
"path/filepath"
"git.oblat.lv/alex/triggerssmith/internal/config"
"github.com/go-chi/chi/v5"
)
type Block struct {
@@ -16,15 +25,27 @@ type Block struct {
CSS string `json:"css"`
}
func LoadHtmlBlock(cfg *config.Config) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !cfg.Server.BlockConfig.Enabled {
http.Error(w, "Block serving is disabled", http.StatusNotImplemented)
type blockHandler struct {
cfg *config.Config
}
func Route(config *config.Config) func(chi.Router) {
h := &blockHandler{
cfg: config,
}
return func(r chi.Router) {
r.Get("/*", h.handleBlock)
}
}
func (h *blockHandler) handleBlock(w http.ResponseWriter, r *http.Request) {
if !h.cfg.Server.BlockConfig.Enabled {
http.Error(w, "Block serving is disabled", http.StatusForbidden)
return
}
blockPath := r.URL.Path[len("/api/block/"):]
block, err := LoadBlock(blockPath, cfg)
block, err := LoadBlock(blockPath, h.cfg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -33,7 +54,6 @@ func LoadHtmlBlock(cfg *config.Config) func(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusOK)
w.Write([]byte(block.ToJSON()))
}
}
// LoadBlock loads a block from the filesystem given its path and configuration.
// It reads the content, JavaScript, and CSS files associated with the block.

View File

@@ -1,3 +1,4 @@
// Package api provides the main API router and route handlers for the Triggersmith application.
package api
import (
@@ -7,6 +8,7 @@ import (
"path/filepath"
"time"
"git.oblat.lv/alex/triggerssmith/api/auth"
"git.oblat.lv/alex/triggerssmith/api/block"
"git.oblat.lv/alex/triggerssmith/internal/config"
"git.oblat.lv/alex/triggerssmith/internal/vars"
@@ -47,10 +49,17 @@ func (r *Router) RouteHandler() chi.Router {
r.r.Handle("/static/*", http.StripPrefix("/static/", fs))
} else {
slog.Info("Static file serving is disabled")
r.r.Get("/", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Static serving is disabled", http.StatusForbidden)
})
r.r.HandleFunc("/static/*", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Static serving is disabled", http.StatusForbidden)
})
}
r.r.Route("/api", func(api chi.Router) {
api.Get("/block/*", block.LoadHtmlBlock(r.cfg))
api.Route("/block", block.Route(r.cfg))
api.Route("/auth", auth.Route(r.cfg))
})
r.r.Get("/health", func(w http.ResponseWriter, r *http.Request) {