Browse Source

feat: load existing sites

main
decentral1se 7 months ago
parent
commit
ce117468ab
No known key found for this signature in database GPG Key ID: 3789458B3D0C410
  1. 22
      app.go
  2. 17
      components/templates.templ
  3. 46
      components/templates_templ.go
  4. 11
      frontend/index.html

22
app.go

@ -91,6 +91,21 @@ func httpGetFile(filepath, url string) error {
return nil
}
func existingSites() ([]string, error) {
var sites []string
siteNameFiles, err := os.ReadDir(SitesDir)
if err != nil {
return sites, err
}
for _, siteNameFile := range siteNameFiles {
sites = append(sites, siteNameFile.Name())
}
return sites, nil
}
func initialise(w http.ResponseWriter, r *http.Request) {
if err := ensureDataDir(); err != nil {
w.Write([]byte(fmt.Sprintf("unable to create data directory: %s", err)))
@ -100,9 +115,12 @@ func initialise(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("unable to download hugo binary: %s", err)))
}
// TODO(d1): is there a Hugo site already?
sites, err := existingSites()
if err != nil {
w.Write([]byte(fmt.Sprintf("unable to list existing sites: %s", err)))
}
templ.Handler(components.NewSiteInput()).ServeHTTP(w, r)
templ.Handler(components.Homepage(sites)).ServeHTTP(w, r)
}
func hugoNewSite(w http.ResponseWriter, r *http.Request) {

17
components/templates.templ

@ -1,7 +1,20 @@
package components
templ NewSiteInput() {
<div>Please enter name of your new Hugo site below 👇</div>
import (
"fmt"
)
templ Homepage(sites []string) {
if len(sites) != 0 {
<div>Configure an existing Hugo site below 👇</div>
<ul>
for _, site := range sites {
<li><a href={ templ.URL(fmt.Sprintf("/%s/config", site)) }>{ site }</a></li>
}
</ul>
}
<div>Create a new Hugo site below 👇</div>
<form id="new-site-form" hx-post="/hugo/new" hx-trigger="submit">
<input type="text" required="required" name="site-name" />
</form>

46
components/templates_templ.go

@ -10,7 +10,11 @@ import "context"
import "io"
import "bytes"
func NewSiteInput() templ.Component {
import (
"fmt"
)
func Homepage(sites []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 {
@ -23,7 +27,45 @@ func NewSiteInput() templ.Component {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div>Please enter name of your new Hugo site below 👇</div><form id=\"new-site-form\" hx-post=\"/hugo/new\" hx-trigger=\"submit\"><input type=\"text\" required=\"required\" name=\"site-name\"></form>")
if len(sites) != 0 {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div>Configure an existing Hugo site below 👇</div><ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, site := range sites {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.SafeURL = templ.URL(fmt.Sprintf("/%s/config", site))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
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
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(site)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `components/templates.templ`, Line: 12, Col: 71}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a></li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</ul>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div>Create a new Hugo site below 👇</div><form id=\"new-site-form\" hx-post=\"/hugo/new\" hx-trigger=\"submit\"><input type=\"text\" required=\"required\" name=\"site-name\"></form>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}

11
frontend/index.html

@ -8,7 +8,14 @@
<script src="https://unpkg.com/htmx.org@1.9.6"></script>
</head>
<body>
<div id="app" hx-get="/init" hx-trigger="load once" hx-indicator="#initial-loader"></div>
<p id="initial-loader" class="htmx-indicator">Initialising Hugo...</p>
<div
id="app"
hx-get="/init"
hx-trigger="load once"
hx-indicator="#initial-loader"
hx-target="this"
hx-swap="outerHtml">
<p id="initial-loader" class="htmx-indicator">Bootstrapping snackbar...</p>
</div>
</body>
</html>