5
5
package route
6
6
7
7
import (
8
+ "fmt"
9
+ "log"
8
10
"net/http"
9
11
"net/http/pprof"
12
+ "strings"
10
13
11
14
"github.com/gin-gonic/gin"
12
- "github.com/sirupsen/logrus"
13
15
"golang.design/x/ssaplayground/src/config"
14
16
)
15
17
16
- // Register routers
18
+ // Register routers for the ssa service.
17
19
func Register () * gin.Engine {
18
- r := & Router {
19
- Engine : gin .Default (),
20
- }
20
+ r := & Router {Engine : gin .Default ()}
21
21
r .SetupAPI ()
22
22
r .SetupApp ()
23
- if config .Get ().Mode != "debug" {
23
+ if config .Get ().Mode != gin . DebugMode {
24
24
return r .Engine
25
25
}
26
26
r .SetupProfile ()
27
27
return r .Engine
28
28
}
29
29
30
- // Router .. .
30
+ // Router is a router engine .
31
31
type Router struct {
32
32
Engine * gin.Engine
33
33
}
34
34
35
- // SetupAPI .. .
35
+ // SetupAPI serves the API endpoints of the gossa service .
36
36
func (r * Router ) SetupAPI () {
37
37
v1 := r .Engine .Group ("/gossa/api/v1" )
38
38
{
@@ -41,46 +41,54 @@ func (r *Router) SetupAPI() {
41
41
}
42
42
}
43
43
44
- // SetupApp .. .
44
+ // SetupApp serves the static website of Go SSA Playground .
45
45
func (r * Router ) SetupApp () {
46
46
r .Engine .Use (static ("/gossa" ))
47
- logrus . Infof ("GoSSAWeb is on: http://%s, static: %s" , config .Get ().Addr , config .Get ().Static )
47
+ log . Printf ("GoSSAWeb is on: http://%s, static: %s" , config .Get ().Addr , config .Get ().Static )
48
48
}
49
49
50
- // SetupProfile profiles the standard HandlerFuncs from the net/http/pprof package with
50
+ // SetupProfile the standard HandlerFuncs from the net/http/pprof package with
51
51
// the provided gin.Engine. prefixOptions is a optional. If not prefixOptions,
52
52
// the default path prefix is used, otherwise first prefixOptions will be path prefix.
53
53
//
54
54
// Basic Usage:
55
55
//
56
56
// - use the pprof tool to look at the heap profile:
57
- // go tool pprof http://localhost:9999 /debug/pprof/heap
57
+ // go tool pprof localhost:8080/midgard/api/v1 /debug/pprof/heap
58
58
// - look at a 30-second CPU profile:
59
- // go tool pprof http://localhost:9999 /debug/pprof/profile
59
+ // go tool pprof localhost:8080/midgard/api/v1 /debug/pprof/profile
60
60
// - look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate:
61
- // go tool pprof http://localhost:9999 /debug/pprof/block
61
+ // go tool pprof localhost:8080/midgard/api/v1 /debug/pprof/block
62
62
// - collect a 5-second execution trace:
63
- // wget http://localhost:9999 /debug/pprof/trace?seconds=5
63
+ // go tool pprof localhost:8080/midgard/api/v1 /debug/pprof/trace?seconds=5
64
64
//
65
65
func (r * Router ) SetupProfile () {
66
66
pprofHandler := func (h http.HandlerFunc ) gin.HandlerFunc {
67
67
handler := http .HandlerFunc (h )
68
68
return func (c * gin.Context ) {
69
+
70
+ fmt .Println (c .Request .Host )
71
+ if ! strings .Contains (c .Request .Host , "localhost" ) {
72
+ c .AbortWithStatus (http .StatusUnauthorized )
73
+ return
74
+ }
75
+
69
76
handler .ServeHTTP (c .Writer , c .Request )
70
77
}
71
78
}
72
- prefixRouter := r .Engine .Group ("/debug/pprof" )
79
+ rr := r .Engine .Group ("/debug/pprof" )
73
80
{
74
- prefixRouter .GET ("/" , pprofHandler (pprof .Index ))
75
- prefixRouter .GET ("/cmdline" , pprofHandler (pprof .Cmdline ))
76
- prefixRouter .GET ("/profile" , pprofHandler (pprof .Profile ))
77
- prefixRouter .POST ("/symbol" , pprofHandler (pprof .Symbol ))
78
- prefixRouter .GET ("/symbol" , pprofHandler (pprof .Symbol ))
79
- prefixRouter .GET ("/trace" , pprofHandler (pprof .Trace ))
80
- prefixRouter .GET ("/block" , pprofHandler (pprof .Handler ("block" ).ServeHTTP ))
81
- prefixRouter .GET ("/goroutine" , pprofHandler (pprof .Handler ("goroutine" ).ServeHTTP ))
82
- prefixRouter .GET ("/heap" , pprofHandler (pprof .Handler ("heap" ).ServeHTTP ))
83
- prefixRouter .GET ("/mutex" , pprofHandler (pprof .Handler ("mutex" ).ServeHTTP ))
84
- prefixRouter .GET ("/threadcreate" , pprofHandler (pprof .Handler ("threadcreate" ).ServeHTTP ))
81
+ rr .GET ("/" , pprofHandler (pprof .Index ))
82
+ rr .GET ("/cmdline" , pprofHandler (pprof .Cmdline ))
83
+ rr .GET ("/profile" , pprofHandler (pprof .Profile ))
84
+ rr .POST ("/symbol" , pprofHandler (pprof .Symbol ))
85
+ rr .GET ("/symbol" , pprofHandler (pprof .Symbol ))
86
+ rr .GET ("/trace" , pprofHandler (pprof .Trace ))
87
+ rr .GET ("/allocs" , pprofHandler (pprof .Handler ("allocs" ).ServeHTTP ))
88
+ rr .GET ("/block" , pprofHandler (pprof .Handler ("block" ).ServeHTTP ))
89
+ rr .GET ("/goroutine" , pprofHandler (pprof .Handler ("goroutine" ).ServeHTTP ))
90
+ rr .GET ("/heap" , pprofHandler (pprof .Handler ("heap" ).ServeHTTP ))
91
+ rr .GET ("/mutex" , pprofHandler (pprof .Handler ("mutex" ).ServeHTTP ))
92
+ rr .GET ("/threadcreate" , pprofHandler (pprof .Handler ("threadcreate" ).ServeHTTP ))
85
93
}
86
94
}
0 commit comments