Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (72 loc) · 2.38 KB
/
Copy pathmain.go
File metadata and controls
83 lines (72 loc) · 2.38 KB
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
// The apigraph controller: an interactive D3.js map of every registered route, mounted behind an admin guard.
//
// The graph shows paths, methods, handler names and editor links to each
// handler's source line. It is generated after the application builds, so the
// mount order relative to the routes it draws does not matter.
//
// Mount it behind an admin guard, and preferably in development builds only: it
// exposes the whole API surface plus local source file paths. The guard below
// compares the X-Admin-Token request header against the ADMIN_TOKEN
// environment variable ("dev" when unset).
//
// Learn: draw your route table as a page you can click through, and keep it behind a token.
// Before: ../multi-app
//
// Run:
//
// go run main.go
//
// The page loads d3.js from the network, so open it in a browser with the
// token header set, or read the HTML with curl.
//
// Try:
//
// curl -i http://localhost:8080/admin/apigraph # 401, no token
// curl -H "X-Admin-Token: dev" http://localhost:8080/admin/apigraph # 200, the graph page
//
// Next: ../sitemap
package main
import (
"os"
"github.com/kataras/iris/v14"
"github.com/kataras/iris/v14/controller/apigraph"
"github.com/kataras/iris/v14/rest/errors"
)
func main() {
app := iris.New()
// Some routes to draw.
app.Get("/", text("index"))
users := app.Router("/users")
users.Get("/", text("list users"))
users.Post("/", text("create user"))
users.Get("/{id:uint64}", text("get user"))
users.Put("/{id:uint64}", text("update user"))
users.Get("/{id:uint64}/posts", text("list user posts"))
// Themes: "light" (default) or "dark"; register your own with
// apigraph.RegisterTheme.
admin := app.Router("/admin", requireAdminToken(adminToken()))
admin.Router("/apigraph", apigraph.New(apigraph.Options{Theme: "dark"}))
app.Listen(":8080")
}
func adminToken() string {
if token := os.Getenv("ADMIN_TOKEN"); token != "" {
return token
}
return "dev"
}
func requireAdminToken(token string) iris.Handler {
return func(ctx iris.Context) {
if ctx.Header("X-Admin-Token") != token {
// The error code carries the 401 and stops the chain; the
// renderer negotiates JSON or an HTML page from Accept.
errors.Unauthenticated.Message(ctx, "missing or invalid X-Admin-Token header")
return
}
ctx.Next()
}
}
func text(s string) iris.Handler {
return func(ctx iris.Context) {
ctx.WriteString(s)
}
}