-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
77 lines (63 loc) · 1.71 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
74
75
76
77
package main
import (
"math/rand"
"os"
"strings"
"time"
_ "time/tzdata"
_ "bark-serverless/controller"
"bark-serverless/logger"
"bark-serverless/router"
apiWrapper "github.com/TMaize/scf-apigw-wrap"
"github.com/spf13/cast"
"github.com/tencentyun/scf-go-lib/cloudfunction"
"github.com/tencentyun/scf-go-lib/events"
"go.uber.org/zap"
)
// 程序运行模式
type runMode uint
const (
runModeTencentSCF runMode = iota // 腾讯云SCF - 用于SCF部署
runModeApiServer // API服务 - 用于本地调试
)
var _ = [...]runMode{runModeTencentSCF, runModeApiServer}
const (
ApiServerName = "api_path" // API网关名称
runModeKey = "mode" // 运行环境
portKey = "port" // API服务运行时的端口
)
func init() {
// 初始化时区
time.Local, _ = time.LoadLocation("Asia/Shanghai")
// 初始化随机数种子
rand.Seed(time.Now().UnixNano())
}
func main() {
switch mode := runMode(cast.ToUint(os.Getenv(runModeKey))); mode {
case runModeTencentSCF:
logger.GetGlobalLog().Info("SCF模式")
cloudfunction.Start(func(req events.APIGatewayRequest) (resp events.APIGatewayResponse, err error) {
// 转换一下路由路径
path := strings.TrimPrefix(req.Path, os.Getenv(ApiServerName))
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// 调用gin server
resp = apiWrapper.Wrap(req, path, router.SetupRouter())
return
})
case runModeApiServer:
logger.GetGlobalLog().Info("api服务模式")
if err := router.SetupRouter().Run(":" + func() string {
p := os.Getenv(portKey)
if p == "" {
return "8080"
}
return p
}()); err != nil {
logger.GetGlobalLog().Error("服务启动失败", zap.Error(err))
return
}
}
return
}