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 (
./assert

View File

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

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/go.mod h1:z0L20VphVg+Ec2+hnpLFTG2MZTrWYFprav1kpxDba0Q=
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
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 == ""
}

View File

@ -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(`<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",
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",
},
},
},
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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