-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkTag.go
64 lines (54 loc) · 1 KB
/
checkTag.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
package checktag
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
const (
DefaultRunAddr = ":38080"
DefaultName = "/checktag"
DefaultApiPrefix = "/path"
)
type Cfg struct {
RunAddr string
APIBase string
ApiPrefix string
}
var nowCfg *Cfg
func initDefaultCfg() *Cfg {
cfg := Cfg{
RunAddr: DefaultRunAddr,
APIBase: DefaultName,
ApiPrefix: DefaultApiPrefix,
}
return &cfg
}
func checkCfg(cfg *Cfg) {
defaultCfg := initDefaultCfg()
if cfg.RunAddr == "" {
cfg.RunAddr = defaultCfg.RunAddr
}
if cfg.APIBase == "" {
cfg.APIBase = defaultCfg.APIBase
}
if cfg.ApiPrefix == "" {
cfg.ApiPrefix = defaultCfg.ApiPrefix
}
nowCfg = cfg
}
func Register(r *gin.Engine, cfg *Cfg) error {
checkCfg(cfg)
mGroup := r.Group(cfg.APIBase)
{
mGroup.GET(cfg.ApiPrefix, apiPrefix)
fmt.Printf("register api at http://127.0.0.1%v\n", cfg.RunAddr)
}
return nil
}
func NowCfg() *Cfg {
return nowCfg
}
func apiPrefix(c *gin.Context) {
message := "OK"
c.String(http.StatusOK, message)
}