-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (44 loc) · 1.18 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
package main
import (
"errors"
"log"
"net/http"
g "github.com/maragudk/gomponents"
c "github.com/maragudk/gomponents/components"
. "github.com/maragudk/gomponents/html"
)
func main() {
http.Handle("/", createHandler(indexPage()))
if err := http.ListenAndServe("localhost:8080", nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Println("Error:", err)
}
}
func createHandler(title string, body g.Node) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_ = Page(title, r.URL.Path, body).Render(w);
}
}
func indexPage() (string, g.Node) {
return "Welcome!", Div(
H1(g.Text("Welcome to the homepage!")),
P(g.Text("This is the homepage. Please enjoy your stay.")),
)
}
func Page(title, path string, body g.Node) g.Node {
return c.HTML5(c.HTML5Props{
Title: title,
Language: "en",
Head: []g.Node{
Link(g.Attr("rel", "stylesheet"), g.Attr("href", "https://unpkg.com/mvp.css")),
// Script(Src("https://cdn.tailwindcss.com?plugins=typography")),
},
Body: []g.Node{
Container(
body,
),
},
})
}
func Container(children ...g.Node) g.Node {
return Div(Class("max-w-7xl mx-auto px-2 sm:px-6 lg:px-8"), g.Group(children))
}