2022-09-22 00:41:06 -07:00
|
|
|
//go:build go1.18
|
|
|
|
// +build go1.18
|
|
|
|
|
2020-09-21 07:30:29 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
g "github.com/maragudk/gomponents"
|
2020-12-10 04:00:23 -08:00
|
|
|
c "github.com/maragudk/gomponents/components"
|
2020-12-10 05:27:10 -08:00
|
|
|
. "github.com/maragudk/gomponents/html"
|
2020-09-21 07:30:29 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-10-28 08:59:04 -07:00
|
|
|
_ = http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
|
2020-09-21 07:30:29 -07:00
|
|
|
}
|
|
|
|
|
2020-10-28 08:59:04 -07:00
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
2020-12-10 05:27:10 -08:00
|
|
|
_ = Page(props{
|
2020-10-28 08:59:04 -07:00
|
|
|
title: r.URL.Path,
|
|
|
|
path: r.URL.Path,
|
2020-12-10 05:27:10 -08:00
|
|
|
}).Render(w)
|
2020-09-21 07:30:29 -07:00
|
|
|
}
|
|
|
|
|
2020-11-02 01:03:05 -08:00
|
|
|
type props struct {
|
2020-09-21 07:30:29 -07:00
|
|
|
title string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2020-12-10 05:27:10 -08:00
|
|
|
// Page is a whole document to output.
|
|
|
|
func Page(p props) g.Node {
|
|
|
|
return c.HTML5(c.HTML5Props{
|
|
|
|
Title: p.title,
|
|
|
|
Language: "en",
|
|
|
|
Head: []g.Node{
|
|
|
|
StyleEl(Type("text/css"),
|
|
|
|
g.Raw("html { font-family: sans-serif; }"),
|
|
|
|
g.Raw("ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; }"),
|
|
|
|
g.Raw("ul li { display: block; padding: 8px; float: left; }"),
|
|
|
|
g.Raw(".is-active { font-weight: bold; }"),
|
2020-09-21 07:30:29 -07:00
|
|
|
),
|
2020-12-10 05:27:10 -08:00
|
|
|
},
|
|
|
|
Body: []g.Node{
|
|
|
|
Navbar(p.path, []PageLink{
|
|
|
|
{Path: "/foo", Name: "Foo"},
|
|
|
|
{Path: "/bar", Name: "Bar"},
|
|
|
|
}),
|
2021-05-05 00:03:16 -07:00
|
|
|
H1(g.Text(p.title)),
|
2020-12-10 05:27:10 -08:00
|
|
|
P(g.Textf("Welcome to the page at %v.", p.path)),
|
|
|
|
},
|
|
|
|
})
|
2020-09-21 07:30:29 -07:00
|
|
|
}
|
|
|
|
|
2020-12-10 05:27:10 -08:00
|
|
|
type PageLink struct {
|
|
|
|
Path string
|
|
|
|
Name string
|
2020-09-21 07:30:29 -07:00
|
|
|
}
|
|
|
|
|
2020-12-10 05:27:10 -08:00
|
|
|
func Navbar(currentPath string, links []PageLink) g.Node {
|
|
|
|
return Div(
|
|
|
|
Ul(
|
|
|
|
NavbarLink("/", "Home", currentPath),
|
|
|
|
|
2022-09-22 00:41:06 -07:00
|
|
|
g.Group(g.Map(links, func(pl PageLink) g.Node {
|
|
|
|
return NavbarLink(pl.Path, pl.Name, currentPath)
|
2020-12-10 05:27:10 -08:00
|
|
|
})),
|
|
|
|
),
|
|
|
|
|
|
|
|
Hr(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NavbarLink(href, name, currentPath string) g.Node {
|
|
|
|
return Li(A(Href(href), c.Classes{"is-active": currentPath == href}, g.Text(name)))
|
2020-09-21 07:30:29 -07:00
|
|
|
}
|