Refactor everything that's used all over into a common/ package and sync files from static/ into public/ so that public's not in the git.

This commit is contained in:
Zed A. Shaw 2025-07-25 11:35:04 -04:00
parent d12817f4cc
commit ec7298cce0
36 changed files with 112 additions and 902 deletions

42
common/api.go Normal file
View file

@ -0,0 +1,42 @@
package common
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/go-playground/validator/v10"
)
type Failure struct {
Message string `json:"message"`
}
func IfErrNil(err error, c *fiber.Ctx) error {
if err != nil {
log.Output(10, err.Error())
c.SendStatus(fiber.StatusInternalServerError)
return c.JSON(Failure{err.Error()})
}
return err
}
func ReceivePost[T any](c *fiber.Ctx) (*T, error) {
var result *T
result = new(T)
if err := c.BodyParser(result); err != nil {
log.Println(err);
return result, err
}
var validate *validator.Validate
validate = validator.New(validator.WithRequiredStructEnabled())
if err := validate.Struct(result); err != nil {
validationErrors := err.(validator.ValidationErrors)
log.Println(validationErrors)
return result, err
}
return result, nil
}

12
common/errors.go Normal file
View file

@ -0,0 +1,12 @@
package common
import (
"log"
"fmt"
)
func Fail(err error, format string, v ...any) error {
err_format := fmt.Sprintf("ERROR: %v; %s", err, format)
log.Printf(err_format, v...)
return err
}

64
common/web.go Normal file
View file

@ -0,0 +1,64 @@
package common
import (
"log"
"strings"
"io/fs"
"path/filepath"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
)
func RenderPages(pages_path string, target string, layout string) {
engine := html.New(pages_path, ".html")
engine.Load()
err := filepath.WalkDir(pages_path,
func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
if err != nil { return Fail(err, "path: %s", path); }
dir := filepath.Dir(path)
err = os.MkdirAll(dir, 0750)
if err != nil {
return Fail(err, "making dir %s", dir);
}
split_path := strings.Split(path, string(os.PathSeparator))[1:]
source_name := strings.Join(split_path, "/") // Render wants / even on windows
ext := filepath.Ext(source_name)
template_name, found := strings.CutSuffix(source_name, ext)
if found && ext == ".html" && template_name != layout {
prefixed_path := append([]string{target}, split_path...)
target_path := filepath.Join(prefixed_path...)
_, err := os.Stat(target_path)
if os.IsNotExist(err) {
target_dir := filepath.Dir(target_path)
log.Println("MAKING: ", target_dir)
os.MkdirAll(target_dir, 0750)
}
// TODO: compare time stamps and skip if not newer
out, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil { return Fail(err, "writing file %s", target_path) }
// generate a data-testid for all pages based on template name
page_id := strings.ReplaceAll(template_name, "/", "-") + "-page"
err = engine.Render(out, template_name, fiber.Map{"PageId": page_id}, layout)
if err != nil { return Fail(err, "failed to render %s", path) }
log.Printf("RENDER: %s -> %s", template_name, target_path)
out.Close()
}
}
return nil
})
if err != nil { log.Fatalf("can't walk content") }
}