-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (86 loc) · 1.75 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
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"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 Make struct {
Name string
Models []string
}
var makes []Make
func init_db() {
makes = []Make{
Make{
Name: "Audi",
Models: []string{"A1", "A4", "A6"},
},
Make{
Name: "BMW",
Models: []string{"325i", "325ix", "X5"},
},
Make{
Name: "Toyota",
Models: []string{"Landcruiser", "Tacoma", "Yaris"},
},
}
}
func find_make(name string) *Make {
var found *Make
for _, maker := range makes {
if maker.Name == name {
found = &maker
}
}
return found
}
func main() {
init_db()
fmt.Println(makes)
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.GET("/makes", func(c *gin.Context) {
var make_names []string
for _, maker := range makes {
make_names = append(make_names, maker.Name)
}
c.JSON(http.StatusOK, make_names)
})
router.GET("/models", func(c *gin.Context) {
make_name := c.Query("make")
found := find_make(make_name)
if found == nil {
c.AbortWithStatusJSON(
http.StatusNotFound,
gin.H{"reason": "wrong make"},
)
return
}
c.JSON(http.StatusOK, found.Models)
})
router.Run(":8080")
}