checkpoint

This commit is contained in:
tavo 2025-06-30 23:29:22 -06:00
parent 92cd81e24c
commit 72a0c64cd0
9 changed files with 126 additions and 37 deletions

View file

@ -4,28 +4,25 @@ import (
"strings" "strings"
) )
var basePartials = []string{ var base = []string{
"views/baseof.html", "templates/baseof.html",
"views/_partials/head.html", "templates/_partials/head.html",
"views/_partials/header.html", "templates/_partials/header.html",
"views/_partials/footer.html", "templates/_partials/footer.html",
} }
var ViewMap = map[string][]string{ var ViewMap = map[string][]string{
"index-page": append(basePartials, "views/index.html", "views/index-page.html"), "login-page": append(
"login-page": append(basePartials, "views/login.html", "views/login-page.html"), base,
"dashboard-page": append(basePartials, "views/dashboard.html", "views/dashboard-page.html", "views/control-madre.html"), "templates/login.html",
"suppliers-page": append(basePartials, "views/suppliers.html", "views/suppliers-page.html"), "templates/login-page.html",
"fse-page": append(basePartials, "views/fse.html", "views/fse-page.html"), ),
"panel-page": append(basePartials, "views/panel.html", "views/panel-page.html", "views/users.html", "views/user.html"),
"login": {"views/login.html"}, "index-page": append(
"dashboard": {"views/dashboard.html", "views/control-madre.html"}, base,
"control-madre": {"views/control-madre.html"}, "templates/index.html",
"user": {"views/user.html"}, "templates/index-page.html",
"users": {"views/users.html", "views/user.html"}, ),
"success-button": {"views/success-button.html"},
"fail-button": {"views/fail-button.html"},
} }
var FuncMap = map[string]any{ var FuncMap = map[string]any{

View file

@ -0,0 +1,19 @@
package handlers
import (
"net/http"
"git.tavo.one/tavo/axiom/views"
)
func (h *Handler) SampleIndex(w http.ResponseWriter, r *http.Request) {
views.RenderHTML(w, r, "index-page", nil)
}
func (h *Handler) SampleLoginPage(w http.ResponseWriter, r *http.Request) {
views.RenderHTML(w, r, "login-page", nil)
}
func (h *Handler) SampleLoginForm(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Authentication complete!"))
}

View file

@ -1,9 +0,0 @@
package handlers
import (
"net/http"
)
func (h *Handler) SampleIndex(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello from Index!"))
}

20
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"io/fs"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -15,10 +16,10 @@ import (
"git.tavo.one/tavo/axiom/views" "git.tavo.one/tavo/axiom/views"
) )
// //go:embed static/* //go:embed static/*
// var publicFS embed.FS var publicFS embed.FS
//go:embed views/* //go:embed templates/*
var viewFS embed.FS var viewFS embed.FS
func init() { func init() {
@ -62,8 +63,17 @@ func main() {
} }
handler := handlers.New(handlerConfig) handler := handlers.New(handlerConfig)
router := http.NewServeMux() router := routes(handler)
router.HandleFunc("GET /", handler.SampleIndex)
staticFiles, err := fs.Sub(publicFS, "static")
if err != nil {
log.Fatalf("failed to create static files filesystem: %v", err)
}
router.Handle(
"GET /s/",
http.StripPrefix("/s/", http.FileServer(http.FS(staticFiles))),
)
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)

13
middleware/example.go Normal file
View file

@ -0,0 +1,13 @@
package middleware
import (
"log"
"net/http"
)
func AuthCheck(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Sample Authenticated request: %s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}

18
middleware/middleware.go Normal file
View file

@ -0,0 +1,18 @@
package middleware
import "net/http"
type Middleware func(http.Handler) http.Handler
func Stack(xs ...Middleware) Middleware {
return func(next http.Handler) http.Handler {
for i := len(xs) - 1; i >= 0; i-- {
next = xs[i](next)
}
return next
}
}
func With(mw Middleware, h http.HandlerFunc) http.Handler {
return mw(h)
}

33
routes.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"net/http"
"git.tavo.one/tavo/axiom/handlers"
"git.tavo.one/tavo/axiom/middleware"
)
func routes(handler *handlers.Handler) *http.ServeMux {
router := http.NewServeMux()
protectedStack := middleware.Stack(
middleware.AuthCheck,
)
router.HandleFunc(
"GET /",
handler.SampleIndex,
)
router.HandleFunc(
"GET /login",
handler.SampleLoginPage,
)
router.Handle(
"POST /login",
middleware.With(protectedStack, handler.SampleLoginForm),
)
return router
}

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"io/fs" "io/fs"
@ -68,7 +67,6 @@ func main() {
return fmt.Errorf("failed to minify %s: %w", path, err) return fmt.Errorf("failed to minify %s: %w", path, err)
} }
fmt.Printf("Minified %s -> %s\n", path, dstPath)
return nil return nil
}) })

View file

@ -2,6 +2,7 @@ package views
import ( import (
"bytes" "bytes"
"compress/gzip"
"embed" "embed"
"fmt" "fmt"
"html/template" "html/template"
@ -57,20 +58,29 @@ func initTemplates(viewFS embed.FS, viewMap map[string][]string, funcMap map[str
return cache, nil return cache, nil
} }
func RenderHTML(w http.ResponseWriter, name string, data any) error { func RenderHTML(w http.ResponseWriter, r *http.Request, name string, data any) error {
tmpl, ok := TemplateCache[name] tmpl, ok := TemplateCache[name]
if !ok { if !ok {
http.Error(w, "template not found: "+name, http.StatusNotFound) http.Error(w, "template not found: "+name, http.StatusNotFound)
return fmt.Errorf("template %q not found", name) return fmt.Errorf("template %q not found", name)
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8")
var buf bytes.Buffer var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, tmpl.Name(), data); err != nil { if err := tmpl.ExecuteTemplate(&buf, tmpl.Name(), data); err != nil {
return fmt.Errorf("failed to execute template %q: %w", name, err) return fmt.Errorf("failed to execute template %q: %w", name, err)
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Vary", "Accept-Encoding")
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
_, err := io.Copy(gz, &buf)
return err
}
_, err := io.Copy(w, &buf) _, err := io.Copy(w, &buf)
return err return err
} }