-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (80 loc) · 1.93 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"embed"
"html/template"
"io/fs"
"net/http"
"net/mail"
"github.com/gin-gonic/gin"
)
var htmx_version = "latest"
var nunjucks_version = "3.2.4"
var bootstrap_version = "latest"
//go:embed templates/* img/*
var embed_fs embed.FS
type Contact struct {
FirstName string `json:"firstName" binding:"required"`
LastName string `json:"lastName" binding:"required"`
Email string `json:"email" binding:"required"`
}
type Email struct {
Email string `json:"email" binding:"required"`
}
var contacts []Contact
func main() {
router := gin.Default()
templ := template.Must(
template.New("").Delims("{[{", "}]}").ParseFS(embed_fs, "templates/*.tmpl"),
)
router.SetHTMLTemplate(templ)
router.GET("/", func(c *gin.Context) {
c.HTML(
http.StatusOK,
"index.html.tmpl",
gin.H{
"htmx_version": htmx_version,
"nunjucks_version": nunjucks_version,
"bootstrap_version": bootstrap_version,
},
)
})
var image_fs, _ = fs.Sub(embed_fs, "img")
router.StaticFS("/img", http.FS(image_fs))
router.POST("/contact/email", func(c *gin.Context) {
var email Email
if err := c.Bind(&email); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
if _, err := mail.ParseAddress(email.Email); err != nil {
c.JSON(
http.StatusOK,
gin.H{
"value": email.Email,
"message": "Please enter a valid email address.",
},
)
return
}
if email.Email != "test@test.com" {
c.JSON(
http.StatusOK,
gin.H{
"value": email.Email,
"message": "That email is already taken. Please enter another email.",
},
)
return
}
c.JSON(http.StatusOK, gin.H{"value": email.Email})
})
router.POST("/contact/", func(c *gin.Context) {
var contact Contact
if err := c.Bind(&contact); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
}
contacts = append(contacts, contact)
c.JSON(http.StatusOK, contact)
})
router.Run(":8080")
}