-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
106 lines (91 loc) · 2.05 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
97
98
99
100
101
102
103
104
105
106
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
var htmx_version = "latest"
var htmx_ext_version = "latest"
var nunjucks_version = "3.2.4"
var bootstrap_version = "latest"
//go:embed templates/* img/*
var embed_fs embed.FS
type Contact struct {
ID int `json:"id" binding:"required"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
}
var contacts []Contact
func init_db() {
contacts = []Contact{
Contact{
ID: 0,
FirstName: "Joe",
LastName: "Smith",
Email: "[email protected]",
},
Contact{
ID: 1,
FirstName: "Angie",
LastName: "MacDowell",
Email: "[email protected]",
},
Contact{
ID: 2,
FirstName: "Fuqua",
LastName: "Tarkenton",
Email: "[email protected]",
},
Contact{
ID: 3,
FirstName: "Kim",
LastName: "Yee",
Email: "[email protected]",
},
}
}
func main() {
init_db()
fmt.Println(contacts)
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,
"htmx_ext_version": htmx_ext_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.GET("/contacts/", func(c *gin.Context) {
search := c.Query("search")
if search == "" {
c.JSON(http.StatusOK, contacts)
return
}
var selected_contacts []Contact
for _, contact := range contacts {
if strings.Contains(contact.FirstName, search) ||
strings.Contains(contact.LastName, search) ||
strings.Contains(contact.Email, search) {
selected_contacts = append(selected_contacts, contact)
}
}
c.JSON(http.StatusOK, selected_contacts)
})
router.Run(":8080")
}