htmgo/main.go

76 lines
1.6 KiB
Go
Raw Normal View History

2024-01-22 15:22:16 +00:00
package main
import (
"github.com/gofiber/fiber/v2"
2024-09-11 00:52:18 +00:00
"github.com/google/uuid"
2024-09-11 17:31:40 +00:00
"log"
2024-09-11 18:09:55 +00:00
"mhtml/database"
2024-01-22 15:22:16 +00:00
"mhtml/h"
2024-09-11 00:52:18 +00:00
"mhtml/pages"
"mhtml/partials"
2024-09-11 18:09:55 +00:00
"mhtml/partials/sheet"
2024-09-11 17:31:40 +00:00
"time"
2024-01-22 15:22:16 +00:00
)
func main() {
2024-09-11 00:52:18 +00:00
f := fiber.New()
f.Static("/js", "./js")
f.Use(func(ctx *fiber.Ctx) error {
if ctx.Cookies("mhtml-session") != "" {
return ctx.Next()
}
id := ctx.IP() + uuid.NewString()
ctx.Cookie(&fiber.Cookie{
Name: "mhtml-session",
Value: id,
SessionOnly: true,
})
return ctx.Next()
})
2024-01-22 15:22:16 +00:00
2024-09-11 17:31:40 +00:00
f.Use(func(ctx *fiber.Ctx) error {
if ctx.Path() == "/livereload" {
return ctx.Next()
}
now := time.Now()
err := ctx.Next()
duration := time.Since(now)
ctx.Set("X-Response-Time", duration.String())
// Log or print the request method, URL, and duration
log.Printf("Request: %s %s took %v", ctx.Method(), ctx.OriginalURL(), duration)
return err
})
f.Get("/mhtml/partials*", func(ctx *fiber.Ctx) error {
partial := partials.GetPartialFromContext(ctx)
if partial == nil {
return ctx.SendStatus(404)
}
return h.PartialView(ctx, partial)
2024-01-22 15:22:16 +00:00
})
2024-09-11 00:52:18 +00:00
pages.RegisterPages(f)
2024-01-22 15:22:16 +00:00
2024-09-11 18:09:55 +00:00
f.Post("/api/patients", func(ctx *fiber.Ctx) error {
name := ctx.FormValue("name")
reason := ctx.FormValue("reason-for-visit")
location := ctx.FormValue("location-name")
database.HSet("patients", uuid.New().String(), partials.Patient{
Name: name,
ReasonForVisit: reason,
AppointmentDate: time.Now(),
LocationName: location,
})
return h.PartialViewWithHeaders(ctx, &map[string]string{
"HX-Trigger": "patient-added",
}, sheet.Close(ctx))
})
2024-09-11 00:52:18 +00:00
h.Start(f, h.App{
LiveReload: true,
2024-01-22 15:22:16 +00:00
})
}