/
main.go
73 lines (60 loc) · 1.57 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
package main
import (
"crypto/subtle"
"html/template"
"log"
"net/http"
"github.com/uptrace/bunrouter"
"github.com/uptrace/bunrouter/extra/basicauth"
"github.com/uptrace/bunrouter/extra/reqlog"
)
func main() {
router := bunrouter.New(
bunrouter.Use(reqlog.NewMiddleware(
reqlog.FromEnv("BUNDEBUG"),
)),
)
authMiddleware := basicauth.NewMiddleware(authUser, basicauth.WithRealm("test:test"))
router.GET("/", indexHandler)
router.Use(authMiddleware).
WithGroup("/restricted", func(g *bunrouter.Group) {
g.GET("", debugHandler)
})
router.WithGroup("/public", func(g *bunrouter.Group) {
g.GET("", debugHandler)
})
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)
}
func debugHandler(w http.ResponseWriter, req bunrouter.Request) error {
return bunrouter.JSON(w, bunrouter.H{
"route": req.Route(),
"params": req.Params().Map(),
})
}
func authUser(req bunrouter.Request) (bool, error) {
user, pass, ok := req.BasicAuth()
if !ok {
return false, nil
}
if subtle.ConstantTimeCompare([]byte(user), []byte("test")) == 1 &&
subtle.ConstantTimeCompare([]byte(pass), []byte("test")) == 1 {
return true, nil
}
return false, nil
}
var indexTmpl = `
<html>
<h1>Welcome</h1>
<ul>
<li><a href="/restricted">/restricted</a></li>
<li><a href="/public">/public</a></li>
</ul>
</html>
`
func indexTemplate() *template.Template {
return template.Must(template.New("index").Parse(indexTmpl))
}