-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler.go
63 lines (51 loc) · 1.53 KB
/
handler.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
package function
import (
"context"
"net/http"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/red-gold/telar-core/config"
"github.com/red-gold/telar-core/pkg/log"
micros "github.com/red-gold/telar-web/micros"
"github.com/red-gold/telar-web/micros/profile/database"
"github.com/red-gold/telar-web/micros/profile/router"
)
// Cache state
var app *fiber.App
func init() {
micros.InitConfig()
// Initialize app
app = fiber.New()
app.Use(recover.New())
app.Use(requestid.New())
app.Use(logger.New(
logger.Config{
Format: "[${time}] ${status} - ${latency} ${method} ${path} - ${header:}\n",
},
))
app.Use(cors.New(cors.Config{
AllowOrigins: *config.AppConfig.Origin,
AllowCredentials: true,
AllowHeaders: "Origin, Content-Type, Accept, Access-Control-Allow-Headers, X-Requested-With, X-HTTP-Method-Override, access-control-allow-origin, access-control-allow-headers",
}))
router.SetupRoutes(app)
}
// Handler function
func Handle(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
// Connect
if database.Db == nil {
var startErr error
startErr = database.Connect(ctx)
if startErr != nil {
log.Error("Error startup: %s", startErr.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(startErr.Error()))
}
}
adaptor.FiberApp(app)(w, r)
}