-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
48 lines (38 loc) · 981 Bytes
/
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
package main
import (
"embed"
"log"
"net/http"
"text/template"
"github.com/uptrace/bunrouter"
"github.com/uptrace/bunrouter/extra/reqlog"
)
//go:embed files
var filesFS embed.FS
func main() {
fileServer := http.FileServer(http.FS(filesFS))
router := bunrouter.New(
bunrouter.WithMiddleware(reqlog.NewMiddleware(
reqlog.FromEnv("BUNDEBUG"),
)),
)
router.GET("/", indexHandler)
router.GET("/files/*path", bunrouter.HTTPHandler(fileServer))
log.Println("listening on http://localhost:9999")
log.Println(http.ListenAndServe(":9999", router))
}
func indexHandler(w http.ResponseWriter, req bunrouter.Request) error {
return indexTemplate().Execute(w, nil)
}
var indexTmpl = `
<html>
<h1>Welcome</h1>
<ul>
<li><a href="/files/hello.txt">/files/hello.txt</a></li>
<li><a href="/files/world.txt">/files/world.txt</a></li>
</ul>
</html>
`
func indexTemplate() *template.Template {
return template.Must(template.New("index").Parse(indexTmpl))
}