Sites → New site fills an empty Gitea repository with the theme, a hugo.toml in the site's brand and a starter home page: checked that the repository is empty and builds with Hugo, then the first commit is pushed to its default branch. The site joins the profile, and a First pages card builds its Home, About, Contact and product pages with the page builder; About and Contact join the top menu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/profile"
|
|
"supermarket/internal/sites"
|
|
)
|
|
|
|
func (s *Server) newSitePage(w http.ResponseWriter, r *http.Request) {
|
|
s.renderNewSite(w, r, sites.Defaults(), "")
|
|
}
|
|
|
|
func (s *Server) renderNewSite(w http.ResponseWriter, r *http.Request, n sites.NewSite, errMsg string) {
|
|
gitea := ""
|
|
if s.gitea() != nil {
|
|
gitea = s.giteaURL()
|
|
}
|
|
s.render(w, r, "sites-new", page{Title: "New site", Nav: "sites/new", Error: errMsg, Data: map[string]any{
|
|
"N": n, "Fonts": sites.Fonts, "Gitea": gitea,
|
|
}})
|
|
}
|
|
|
|
// newSiteCreate makes the new site: the first commit to its empty
|
|
// repository, then the site in the profile, read like the others.
|
|
func (s *Server) newSiteCreate(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
f := r.PostFormValue
|
|
n := sites.NewSite{Repo: f("repo"), URL: f("url"), Title: f("title"), Tagline: f("tagline"), Email: f("email"),
|
|
Dark: f("dark"), Primary: f("primary"), Accent: f("accent"), Font: f("font"), Logo: f("logo")}
|
|
if err := n.Check(); err != nil {
|
|
s.renderNewSite(w, r, n, err.Error())
|
|
return
|
|
}
|
|
prof, err := profile.Load(s.DB)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
if prof == nil {
|
|
prof = &profile.Profile{}
|
|
}
|
|
for _, ws := range prof.Websites {
|
|
if strings.EqualFold(strings.TrimRight(ws.URL, "/"), n.URL) {
|
|
s.renderNewSite(w, r, n, n.URL+" is already one of your websites.")
|
|
return
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
|
|
defer cancel()
|
|
branch, err := sites.Create(ctx, s.gitea(), n, s.siteDir(n.Repo), s.commitAuthor())
|
|
if err != nil {
|
|
s.renderNewSite(w, r, n, err.Error())
|
|
return
|
|
}
|
|
prof.Websites = append(prof.Websites, profile.Website{URL: n.URL, Repo: n.Repo, Branch: branch})
|
|
if err := profile.Save(s.DB, prof); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
_ = s.DB.PutJSON(siteSyncKey(n.Repo), siteSynced{At: time.Now(), Branch: branch})
|
|
index := 0
|
|
if p, _ := profile.Load(s.DB); p != nil {
|
|
for i, ws := range p.Websites {
|
|
if strings.TrimRight(ws.URL, "/") == n.URL {
|
|
index = i
|
|
}
|
|
}
|
|
}
|
|
http.Redirect(w, r, "/sites?site="+strconv.Itoa(index)+"&msg=site-created", http.StatusSeeOther)
|
|
}
|