-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
149 lines (141 loc) · 4.84 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"github.com/minivera/go-lander"
"github.com/minivera/go-lander/context"
"github.com/minivera/go-lander/experimental/router"
"github.com/minivera/go-lander/nodes"
)
var appRouter = router.NewRouter()
func routingApp(_ context.Context, _ nodes.Props, _ nodes.Children) nodes.Child {
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("h1", nodes.Attributes{}, nodes.Children{
lander.Text("Sample routing app"),
}),
lander.Component(appRouter.Switch, router.SwitchProps{
Routes: router.RouteDefinitions{
{"/$", func(_ router.Match) nodes.Child {
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("h2", nodes.Attributes{}, nodes.Children{
lander.Text("Home page"),
}),
lander.Html("ul", nodes.Attributes{}, nodes.Children{
lander.Html("li", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/hello",
}, nodes.Children{
lander.Text("To /hello"),
}),
}),
lander.Html("li", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/app",
}, nodes.Children{
lander.Text("To /app"),
}),
}),
lander.Html("li", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/redirect",
}, nodes.Children{
lander.Text("To /redirect, which will send us back here"),
}),
}),
lander.Html("li", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/notfound",
}, nodes.Children{
lander.Text("To the 404 page"),
}),
}),
}),
})
}},
{"/hello$", func(_ router.Match) nodes.Child {
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("h2", nodes.Attributes{}, nodes.Children{
lander.Text("Hello, world!"),
}),
lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/",
}, nodes.Children{
lander.Text("Go back to Home"),
}),
}),
})
}},
{"/app.*", func(_ router.Match) nodes.Child {
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("h2", nodes.Attributes{}, nodes.Children{
lander.Text("Welcome to the app"),
}),
lander.Component(appRouter.Route, router.RouteProps{
Route: "/app/([a-zA-Z0-9]+)/(?P<subroute>[a-zA-Z0-9]+)",
Render: func(match router.Match) nodes.Child {
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("b", nodes.Attributes{}, nodes.Children{
lander.Text("Matched:"),
}),
lander.Html("span", nodes.Attributes{}, nodes.Children{
lander.Text(fmt.Sprintf("Pathname: %s", match.Pathname)),
}),
lander.Html("span", nodes.Attributes{}, nodes.Children{
lander.Text(fmt.Sprintf("Path %s", match.Params["0"])),
}),
lander.Html("span", nodes.Attributes{}, nodes.Children{
lander.Text(fmt.Sprintf("Subpath %s", match.Params["subroute"])),
}),
}).Style("display: flex; flex-direction: column; margin: 1rem; border: 1px solid black;")
},
}, nodes.Children{}),
lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/app/something/other",
}, nodes.Children{
lander.Text("Test the pattern matching"),
}),
}),
lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/",
}, nodes.Children{
lander.Text("Go back to Home"),
}),
}),
})
}},
{"/redirect$", func(_ router.Match) nodes.Child {
return lander.Component(appRouter.Redirect, router.RedirectProps{
To: "/",
}, nodes.Children{})
}},
{".*", func(match router.Match) nodes.Child {
return lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Html("h2", nodes.Attributes{}, nodes.Children{
lander.Text(fmt.Sprintf("404! `%s` was not found", match.Pathname)),
}),
lander.Html("div", nodes.Attributes{}, nodes.Children{
lander.Component(appRouter.Link, router.LinkProps{
To: "/",
}, nodes.Children{
lander.Text("Go back to Home"),
}),
}),
})
}},
},
}, nodes.Children{}),
}).Style("padding: 1rem;")
}
func main() {
c := make(chan bool)
_, err := lander.RenderInto(
lander.Component(appRouter.Provider, nodes.Props{}, nodes.Children{
lander.Component(routingApp, nodes.Props{}, nodes.Children{}),
}), "#app")
if err != nil {
fmt.Println(err)
}
<-c
}