|
|
@ -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 |
|
|
|
} |
|
|
|