From 978b2f3cd396acd612521ca409b9b37d0e77d474 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Mon, 22 Apr 2024 21:09:29 +0200 Subject: [PATCH] wip: fire up hugo serve on startup --- app.go | 39 +++++++++++++++++++++++++++++++++++ components/templates.templ | 4 ++++ components/templates_templ.go | 37 +++++++++++++++++++++++++++++++++ main.go | 2 ++ 4 files changed, 82 insertions(+) diff --git a/app.go b/app.go index 6d33c94..fb66769 100644 --- a/app.go +++ b/app.go @@ -8,10 +8,12 @@ import ( "fmt" "io" "io/ioutil" + "math/rand" "net/http" "os" "os/exec" "path/filepath" + "slices" "varia.zone/snackbar/components" @@ -32,6 +34,27 @@ func NewApp() *App { return &App{} } +func (a *App) startup(ctx context.Context) { + sites, err := existingSites() + if err != nil { + // TODO(d1): is this the correct approach for error handling? + panic(fmt.Sprintf("unable to read sites from %s", SitesDir)) + } + + randPort := rand.Intn(1400-1300) + 1300 + for _, siteName := range sites { + cmd := exec.Command(HugoBinPath, "serve", "--port", fmt.Sprintf("%v", randPort), "--buildDrafts", "--source", filepath.Join(SitesDir, siteName), "--theme", "nostyleplease") + if err := cmd.Run(); err != nil { + // TODO(d1): is this the correct approach for error handling? + panic(fmt.Sprintf("unable to serve site: %s", err)) + } + } +} + +func (a *App) shutdown(ctx context.Context) { + fmt.Print("SHUTTING DOWN!") +} + func ensureDataDir() error { paths := []string{LocalDir, HomeDir, HugoDir, SitesDir} for _, fpath := range paths { @@ -147,6 +170,21 @@ func hugoNewSite(w http.ResponseWriter, r *http.Request) { templ.Handler(components.Homepage(sites)).ServeHTTP(w, r) } +func siteConfig(w http.ResponseWriter, r *http.Request) { + siteName := chi.URLParam(r, "site-name") + + sites, err := existingSites() + if err != nil { + w.Write([]byte(fmt.Sprintf("unable to list existing sites: %s", err))) + } + + if !slices.Contains(sites, siteName) { + w.Write([]byte(fmt.Sprintf("site '%s' does not exist?", siteName))) + } + + templ.Handler(components.SiteConfig(siteName)).ServeHTTP(w, r) +} + // NewRouter creates a new web router. func NewRouter() *chi.Mux { router := chi.NewRouter() @@ -156,6 +194,7 @@ func NewRouter() *chi.Mux { router.Get("/init", initialise) router.Post("/hugo/new", hugoNewSite) + router.Get("/{site-name}/config", siteConfig) return router } diff --git a/components/templates.templ b/components/templates.templ index 8df89ee..387d38a 100644 --- a/components/templates.templ +++ b/components/templates.templ @@ -20,3 +20,7 @@ templ Homepage(sites []string) {

Creating new site...

} + +templ SiteConfig(site string) { +

Welcome to WIP config for { site }

+} diff --git a/components/templates_templ.go b/components/templates_templ.go index ae52446..197e7d1 100644 --- a/components/templates_templ.go +++ b/components/templates_templ.go @@ -75,3 +75,40 @@ func Homepage(sites []string) templ.Component { return templ_7745c5c3_Err }) } + +func SiteConfig(site string) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + if templ_7745c5c3_Var4 == nil { + templ_7745c5c3_Var4 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Welcome to WIP config for ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(site) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `components/templates.templ`, Line: 25, Col: 37} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} diff --git a/main.go b/main.go index e1457e0..cfcab0a 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,8 @@ func main() { return router }, }, + OnStartup: app.startup, + OnShutdown: app.shutdown, Bind: []interface{}{ app, },