feat: static files injection into router

This commit is contained in:
Guz
2025-03-07 20:48:11 -03:00
parent bc658d7dc8
commit 6e2664756b
3 changed files with 25 additions and 1 deletions

View File

@@ -51,6 +51,9 @@ func main() {
}
if *dev {
d := os.DirFS("./static")
opts = append(opts, comicverse.WithStaticFiles(d))
opts = append(opts, comicverse.WithDevelopmentMode())
}

View File

@@ -10,11 +10,13 @@ import (
"net/http"
"forge.capytal.company/capytalcode/project-comicverse/router"
"forge.capytal.company/capytalcode/project-comicverse/static"
"forge.capytal.company/loreddev/x/tinyssert"
)
func New(cfg Config, opts ...Option) (http.Handler, error) {
app := &app{
staticFiles: static.Files(),
developmentMode: false,
context: context.Background(),
@@ -26,6 +28,10 @@ func New(cfg Config, opts ...Option) (http.Handler, error) {
opt(app)
}
if app.staticFiles == nil {
return nil, errors.New("static files must not be a nil interface")
}
if app.context == nil {
return nil, errors.New("context must not be a nil interface")
}
@@ -69,6 +75,7 @@ func WithDevelopmentMode() Option {
type app struct {
handler http.Handler
staticFiles fs.FS
developmentMode bool
context context.Context
@@ -77,6 +84,7 @@ type app struct {
}
func (app *app) setup() error {
app.assert.NotNil(app.staticFiles)
app.assert.NotNil(app.context)
app.assert.NotNil(app.logger)
@@ -84,6 +92,8 @@ func (app *app) setup() error {
app.handler, err = router.New(router.Config{
DisableCache: app.developmentMode,
StaticFiles: app.staticFiles,
Assertions: app.assert,
Logger: app.logger,
})
@@ -93,6 +103,7 @@ func (app *app) setup() error {
return err
}
func (app *app) ServeHTTP(w http.ResponseWriter, r *http.Request) {
app.assert.NotNil(app.handler)
app.handler.ServeHTTP(w, r)

View File

@@ -1,10 +1,11 @@
package router
import (
"errors"
"io/fs"
"log/slog"
"net/http"
"forge.capytal.company/capytalcode/project-comicverse/templates"
"forge.capytal.company/loreddev/x/smalltrip"
"forge.capytal.company/loreddev/x/smalltrip/exception"
"forge.capytal.company/loreddev/x/smalltrip/middleware"
@@ -12,6 +13,7 @@ import (
)
type router struct {
staticFiles fs.FS
cache bool
assert tinyssert.Assertions
@@ -19,6 +21,9 @@ type router struct {
}
func New(cfg Config) (http.Handler, error) {
if cfg.StaticFiles == nil {
return nil, errors.New("static files handler is nil")
}
if cfg.Assertions == nil {
return nil, errors.New("assertions is nil")
}
@@ -27,6 +32,8 @@ func New(cfg Config) (http.Handler, error) {
}
r := &router{
staticFiles: cfg.StaticFiles,
cache: !cfg.DisableCache,
assert: cfg.Assertions,
log: cfg.Logger,
@@ -36,6 +43,7 @@ func New(cfg Config) (http.Handler, error) {
}
type Config struct {
StaticFiles fs.FS
DisableCache bool
Assertions tinyssert.Assertions
@@ -44,6 +52,7 @@ type Config struct {
func (router *router) setup() http.Handler {
router.assert.NotNil(router.log)
router.assert.NotNil(router.staticFiles)
log := router.log
@@ -64,6 +73,7 @@ func (router *router) setup() http.Handler {
r.Use(exception.PanicMiddleware())
r.Use(exception.Middleware())
r.Handle("/static", http.StripPrefix("/static/", http.FileServerFS(router.staticFiles)))
r.HandleFunc("/dashboard", router.dashboard)
return r