Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6859d8cf9 | ||
|
|
99ffa05c61 | ||
|
|
c6d5fa02d1 | ||
|
|
b92682177c | ||
|
|
4be8faaa67 | ||
|
|
e78fd22f51 | ||
|
|
7a189d56ea | ||
|
|
441253351d | ||
|
|
ffec908ca4 | ||
|
|
26052db142 |
@@ -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) {}
|
||||
+35
-15
@@ -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,23 +25,34 @@ 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)
|
||||
return
|
||||
}
|
||||
type blockHandler struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
blockPath := r.URL.Path[len("/api/block/"):]
|
||||
block, err := LoadBlock(blockPath, cfg)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(block.ToJSON()))
|
||||
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, h.cfg)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(block.ToJSON()))
|
||||
}
|
||||
|
||||
// LoadBlock loads a block from the filesystem given its path and configuration.
|
||||
|
||||
+10
-1
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user