-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.go
84 lines (73 loc) · 2.01 KB
/
core.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
package server
import (
"encoding/json"
"time"
"github.com/button-tech/logger"
"github.com/button-tech/utils-price-tool/core/internal/respond"
"github.com/button-tech/utils-price-tool/core/v1"
"github.com/button-tech/utils-price-tool/core/v2"
"github.com/button-tech/utils-price-tool/pkg/storage/cache"
"github.com/qiangxue/fasthttp-routing"
"github.com/valyala/fasthttp"
t "github.com/button-tech/utils-price-tool/types"
)
type Core struct {
S *fasthttp.Server
R *routing.Router
G *routing.RouteGroup
Gv2 *routing.RouteGroup
store *cache.Cache
}
func New(c *cache.Cache) (core *Core) {
core = &Core{
R: routing.New(),
store: c,
}
core.R.Use(cors)
core.initBaseRoute()
core.fs()
v1.API(core.G, c)
v2.API(core.Gv2, c)
return
}
func (core *Core) initBaseRoute() {
core.G = core.R.Group("/courses/v1")
core.Gv2 = core.R.Group("/courses/v2")
}
func (core *Core) fs() {
core.S = &fasthttp.Server{
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
Handler: core.R.HandleRequest,
}
}
func cors(ctx *routing.Context) error {
ctx.Response.Header.Set("Access-Control-Allow-Origin", string(ctx.Request.Header.Peek("Origin")))
ctx.Response.Header.Set("Access-Control-Allow-Credentials", "false")
ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET,HEAD,PUT,POST,DELETE")
ctx.Response.Header.Set(
"Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization",
)
if string(ctx.Method()) == "OPTIONS" {
ctx.Abort()
}
if err := ctx.Next(); err != nil {
if httpError, ok := err.(routing.HTTPError); ok {
ctx.Response.SetStatusCode(httpError.StatusCode())
} else {
ctx.Response.SetStatusCode(fasthttp.StatusInternalServerError)
}
b, err := json.Marshal(err)
if err != nil {
respond.WithJSON(ctx, fasthttp.StatusInternalServerError, t.Payload{
"error": err},
)
logger.Error("cors marshal", err)
return nil
}
ctx.SetContentType("application/json")
ctx.SetBody(b)
}
return nil
}