-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.go
51 lines (44 loc) · 884 Bytes
/
api.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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type api struct {
}
func (a *api) Ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}
func (a *api) Auth(c *gin.Context) {
req := &AuthReq{}
err := c.ShouldBindQuery(req)
if err != nil {
return
}
auth := GetStorage().Get(req.Secret)
if auth == nil {
c.JSON(http.StatusForbidden, gin.H{
"message": "NOT_AUTHORIZED",
})
return
}
c.JSON(http.StatusOK, AuthResp{
TTL: SystemOpts.TTL,
Identity: SystemOpts.Identity,
IdentityURL: SystemOpts.IdentityURL,
Authorizations: auth,
})
}
func (a *api) Refresh(_ *gin.Context) {
GetStorage().Refresh()
}
// APIRoute 启动web服务
func APIRoute() *gin.Engine {
r := gin.Default()
rImpl := api{}
r.GET("/ping", rImpl.Ping)
r.GET("/auth", rImpl.Auth)
r.GET("/refresh", rImpl.Refresh)
return r
}