-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
58 lines (49 loc) · 1.25 KB
/
api.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
package api
import (
"log"
"net/http"
"github.com/gorilla/sessions"
"github.com/labstack/echo"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/middleware"
"github.com/nsogame/common"
)
type APIServer struct {
config *Config
db *common.DB
web *echo.Echo
}
func NewInstance(config *Config) (api *APIServer, err error) {
// db
db, err := common.ConnectDB(config.DbProvider, config.DbConnection)
if err != nil {
return
}
web := echo.New()
web.Debug = config.Debug
// middleware
web.Use(session.Middleware(sessions.NewCookieStore([]byte(config.SecretKey))))
web.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
web.Use(middleware.Recover())
web.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://localhost:1234", "https://osu.ppy.sh"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
AllowCredentials: true,
}))
api = &APIServer{
config: config,
db: db,
web: web,
}
api.router(web)
return
}
func (api *APIServer) Close() {
api.db.Close()
}
func (api *APIServer) Run() {
defer api.Close()
log.Fatal(api.web.Start(api.config.BindAddr))
}