Replace htmlx with hmc

This commit is contained in:
Teajey 2026-02-14 12:29:47 +09:00
parent f09d61509f
commit 7334de957a
Signed by: Teajey
GPG Key ID: 970E790FE834A713
20 changed files with 86 additions and 328 deletions

View File

@ -1,4 +1,4 @@
go 1.24.5 go 1.25.5
use ( use (
./assert ./assert

View File

@ -1,8 +1,6 @@
module lishwist/http module lishwist/http
go 1.23.3 go 1.25.5
toolchain go1.24.5
require ( require (
github.com/Teajey/rsvp v0.19.0-rc.2 github.com/Teajey/rsvp v0.19.0-rc.2
@ -10,7 +8,10 @@ require (
golang.org/x/crypto v0.39.0 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 ( require (
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect

View File

@ -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 h1:h4V7c3uGlSKajkWKt3fldFV+XTMEI8A4o05PMY8xv9A=
github.com/Teajey/rsvp v0.19.0-rc.2/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q= 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= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

View File

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

View File

@ -1,8 +0,0 @@
<Form Method="POST">
<content>
<Input Label="Username" Name="username" Required="true" Value=""></Input>
<Input Label="Password" Type="password" Name="password" Required="true" Value=""></Input>
<A Href="/register">Register</A>
<Submit>Login</Submit>
</content>
</Form>

View File

@ -1,4 +0,0 @@
{
"Href": "/register",
"Name": "Register"
}

View File

@ -1 +0,0 @@
<A Href="/register">Register</A>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,54 +1,42 @@
package props package props
import ( import "github.com/Teajey/hmc"
"lishwist/http/props/htmlx"
)
type Login struct { type Login struct {
GeneralError string `json:",omitempty" xml:",omitempty"` GeneralError string `json:",omitempty" xml:",omitempty"`
SuccessfulRegistration bool `json:",omitempty" xml:",omitempty"` SuccessfulRegistration bool `json:",omitempty" xml:",omitempty"`
SuccessfulSetPassword bool `json:",omitempty" xml:",omitempty"` SuccessfulSetPassword bool `json:",omitempty" xml:",omitempty"`
Username htmlx.Input Username hmc.Input
Password htmlx.Input Password hmc.Input
RegisterLink htmlx.Link RegisterLink hmc.Link
Submit htmlx.Submit
} }
func NewLogin(username, password string) *Login { func NewLogin(username, password string) Login {
return &Login{ return Login{
Username: htmlx.Input{ Username: hmc.Input{
Label: "Username", Label: "Username",
Name: "username", Name: "username",
Required: true, Required: true,
Value: username, Value: username,
}, },
Password: htmlx.Input{ Password: hmc.Input{
Label: "Password", Label: "Password",
Name: "password", Name: "password",
Type: "password", Type: "password",
Required: true, Required: true,
Value: password, Value: password,
}, },
RegisterLink: htmlx.Link{ RegisterLink: hmc.Link{
Name: "Register", Label: "Register",
Href: "/register", Href: "/register",
}, },
Submit: htmlx.Submit{
Label: "Login",
},
} }
} }
func (p *Login) Validate() (valid bool) { func (p *Login) Validate() (valid bool) {
valid = true _ = p.Username.Validate()
if !p.Username.Validate() { _ = p.Password.Validate()
valid = false
}
if !p.Password.Validate() { return p.Username.Error == "" && p.Password.Error == ""
valid = false
}
return
} }

View File

@ -2,22 +2,24 @@ package props
import ( import (
"html/template" "html/template"
"lishwist/http/props/htmlx"
"github.com/Teajey/hmc"
"lishwist/http/templates" "lishwist/http/templates"
) )
type RegisterForm struct { type RegisterForm struct {
Username htmlx.Input Username hmc.Input
Password htmlx.Input Password hmc.Input
ConfirmPassword htmlx.Input ConfirmPassword hmc.Input
Submit htmlx.Submit
} }
type Register struct { type Register struct {
hmc.Namespace
Nav templates.NavCollapse Nav templates.NavCollapse
Error string `json:",omitempty" xml:",omitempty"` Error string `json:",omitempty" xml:",omitempty"`
Warning template.HTML `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) { func (p *RegisterForm) Validate() (valid bool) {
@ -28,42 +30,41 @@ func (p *RegisterForm) Validate() (valid bool) {
valid = false valid = false
} }
if !p.Username.Validate() { _ = p.Username.Validate()
valid = false
}
if !p.Password.Validate() { _ = p.Password.Validate()
valid = false
}
if !p.ConfirmPassword.Validate() { _ = p.ConfirmPassword.Validate()
valid = false
if valid {
valid = p.Username.Error == "" && p.Password.Error == "" && p.ConfirmPassword.Error == ""
} }
return return
} }
func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register { func NewRegister(usernameVal, passwordVal, confirmPassVal string) Register {
return &Register{ return Register{
Namespace: hmc.NS(),
Nav: templates.NavCollapse{ Nav: templates.NavCollapse{
Links: []htmlx.Link{{ Links: []hmc.Link{{
Name: "Login", Label: "Login",
Href: "/", Href: "/",
}}, }},
}, },
Error: "", Error: "",
Warning: template.HTML(`<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p><p class="mb-0">Maybe use a password here that you don't use for important things...</p>`), Warning: template.HTML(`<p>Your password will be stored in a safe, responsible manner; but don't trust my programming skills!</p><p class="mb-0">Maybe use a password here that you don't use for important things...</p>`),
Form: htmlx.Form[RegisterForm]{ Form: hmc.Form[RegisterForm]{
Method: "POST", Method: "POST",
Elements: RegisterForm{ Elements: RegisterForm{
Username: htmlx.Input{ Username: hmc.Input{
Label: "Username", Label: "Username",
Name: "username", Name: "username",
Required: true, Required: true,
MinLength: 4, MinLength: 4,
Value: usernameVal, Value: usernameVal,
}, },
Password: htmlx.Input{ Password: hmc.Input{
Label: "Password", Label: "Password",
Type: "password", Type: "password",
Name: "newPassword", Name: "newPassword",
@ -71,16 +72,13 @@ func NewRegister(usernameVal, passwordVal, confirmPassVal string) *Register {
MinLength: 5, MinLength: 5,
Value: passwordVal, Value: passwordVal,
}, },
ConfirmPassword: htmlx.Input{ ConfirmPassword: hmc.Input{
Label: "Confirm password", Label: "Confirm password",
Type: "password", Type: "password",
Name: "confirmPassword", Name: "confirmPassword",
Required: true, Required: true,
Value: confirmPassVal, Value: confirmPassVal,
}, },
Submit: htmlx.Submit{
Label: "Register",
},
}, },
}, },
} }

View File

@ -6,11 +6,11 @@ import (
lishwist "lishwist/core" lishwist "lishwist/core"
"lishwist/http/props" "lishwist/http/props"
"lishwist/http/props/htmlx"
"lishwist/http/response" "lishwist/http/response"
"lishwist/http/session" "lishwist/http/session"
"lishwist/http/templates" "lishwist/http/templates"
"github.com/Teajey/hmc"
"github.com/Teajey/rsvp" "github.com/Teajey/rsvp"
) )
@ -18,8 +18,8 @@ type AccountProps struct {
Navbar templates.NavCollapse Navbar templates.NavCollapse
GeneralError string `json:",omitempty"` GeneralError string `json:",omitempty"`
PasswordFromAdmin bool `json:",omitempty"` PasswordFromAdmin bool `json:",omitempty"`
Password htmlx.Input Password hmc.Input
ConfirmPassword htmlx.Input ConfirmPassword hmc.Input
} }
func (p *AccountProps) Validate() (valid bool) { func (p *AccountProps) Validate() (valid bool) {
@ -30,12 +30,12 @@ func (p *AccountProps) Validate() (valid bool) {
valid = false valid = false
} }
if !p.Password.Validate() { _ = p.Password.Validate()
valid = false
}
if !p.ConfirmPassword.Validate() { _ = p.ConfirmPassword.Validate()
valid = false
if valid {
valid = p.Password.Error == "" && p.ConfirmPassword.Error == ""
} }
return return
@ -44,18 +44,18 @@ func (p *AccountProps) Validate() (valid bool) {
func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps { func NewAccountProps(username string, passwordFromAdmin bool, passwordVal, confirmPassVal string) *AccountProps {
return &AccountProps{ return &AccountProps{
Navbar: templates.NavCollapse{ Navbar: templates.NavCollapse{
Links: []htmlx.Link{{Href: "/", Name: "Home"}}, Links: []hmc.Link{{Href: "/", Label: "Home"}},
User: &templates.User{Name: username}, User: &templates.User{Name: username},
}, },
PasswordFromAdmin: passwordFromAdmin, PasswordFromAdmin: passwordFromAdmin,
Password: htmlx.Input{ Password: hmc.Input{
Type: "password", Type: "password",
Name: "new_password", Name: "new_password",
Required: true, Required: true,
MinLength: 5, MinLength: 5,
Value: passwordVal, Value: passwordVal,
}, },
ConfirmPassword: htmlx.Input{ ConfirmPassword: hmc.Input{
Type: "password", Type: "password",
Name: "confirm_password", Name: "confirm_password",
Required: true, Required: true,

View File

@ -9,13 +9,18 @@ import (
lishwist "lishwist/core" lishwist "lishwist/core"
"lishwist/http/props" "lishwist/http/props"
"lishwist/http/props/htmlx"
"lishwist/http/response" "lishwist/http/response"
"lishwist/http/session" "lishwist/http/session"
"github.com/Teajey/hmc"
"github.com/Teajey/rsvp" "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 { func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body {
login := props.NewLogin("", "") login := props.NewLogin("", "")
@ -32,7 +37,10 @@ func Login(s *session.Session, w rsvp.ResponseWriter, r *http.Request) rsvp.Body
login.SuccessfulSetPassword = flashProps.SuccessfulSetPassword 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} return rsvp.Body{TemplateName: "login.gotmpl", Data: body}
} }
@ -63,7 +71,10 @@ func LoginWithCreds(username, password string, session *session.Session, referre
if !valid { if !valid {
session.FlashSet(&login) session.FlashSet(&login)
log.Printf("Invalid login submission: %#v\n", 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 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" 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) session.FlashSet(&login)
log.Printf("Invalid credentials: %s: %#v\n", err, login) log.Printf("Invalid credentials: %s: %#v\n", err, login)
resp.Data = htmlx.Form[*props.Login]{Method: "POST", Elements: login}
return resp
default: default:
login.GeneralError = "Something went wrong." login.GeneralError = "Something went wrong."
session.FlashSet(&login) session.FlashSet(&login)
log.Printf("Login error: %s\n", err) 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("") session.SetID("")

View File

@ -13,7 +13,7 @@
{{end}} {{end}}
{{define "hyperlink"}} {{define "hyperlink"}}
<a href="{{.Href}}">{{.Name}}</a> <a href="{{.Href}}">{{.Label}}</a>
{{end}} {{end}}
{{define "head"}} {{define "head"}}
@ -60,7 +60,7 @@
<ul class="navbar-nav"> <ul class="navbar-nav">
{{range .}} {{range .}}
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{{.Href}}">{{.Name}}</a> <a class="nav-link" href="{{.Href}}">{{.Label}}</a>
</li> </li>
{{end}} {{end}}
</ul> </ul>

View File

@ -37,7 +37,7 @@
<div class="mb-3"> <div class="mb-3">
{{template "hyperlink" .RegisterLink}} {{template "hyperlink" .RegisterLink}}
</div> </div>
<input class="btn btn-primary" type="submit" value="{{.Submit.Label}}"> <button class="btn btn-primary" type="submit">Login</button>
</div> </div>
</form> </form>
</div> </div>

View File

@ -34,7 +34,7 @@
{{.ConfirmPassword.Label}} {{.ConfirmPassword.Label}}
{{template "input" .ConfirmPassword}} {{template "input" .ConfirmPassword}}
</label> </label>
<input class="btn btn-primary" type="submit" value="{{.Submit.Label}}"> <button class="btn btn-primary" type="submit">Register</button>
</div> </div>
{{end}} {{end}}
</form> </form>

View File

@ -2,7 +2,8 @@ package templates
import ( import (
"html/template" "html/template"
"lishwist/http/props/htmlx"
"github.com/Teajey/hmc"
) )
type AccountLink struct { type AccountLink struct {
@ -22,16 +23,16 @@ type User struct {
type NavCollapse struct { type NavCollapse struct {
User *User `json:",omitempty"` User *User `json:",omitempty"`
AccountLink *AccountLink `json:",omitempty"` AccountLink *AccountLink `json:",omitempty"`
Links []htmlx.Link Links []hmc.Link
} }
func DefaultNavCollapse() NavCollapse { 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 { func UserNavCollapse(username string, accountAlert bool) NavCollapse {
return NavCollapse{ return NavCollapse{
Links: []htmlx.Link{{Href: "/", Name: "Home"}}, Links: []hmc.Link{{Href: "/", Label: "Home"}},
User: &User{Name: username}, User: &User{Name: username},
AccountLink: &AccountLink{Alert: accountAlert}, AccountLink: &AccountLink{Alert: accountAlert},
} }