-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
143 lines (118 loc) · 3.33 KB
/
server.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
132
133
134
135
136
137
138
139
140
141
142
143
package server
import (
"embed"
"net/http"
"os"
"os/signal"
"syscall"
"time"
dbRestServer "github.com/dbrest-io/dbrest/server"
"github.com/dbrest-io/dbrest/state"
"github.com/flarco/g"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
"github.com/spf13/cast"
)
// Server is the main server
type Server struct {
Host string
Port string
EchoServer *echo.Echo
StartTime time.Time
}
//go:embed app
var appFiles embed.FS
var contentHandler = echo.WrapHandler(http.FileServer(http.FS(appFiles)))
var contentRewrite = middleware.Rewrite(map[string]string{
"/*": "/app/$1",
"/static/*": "/app/static/$1",
"/assets/*": "/app/assets/$1",
})
// RouteName is the name of a route
type RouteName string
const (
// RouteWs is the websocket route
RouteIndex RouteName = "/"
)
func (r RouteName) String() string {
return string(r)
}
// NewServer creates a new server
func NewServer() *Server {
e := echo.New()
// e.Use(sentry.SentryEcho())
e.Use(middleware.RequestIDWithConfig(middleware.RequestIDConfig{
Generator: func() string {
return cast.ToString(time.Now().UnixNano())
},
}))
// CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
// AllowOrigins: []string{"http://localhost:5987", "http://localhost:3000", "http://localhost:3001", "tauri://localhost", "https://custom-protocol-taurilocalhost"},
// AllowCredentials: true,
// AllowHeaders: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, "X-Request-ID", "X-Request-Columns", "X-Request-Continue", "access-control-allow-origin", "access-control-allow-headers"},
AllowOriginFunc: func(origin string) (bool, error) {
return true, nil
},
}))
// embedded files
e.GET(RouteIndex.String()+"*", contentHandler, contentRewrite)
// add routes
for _, route := range StandardRoutes {
route.Middlewares = append(route.Middlewares, middleware.Logger())
route.Middlewares = append(route.Middlewares, middleware.Recover())
e.AddRoute(route)
}
for _, route := range dbRestServer.StandardRoutes {
route.Middlewares = append(route.Middlewares, middleware.Logger())
route.Middlewares = append(route.Middlewares, middleware.Recover())
switch route.Name {
case "submitSQL", "submitSQL_ID", "cancelSQL", "getTableSelect":
route.Middlewares = append(route.Middlewares, queryMiddleware)
default:
route.Middlewares = append(route.Middlewares, schemataMiddleware)
}
e.AddRoute(route)
}
port := os.Getenv("PORT")
if port == "" {
port = "5987"
}
host := os.Getenv("HOST")
if host == "" {
host = "0.0.0.0" // default
}
return &Server{
Host: host,
Port: port,
EchoServer: e,
}
}
// Start starts the server
func (srv *Server) Start() {
defer srv.Close()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
srv.Close()
os.Exit(1)
}()
srv.StartTime = time.Now()
if err := srv.EchoServer.Start(srv.Host + ":" + srv.Port); err != http.ErrServerClosed {
g.LogFatal(g.Error(err, "could not start server"))
}
}
// Loop cycles tasks
func (srv *Server) Loop() {
ticker6Hours := time.NewTicker(6 * time.Hour)
defer ticker6Hours.Stop()
<-ticker6Hours.C
}
func (srv *Server) Hostname() string {
return g.F("http://%s:%s", srv.Host, srv.Port)
}
func (srv *Server) Close() {
state.CloseConnections()
}