Get and set site name

This commit is contained in:
2024-11-16 18:21:51 -07:00
parent beef4523c2
commit 7aa3c4fb3f
10 changed files with 235 additions and 100 deletions

5
desktop/fields/fields.go Normal file
View File

@ -0,0 +1,5 @@
package fields
const (
SiteName = "sitename"
)

View File

@ -19,12 +19,10 @@ var assets embed.FS
const ListenPort = "9245"
const WindowNameMain = "mainwindow"
var globalDB *peachy.DB
var globalDB peachy.DB
func closeDB() {
if globalDB != nil {
globalDB.Close()
}
globalDB.Close()
}
// SetURl is a workaround for WebviewWindow.SetURL not adding the base URL
@ -36,10 +34,10 @@ func SetURL(win application.Window, path string) application.Window {
return win.SetURL(u)
}
func setDB(db *peachy.DB) error {
func setDB(db peachy.DB) error {
slog.Info("opened DB, setting URL")
globalDB = db
SetURL(application.Get().GetWindowByName(WindowNameMain), "/").SetTitle("you opened a DB")
SetURL(application.Get().GetWindowByName(WindowNameMain), "/site")
return nil
}
@ -73,13 +71,12 @@ func dbFileFilter() []application.FileFilter {
}
func main() {
r := routes.NewChiRouter(ListenPort)
r := routes.NewChiRouter(ListenPort, &globalDB)
// Create the application
app := application.New(application.Options{
Name: "Peachy",
Description: "A demo of using raw HTML & CSS", // Description of the application
Assets: application.AssetOptions{ // Assets to embed (our static files)
Description: "A desktop CMS for static sites",
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
Middleware: func(next http.Handler) http.Handler {
r.NotFound(next.ServeHTTP)
@ -94,16 +91,15 @@ func main() {
closeDB()
})
// V3 introduces multiple windows, so we need to create a window
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Name: WindowNameMain,
Title: "Your Project",
Mac: application.MacWindow{
Backdrop: application.MacBackdropTranslucent,
},
URL: "/splash", // URL to load when the window is created
Width: 1080, // Width of the window
Height: 720, // Height of the window
URL: "/", // URL to load when the window is created
Width: 1080, // Width of the window
Height: 720, // Height of the window
Centered: false,
})

View File

@ -4,40 +4,37 @@ import (
h "git.codemonkeysoftware.net/b/hatmill"
a "git.codemonkeysoftware.net/b/hatmill/attribute"
e "git.codemonkeysoftware.net/b/hatmill/element"
"git.codemonkeysoftware.net/b/peachy-go"
"git.codemonkeysoftware.net/b/peachy-go/desktop/fields"
"git.codemonkeysoftware.net/b/peachy-go/hx"
)
func hxGet(url string) h.Attrib {
return h.Attrib{
Key: "hx-get",
Value: a.String(url),
func HomePage(db *peachy.DB) (func() h.ParentElement, error) {
siteName, err := db.SiteName()
if err != nil {
return nil, err
}
}
func hxTrigger(event string) h.Attrib {
return h.Attrib{
Key: "hx-trigger",
Value: a.String(event),
}
}
func hxTarget(target string) h.Attrib {
return h.Attrib{
Key: "hx-target",
Value: a.String(target),
}
}
func HomePage() h.ParentElement {
return e.Div()(
e.H1()(h.Text("[untitled]")),
e.Button(
a.Type("button"),
hxGet("/hello"),
hxTrigger("click"),
hxTarget("#hello"),
)(h.Text("Click Here!")),
e.Div(a.Id("hello"))(),
)
return func() h.ParentElement {
return e.Div()(
e.Input(
a.Type("text"),
a.Value(siteName),
a.Placeholder("site name"),
a.Name(fields.SiteName),
hx.Trigger("change"),
hx.Target("this"),
hx.Patch("/site"),
hx.Swap(hx.OuterHTML),
),
e.Button(
a.Type("button"),
hx.Get("/hello"),
hx.Trigger("click"),
hx.Target("#hello"),
)(h.Text("Click Here!")),
e.Div(a.Id("hello"))(),
)
}, nil
}
func Splash() h.Term {

View File

@ -4,7 +4,9 @@ import (
"log/slog"
"net/http"
"git.codemonkeysoftware.net/b/peachy-go"
"git.codemonkeysoftware.net/b/peachy-go/desktop/components"
"git.codemonkeysoftware.net/b/peachy-go/desktop/fields"
"git.codemonkeysoftware.net/b/peachy-go/desktop/pages"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
@ -14,7 +16,7 @@ import (
/*
Create a new chi router, configure it and return it.
*/
func NewChiRouter(port string) *chi.Mux {
func NewChiRouter(port string, db *peachy.DB) *chi.Mux {
r := chi.NewRouter()
@ -40,14 +42,31 @@ func NewChiRouter(port string) *chi.Mux {
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
r.Get("/splash", func(w http.ResponseWriter, r *http.Request) {
slog.Info("we splashin")
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
HXRender(w, r, pages.Splash)
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
slog.Info("we homies")
HXRender(w, r, pages.HomePage)
r.Get("/site", func(w http.ResponseWriter, r *http.Request) {
page, err := pages.HomePage(db)
if err != nil {
logger.Error("internal error", "path", r.URL.Path, "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
HXRender(w, r, page)
})
r.Patch("/site", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.Form.Has(fields.SiteName) {
err := db.SetSiteName(r.Form.Get(fields.SiteName))
if err != nil {
logger.Error("internal error", "path", r.URL.Path, "error", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
})
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {