forked from foxiswho/echo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
executable file
·120 lines (100 loc) · 2.71 KB
/
router.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
package router
import (
"context"
"net/url"
"os"
"os/signal"
"time"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/labstack/gommon/log"
. "github.com/foxiswho/echo-go/conf"
"github.com/foxiswho/echo-go/middleware/opentracing"
"github.com/foxiswho/echo-go/router/example"
"github.com/foxiswho/echo-go/router/example/socket"
"github.com/foxiswho/echo-go/router/web"
)
type (
Host struct {
Echo *echo.Echo
}
)
func InitRoutes() map[string]*Host {
// Hosts
hosts := make(map[string]*Host)
hosts[Conf.Server.DomainWeb] = &Host{web.Routers()}
hosts[Conf.Server.DomainApi] = &Host{example.RoutersApi()}
hosts[Conf.Server.DomainSocket] = &Host{socket.Routers()}
return hosts
}
// 子域名部署
func RunSubdomains(confFilePath string) {
// 配置初始化
if err := InitConfig(confFilePath); err != nil {
log.Panic(err)
}
// 全局日志级别
log.SetLevel(GetLogLvl())
// Server
e := echo.New()
e.Pre(mw.RemoveTrailingSlash())
// OpenTracing
otCtf := opentracing.Configuration{
Disabled: Conf.Opentracing.Disable,
Type: Conf.Opentracing.Type,
}
if closer := otCtf.InitGlobalTracer(
opentracing.ServiceName(Conf.Opentracing.ServiceName),
opentracing.Address(Conf.Opentracing.Address),
); closer != nil {
defer closer.Close()
}
// 日志级别
e.Logger.SetLevel(GetLogLvl())
// Secure, XSS/CSS HSTS
e.Use(mw.SecureWithConfig(mw.DefaultSecureConfig))
mw.MethodOverride()
// CORS
e.Use(mw.CORSWithConfig(mw.CORSConfig{
AllowOrigins: []string{"http://" + Conf.Server.DomainWeb, "http://" + Conf.Server.DomainApi},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAcceptEncoding, echo.HeaderAuthorization},
}))
hosts := InitRoutes()
e.Any("/*", func(c echo.Context) (err error) {
req := c.Request()
res := c.Response()
u, _err := url.Parse(c.Scheme() + "://" + req.Host)
if _err != nil {
e.Logger.Errorf("Request URL parse error:%v", _err)
}
host := hosts[u.Hostname()]
if host == nil {
e.Logger.Info("Host not found")
err = echo.ErrNotFound
} else {
host.Echo.ServeHTTP(res, req)
}
return
})
if !Conf.Server.Graceful {
e.Logger.Fatal(e.Start(Conf.Server.Addr))
} else {
// Graceful Shutdown
// Start server
go func() {
if err := e.Start(Conf.Server.Addr); err != nil {
e.Logger.Errorf("Shutting down the server with error:%v", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}
}