-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
executable file
·131 lines (117 loc) · 3.81 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2020 The poly network Authors
* This file is part of The poly network library.
*
* The poly network is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The poly network is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with The poly network . If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"fmt"
"os"
"poly-bridge/basedef"
"poly-bridge/cacheRedis"
"poly-bridge/common"
"poly-bridge/conf"
"poly-bridge/explorer"
"poly-bridge/http"
"poly-bridge/nft_http"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
"github.com/beego/beego/v2/server/web/filter/cors"
"github.com/urfave/cli"
//http2 "net/http"
_ "net/http/pprof"
)
func main() {
//go func() {
// http2.ListenAndServe("0.0.0.0:3344", nil)
//}()
if err := setupApp().Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func setupApp() *cli.App {
app := cli.NewApp()
app.Name = "poly bridge server"
app.Usage = "poly-bridge http server"
app.Action = run
app.Version = "1.0.0"
app.Copyright = "Copyright in 2019 The PolyNetwork Authors"
app.Flags = []cli.Flag{
conf.ConfigPathFlag,
}
return app
}
func run(ctx *cli.Context) {
// Initialize
configFile := ctx.GlobalString("config")
config := conf.NewConfig(configFile)
if config == nil || config.HttpConfig == nil {
//fmt.Println(config)
//fmt.Println(config.HttpConfig)
panic("Invalid server config")
}
logs.SetLogger(logs.AdapterFile, fmt.Sprintf(`{"filename":"%s"}`, config.HttpLogFile))
basedef.ConfirmEnv(config.Env)
common.SetupChainsSDK(config)
web.InsertFilter("*", web.BeforeRouter, cors.Allow(
&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowCredentials: true,
},
))
// TG bot
common.TgBotInit()
// bridge http
http.Init()
// explorer http
explorer.Init()
// redis
cacheRedis.Init()
// register http routers
web.AddNamespace(
web.NewNamespace("/v1",
nft_http.Init(config),
http.GetRouter(config),
explorer.GetRouter(),
),
)
// Insert web config
web.BConfig.Listen.HTTPAddr = config.HttpConfig.Address
web.BConfig.Listen.HTTPPort = config.HttpConfig.Port
web.BConfig.RunMode = config.RunMode
web.BConfig.AppName = "bridgehttp"
web.BConfig.CopyRequestBody = true
web.BConfig.EnableErrorsRender = false
if config.RunMode == "dev" {
var FilterLog = func(ctx *context.Context) {
url, _ := json.Marshal(ctx.Input.Data()["RouterPattern"])
params := string(ctx.Input.RequestBody)
outputBytes, _ := json.Marshal(ctx.Input.Data()["json"])
divider := " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
topDivider := "┌" + divider
middleDivider := "├" + divider
bottomDivider := "└" + divider
outputStr := "\n" + topDivider + "\n│ url:" + string(url) + "\n" + middleDivider + "\n│ request:" + string(params) + "\n│ response:" + string(outputBytes) + "\n" + bottomDivider
logs.Info(outputStr)
}
web.InsertFilter("/*", web.FinishRouter, FilterLog)
}
web.Run()
}