From 7334de957ae56db50000b1cb465e39e80e5fe213 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Sat, 14 Feb 2026 12:29:47 +0900 Subject: [PATCH] Replace htmlx with hmc --- go.work | 2 +- http/go.mod | 9 +-- http/go.sum | 2 + http/props/htmlx/TestSnapshotForm.snap.json | 25 -------- http/props/htmlx/TestSnapshotForm.snap.xml | 8 --- http/props/htmlx/TestSnapshotLink.snap.json | 4 -- http/props/htmlx/TestSnapshotLink.snap.xml | 1 - http/props/htmlx/form.go | 28 --------- http/props/htmlx/input.go | 65 --------------------- http/props/htmlx/link.go | 24 -------- http/props/htmlx/snapshot_test.go | 54 ----------------- http/props/htmlx/submit.go | 36 ------------ http/props/login.go | 40 +++++-------- http/props/register.go | 50 ++++++++-------- http/routing/account.go | 22 +++---- http/routing/login.go | 27 ++++++--- http/templates/base.gotmpl | 4 +- http/templates/login.gotmpl | 2 +- http/templates/register.gotmpl | 2 +- http/templates/templates.go | 9 +-- 20 files changed, 86 insertions(+), 328 deletions(-) delete mode 100644 http/props/htmlx/TestSnapshotForm.snap.json delete mode 100644 http/props/htmlx/TestSnapshotForm.snap.xml delete mode 100644 http/props/htmlx/TestSnapshotLink.snap.json delete mode 100644 http/props/htmlx/TestSnapshotLink.snap.xml delete mode 100644 http/props/htmlx/form.go delete mode 100644 http/props/htmlx/input.go delete mode 100644 http/props/htmlx/link.go delete mode 100644 http/props/htmlx/snapshot_test.go delete mode 100644 http/props/htmlx/submit.go diff --git a/go.work b/go.work index 5118191..53ba4e5 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.24.5 +go 1.25.5 use ( ./assert diff --git a/http/go.mod b/http/go.mod index 8cb913f..02a922f 100644 --- a/http/go.mod +++ b/http/go.mod @@ -1,8 +1,6 @@ module lishwist/http -go 1.23.3 - -toolchain go1.24.5 +go 1.25.5 require ( github.com/Teajey/rsvp v0.19.0-rc.2 @@ -10,7 +8,10 @@ require ( golang.org/x/crypto v0.39.0 ) -require github.com/gorilla/securecookie v1.1.2 +require ( + github.com/Teajey/hmc v0.6.0-rc.5 + github.com/gorilla/securecookie v1.1.2 +) require ( github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect diff --git a/http/go.sum b/http/go.sum index 041439d..4ededae 100644 --- a/http/go.sum +++ b/http/go.sum @@ -1,3 +1,5 @@ +github.com/Teajey/hmc v0.6.0-rc.5 h1:snog8SPc/FtQ/CrtAoFWuJa7r35XrSOwE8xw1Y1XtnM= +github.com/Teajey/hmc v0.6.0-rc.5/go.mod h1:9K7SL4RyybNLHooqsZcyFoL6QGqqa2zPgIBidw4qWQY= github.com/Teajey/rsvp v0.19.0-rc.2 h1:h4V7c3uGlSKajkWKt3fldFV+XTMEI8A4o05PMY8xv9A= github.com/Teajey/rsvp v0.19.0-rc.2/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= diff --git a/http/props/htmlx/TestSnapshotForm.snap.json b/http/props/htmlx/TestSnapshotForm.snap.json deleted file mode 100644 index cf3431d..0000000 --- a/http/props/htmlx/TestSnapshotForm.snap.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "Method": "POST", - "Elements": { - "Username": { - "Label": "Username", - "Name": "username", - "Required": true, - "Value": "" - }, - "Password": { - "Label": "Password", - "Type": "password", - "Name": "password", - "Required": true, - "Value": "" - }, - "RegisterLink": { - "Href": "/register", - "Name": "Register" - }, - "Submit": { - "Label": "Login" - } - } -} diff --git a/http/props/htmlx/TestSnapshotForm.snap.xml b/http/props/htmlx/TestSnapshotForm.snap.xml deleted file mode 100644 index fbe94cf..0000000 --- a/http/props/htmlx/TestSnapshotForm.snap.xml +++ /dev/null @@ -1,8 +0,0 @@ -
- - - - Register - Login - -
diff --git a/http/props/htmlx/TestSnapshotLink.snap.json b/http/props/htmlx/TestSnapshotLink.snap.json deleted file mode 100644 index dd94c74..0000000 --- a/http/props/htmlx/TestSnapshotLink.snap.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "Href": "/register", - "Name": "Register" -} diff --git a/http/props/htmlx/TestSnapshotLink.snap.xml b/http/props/htmlx/TestSnapshotLink.snap.xml deleted file mode 100644 index 9470b01..0000000 --- a/http/props/htmlx/TestSnapshotLink.snap.xml +++ /dev/null @@ -1 +0,0 @@ -Register diff --git a/http/props/htmlx/form.go b/http/props/htmlx/form.go deleted file mode 100644 index 87feec4..0000000 --- a/http/props/htmlx/form.go +++ /dev/null @@ -1,28 +0,0 @@ -package htmlx - -import ( - "encoding/xml" -) - -type Form[T any] struct { - Method string - Elements T -} - -func (i Form[T]) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - start.Name = xml.Name{Local: "Form"} - - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Method"}, Value: i.Method}) - - err := e.EncodeToken(start) - if err != nil { - return err - } - - err = e.Encode(i.Elements) - if err != nil { - return err - } - - return e.EncodeToken(start.End()) -} diff --git a/http/props/htmlx/input.go b/http/props/htmlx/input.go deleted file mode 100644 index d8c7807..0000000 --- a/http/props/htmlx/input.go +++ /dev/null @@ -1,65 +0,0 @@ -package htmlx - -import ( - "encoding/xml" - "fmt" -) - -type Input struct { - Label string - Type string `json:",omitempty"` - Name string - Required bool `json:",omitempty"` - Value string - MinLength uint `json:",omitempty"` - Error string `json:",omitempty"` -} - -func (i Input) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - start.Name = xml.Name{Local: "Input"} - - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Label"}, Value: i.Label}) - if i.Type != "" { - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Type"}, Value: i.Type}) - } - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Name"}, Value: i.Name}) - if i.Required { - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Required"}, Value: "true"}) - } - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Value"}, Value: i.Value}) - if i.MinLength > 0 { - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "MinLength"}, Value: fmt.Sprintf("%d", i.MinLength)}) - } - - if err := e.EncodeToken(start); err != nil { - return nil - } - - if i.Error != "" { - errorStart := xml.StartElement{Name: xml.Name{Local: "Error"}} - if err := e.EncodeElement(i.Error, errorStart); err != nil { - return err - } - } - - if err := e.EncodeToken(start.End()); err != nil { - return err - } - - return nil -} - -func (p *Input) Validate() bool { - if p.Required && p.Value == "" { - p.Error = fmt.Sprintf("%v is required", p.Name) - return false - } - - value_len := len(p.Value) - if p.MinLength > 0 && int(p.MinLength) > value_len { - p.Error = fmt.Sprintf("%v requires at least %v characters (currently %v characters)", p.Name, p.MinLength, value_len) - return false - } - - return true -} diff --git a/http/props/htmlx/link.go b/http/props/htmlx/link.go deleted file mode 100644 index a2f3cf7..0000000 --- a/http/props/htmlx/link.go +++ /dev/null @@ -1,24 +0,0 @@ -package htmlx - -import "encoding/xml" - -type Link struct { - Href string - Name string -} - -func (i Link) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - start.Name = xml.Name{Local: "A"} - - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Href"}, Value: i.Href}) - - if err := e.EncodeToken(start); err != nil { - return err - } - - if err := e.EncodeToken(xml.CharData(i.Name)); err != nil { - return err - } - - return e.EncodeToken(start.End()) -} diff --git a/http/props/htmlx/snapshot_test.go b/http/props/htmlx/snapshot_test.go deleted file mode 100644 index 01f470d..0000000 --- a/http/props/htmlx/snapshot_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package htmlx_test - -import ( - "testing" - - "lishwist/assert" - "lishwist/http/props/htmlx" -) - -type content struct { - Username htmlx.Input - Password htmlx.Input - RegisterLink htmlx.Link - Submit htmlx.Submit -} - -func TestSnapshotForm(t *testing.T) { - form := htmlx.Form[content]{ - Method: "POST", - Elements: content{ - Username: htmlx.Input{ - Label: "Username", - Name: "username", - Required: true, - }, - Password: htmlx.Input{ - Label: "Password", - Name: "password", - Type: "password", - Required: true, - }, - RegisterLink: htmlx.Link{ - Name: "Register", - Href: "/register", - }, - Submit: htmlx.Submit{ - Label: "Login", - }, - }, - } - - assert.SnapshotXml(t, form) - assert.SnapshotJson(t, form) -} - -func TestSnapshotLink(t *testing.T) { - link := htmlx.Link{ - Name: "Register", - Href: "/register", - } - - assert.SnapshotXml(t, link) - assert.SnapshotJson(t, link) -} diff --git a/http/props/htmlx/submit.go b/http/props/htmlx/submit.go deleted file mode 100644 index 6b0f609..0000000 --- a/http/props/htmlx/submit.go +++ /dev/null @@ -1,36 +0,0 @@ -package htmlx - -import ( - "encoding/xml" -) - -type Submit struct { - Label string - Name string `json:",omitempty"` - Value string `json:",omitempty"` -} - -func (i Submit) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - start.Name = xml.Name{Local: "Submit"} - - if i.Name != "" { - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Name"}, Value: i.Name}) - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Value"}, Value: i.Value}) - } else if i.Value != "" { - start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "Value"}, Value: i.Value}) - } - - if err := e.EncodeToken(start); err != nil { - return err - } - - if err := e.EncodeToken(xml.CharData(i.Label)); err != nil { - return err - } - - if err := e.EncodeToken(start.End()); err != nil { - return err - } - - return nil -} diff --git a/http/props/login.go b/http/props/login.go index 868f576..5742139 100644 --- a/http/props/login.go +++ b/http/props/login.go @@ -1,54 +1,42 @@ package props -import ( - "lishwist/http/props/htmlx" -) +import "github.com/Teajey/hmc" type Login struct { GeneralError string `json:",omitempty" xml:",omitempty"` SuccessfulRegistration bool `json:",omitempty" xml:",omitempty"` SuccessfulSetPassword bool `json:",omitempty" xml:",omitempty"` - Username htmlx.Input - Password htmlx.Input - RegisterLink htmlx.Link - Submit htmlx.Submit + Username hmc.Input + Password hmc.Input + RegisterLink hmc.Link } -func NewLogin(username, password string) *Login { - return &Login{ - Username: htmlx.Input{ +func NewLogin(username, password string) Login { + return Login{ + Username: hmc.Input{ Label: "Username", Name: "username", Required: true, Value: username, }, - Password: htmlx.Input{ + Password: hmc.Input{ Label: "Password", Name: "password", Type: "password", Required: true, Value: password, }, - RegisterLink: htmlx.Link{ - Name: "Register", - Href: "/register", - }, - Submit: htmlx.Submit{ - Label: "Login", + RegisterLink: hmc.Link{ + Label: "Register", + Href: "/register", }, } } func (p *Login) Validate() (valid bool) { - valid = true + _ = p.Username.Validate() - if !p.Username.Validate() { - valid = false - } + _ = p.Password.Validate() - if !p.Password.Validate() { - valid = false - } - - return + return p.Username.Error == "" && p.Password.Error == "" } diff --git a/http/props/register.go b/http/props/register.go index e8aaf9c..22a95df 100644 --- a/http/props/register.go +++ b/http/props/register.go @@ -2,22 +2,24 @@ package props import ( "html/template" - "lishwist/http/props/htmlx" + + "github.com/Teajey/hmc" + "lishwist/http/templates" ) type RegisterForm struct { - Username htmlx.Input - Password htmlx.Input - ConfirmPassword htmlx.Input - Submit htmlx.Submit + Username hmc.Input + Password hmc.Input + ConfirmPassword hmc.Input } type Register struct { + hmc.Namespace Nav templates.NavCollapse Error string `json:",omitempty" xml:",omitempty"` Warning template.HTML `json:",omitempty" xml:",omitempty"` - Form htmlx.Form[RegisterForm] + Form hmc.Form[RegisterForm] } func (p *RegisterForm) Validate() (valid bool) { @@ -28,42 +30,41 @@ func (p *RegisterForm) Validate() (valid bool) { valid = false } - if !p.Username.Validate() { - valid = false - } + _ = p.Username.Validate() - if !p.Password.Validate() { - valid = false - } + _ = p.Password.Validate() - if !p.ConfirmPassword.Validate() { - valid = false + _ = p.ConfirmPassword.Validate() + + if valid { + valid = p.Username.Error == "" && p.Password.Error == "" && p.ConfirmPassword.Error == "" } return } -func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register { - return &Register{ +func NewRegister(usernameVal, passwordVal, confirmPassVal string) Register { + return Register{ + Namespace: hmc.NS(), Nav: templates.NavCollapse{ - Links: []htmlx.Link{{ - Name: "Login", - Href: "/", + Links: []hmc.Link{{ + Label: "Login", + Href: "/", }}, }, Error: "", Warning: template.HTML(`

Your password will be stored in a safe, responsible manner; but don't trust my programming skills!

Maybe use a password here that you don't use for important things...

`), - Form: htmlx.Form[RegisterForm]{ + Form: hmc.Form[RegisterForm]{ Method: "POST", Elements: RegisterForm{ - Username: htmlx.Input{ + Username: hmc.Input{ Label: "Username", Name: "username", Required: true, MinLength: 4, Value: usernameVal, }, - Password: htmlx.Input{ + Password: hmc.Input{ Label: "Password", Type: "password", Name: "newPassword", @@ -71,16 +72,13 @@ func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register { MinLength: 5, Value: passwordVal, }, - ConfirmPassword: htmlx.Input{ + ConfirmPassword: hmc.Input{ Label: "Confirm password", Type: "password", Name: "confirmPassword", Required: true, Value: confirmPassVal, }, - Submit: htmlx.Submit{ - Label: "Register", - }, }, }, } diff --git a/http/routing/account.go b/http/routing/account.go index 2f0a3ef..3c07bb4 100644 --- a/http/routing/account.go +++ b/http/routing/account.go @@ -6,11 +6,11 @@ import ( lishwist "lishwist/core" "lishwist/http/props" - "lishwist/http/props/htmlx" "lishwist/http/response" "lishwist/http/session" "lishwist/http/templates" + "github.com/Teajey/hmc" "github.com/Teajey/rsvp" ) @@ -18,8 +18,8 @@ type AccountProps struct { Navbar templates.NavCollapse GeneralError string `json:",omitempty"` PasswordFromAdmin bool `json:",omitempty"` - Password htmlx.Input - ConfirmPassword htmlx.Input + Password hmc.Input + ConfirmPassword hmc.Input } func (p *AccountProps) Validate() (valid bool) { @@ -30,12 +30,12 @@ func (p *AccountProps) Validate() (valid bool) { valid = false } - if !p.Password.Validate() { - valid = false - } + _ = p.Password.Validate() - if !p.ConfirmPassword.Validate() { - valid = false + _ = p.ConfirmPassword.Validate() + + if valid { + valid = p.Password.Error == "" && p.ConfirmPassword.Error == "" } return @@ -44,18 +44,18 @@ func (p *AccountProps) Validate() (valid bool) { func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps { return &AccountProps{ Navbar: templates.NavCollapse{ - Links: []htmlx.Link{{Href: "/", Name: "Home"}}, + Links: []hmc.Link{{Href: "/", Label: "Home"}}, User: &templates.User{Name: username}, }, PasswordFromAdmin: passwordFromAdmin, - Password: htmlx.Input{ + Password: hmc.Input{ Type: "password", Name: "new_password", Required: true, MinLength: 5, Value: passwordVal, }, - ConfirmPassword: htmlx.Input{ + ConfirmPassword: hmc.Input{ Type: "password", Name: "confirm_password", Required: true, diff --git a/http/routing/login.go b/http/routing/login.go index cd3925c..3a2ce41 100644 --- a/http/routing/login.go +++ b/http/routing/login.go @@ -9,13 +9,18 @@ import ( lishwist "lishwist/core" "lishwist/http/props" - "lishwist/http/props/htmlx" "lishwist/http/response" "lishwist/http/session" + "github.com/Teajey/hmc" "github.com/Teajey/rsvp" ) +type LoginPage struct { + hmc.Namespace + Form hmc.Form[props.Login] +} + func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body { login := props.NewLogin("", "") @@ -32,7 +37,10 @@ func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword } - body := htmlx.Form[*props.Login]{Method: "POST", Elements: login} + body := LoginPage{ + Namespace: hmc.NS(), + Form: hmc.Form[props.Login]{Method: "POST", Elements: login}, + } return rsvp.Body{TemplateName: "login.gotmpl", Data: body} } @@ -63,7 +71,10 @@ func LoginWithCreds(username, password string, session *session.Session, referre if !valid { session.FlashSet(&login) log.Printf("Invalid login submission: %#v\n", login) - resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login} + resp.Data = LoginPage{ + Namespace: hmc.NS(), + Form: hmc.Form[props.Login]{Method: "POST", Elements: login}, + } return resp } @@ -75,15 +86,17 @@ func LoginWithCreds(username, password string, session *session.Session, referre login.GeneralError = "Username or password invalid. If you're having trouble accessing your account, you may want to consider asking the System Admin (Thomas) to reset your password" session.FlashSet(&login) log.Printf("Invalid credentials: %s: %#v\n", err, login) - resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login} - return resp default: login.GeneralError = "Something went wrong." session.FlashSet(&login) log.Printf("Login error: %s\n", err) - resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login} - return resp } + + resp.Data = LoginPage{ + Namespace: hmc.NS(), + Form: hmc.Form[props.Login]{Method: "POST", Elements: login}, + } + return resp } session.SetID("") diff --git a/http/templates/base.gotmpl b/http/templates/base.gotmpl index 872c14b..0675619 100644 --- a/http/templates/base.gotmpl +++ b/http/templates/base.gotmpl @@ -13,7 +13,7 @@ {{end}} {{define "hyperlink"}} -{{.Name}} +{{.Label}} {{end}} {{define "head"}} @@ -60,7 +60,7 @@ diff --git a/http/templates/login.gotmpl b/http/templates/login.gotmpl index 04427cb..66dbc13 100644 --- a/http/templates/login.gotmpl +++ b/http/templates/login.gotmpl @@ -37,7 +37,7 @@
{{template "hyperlink" .RegisterLink}}
- + diff --git a/http/templates/register.gotmpl b/http/templates/register.gotmpl index 48179f4..a081e5d 100644 --- a/http/templates/register.gotmpl +++ b/http/templates/register.gotmpl @@ -34,7 +34,7 @@ {{.ConfirmPassword.Label}} {{template "input" .ConfirmPassword}} - + {{end}} diff --git a/http/templates/templates.go b/http/templates/templates.go index 5814c45..e4cf8ed 100644 --- a/http/templates/templates.go +++ b/http/templates/templates.go @@ -2,7 +2,8 @@ package templates import ( "html/template" - "lishwist/http/props/htmlx" + + "github.com/Teajey/hmc" ) type AccountLink struct { @@ -22,16 +23,16 @@ type User struct { type NavCollapse struct { User *User `json:",omitempty"` AccountLink *AccountLink `json:",omitempty"` - Links []htmlx.Link + Links []hmc.Link } func DefaultNavCollapse() NavCollapse { - return NavCollapse{Links: []htmlx.Link{{Href: "/", Name: "Home"}}} + return NavCollapse{Links: []hmc.Link{{Href: "/", Label: "Home"}}} } func UserNavCollapse(username string, accountAlert bool) NavCollapse { return NavCollapse{ - Links: []htmlx.Link{{Href: "/", Name: "Home"}}, + Links: []hmc.Link{{Href: "/", Label: "Home"}}, User: &User{Name: username}, AccountLink: &AccountLink{Alert: accountAlert}, }