From 74dd3e08df6071c796f9028d56fe72a46ba53262 Mon Sep 17 00:00:00 2001 From: bdnugget <1001337108312v3@gmail.com> Date: Wed, 18 Sep 2024 21:11:50 +0200 Subject: [PATCH] initial commit --- README.md | 5 +- cmd/main.go | 158 +++++++++++++++++++++++++++++++++++++++++++++++ css/index.css | 14 +++++ go.mod | 19 ++++++ go.sum | 35 +++++++++++ images/bars.svg | 52 ++++++++++++++++ views/index.html | 83 +++++++++++++++++++++++++ 7 files changed, 364 insertions(+), 2 deletions(-) create mode 100644 cmd/main.go create mode 100644 css/index.css create mode 100644 go.mod create mode 100644 go.sum create mode 100644 images/bars.svg create mode 100644 views/index.html diff --git a/README.md b/README.md index 1e54c68..0cbaf75 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ -# boner-htmx +[Boner site and guestbook](https://boner.be) -Website and guestbook for boner.be \ No newline at end of file +Implementation of boner.be website and guestbook with the meme stack of Go and HTMX +(_)_)::::::::::::::D~~~~ diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..1d191f6 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,158 @@ +package main + +import ( + "html/template" + "io" + "strconv" + "time" + + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +type Templates struct { + templates *template.Template +} + +func (t *Templates) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + return t.templates.ExecuteTemplate(w, name, data) +} + +func newTemplate() *Templates { + return &Templates{ + templates: template.Must(template.ParseGlob("views/*.html")), + } +} + +var id = 0 + +type Contact struct { + Name string + Email string + Id int +} + +func newContact(name, email string) Contact { + id++ + return Contact{ + Name: name, + Email: email, + Id: id, + } +} + +type Contacts = []Contact + +type Data struct { + Contacts Contacts +} + +func (d *Data) indexOf(id int) int { + for i, contact := range d.Contacts { + if contact.Id == id { + return i + } + } + return -1 +} + +func (d *Data) hasEmail(email string) bool { + for _, contact := range d.Contacts { + if contact.Email == email { + return true + } + } + return false +} + +func newData() Data { + return Data{ + Contacts: []Contact{ + newContact("John Smith", "johndoe@me.com"), + newContact("Jane Doe", "janedoe@me.com"), + }, + } +} + +type FormData struct { + Values map[string]string + Errors map[string]string +} + +func newFormData() FormData { + return FormData{ + Values: make(map[string]string), + Errors: make(map[string]string), + } +} + +type Page struct { + Data Data + Form FormData +} + +func newPage() Page { + return Page{ + Data: newData(), + Form: newFormData(), + } +} + +func main() { + + e := echo.New() + e.Use(middleware.Logger()) + + page := newPage() + e.Renderer = newTemplate() + + e.Static("/css", "css") + e.Static("/images", "images") + + e.GET("/", func(c echo.Context) error { + return c.Render(200, "index", page) + }) + + e.POST("/contacts", func(c echo.Context) error { + name := c.FormValue("name") + email := c.FormValue("email") + + if page.Data.hasEmail(email) { + formData := newFormData() + formData.Values["name"] = name + formData.Values["email"] = email + formData.Errors["email"] = "Email already exists" + + return c.Render(422, "form", formData) + } + + contact := newContact(name, email) + page.Data.Contacts = append(page.Data.Contacts, contact) + + c.Render(200, "form", newFormData()) + return c.Render(200, "oob-contact", contact) + }) + + e.DELETE("/contacts/:id", func(c echo.Context) error { + + // TODO: remove this to make server faster LOL + time.Sleep(1 * time.Second) + + idStr := c.Param("id") + id, err := strconv.Atoi(idStr) + if err != nil { + return c.String(400, "Invalid ID") + } + + i := page.Data.indexOf(id) + if i == -1 { + return c.String(404, "Contact not found") + } + + page.Data.Contacts = append(page.Data.Contacts[:i], page.Data.Contacts[i+1:]...) + return c.NoContent(200) + + }) + + e.Logger.Fatal(e.Start(":1337")) +} diff --git a/css/index.css b/css/index.css new file mode 100644 index 0000000..ccab817 --- /dev/null +++ b/css/index.css @@ -0,0 +1,14 @@ +.htmx-indicator { + opacity:0; + transition: opacity 500ms ease-in; +} +.htmx-request .htmx-indicator{ + opacity:1 +} +.htmx-request.htmx-indicator{ + opacity:1 +} +.contact.htmx-swapping { + opacity: 0; + transition: opacity 500ms ease-in; +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..456c721 --- /dev/null +++ b/go.mod @@ -0,0 +1,19 @@ +module chemistry.software/htmx + +go 1.23.0 + +require github.com/labstack/echo/v4 v4.12.0 + +require ( + github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.22.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/time v0.5.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cadc1f3 --- /dev/null +++ b/go.sum @@ -0,0 +1,35 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= +github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= +github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0= +github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= +golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/images/bars.svg b/images/bars.svg new file mode 100644 index 0000000..7cb07e6 --- /dev/null +++ b/images/bars.svg @@ -0,0 +1,52 @@ + diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..4badfec --- /dev/null +++ b/views/index.html @@ -0,0 +1,83 @@ +{{ block "index" . }} + + +
+ + + +