-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (58 loc) · 1.59 KB
/
main.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
package main
import (
"fmt"
"go-buks/configs"
"go-buks/database"
"go-buks/docs"
"go-buks/models"
"go-buks/routes"
"go-buks/utils"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/helmet"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/swagger"
)
// @securityDefinitions.apikey BearerAuth
// @name Authorization
// @in header
// @description Type "Bearer" followed by a space and then your token
func main() {
// Load Env
configs.LoadEnv()
// Database
database.Connect(configs.Env.DATABASE_URI)
database.Migrate(&models.User{}, &models.Book{})
// Server
port := fmt.Sprintf(":%s", configs.Env.PORT)
app := fiber.New(fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
ErrorHandler: utils.ErrorHandler,
})
// Swagger
docs.SwaggerInfo.Title = "Go-Buks API"
docs.SwaggerInfo.Version = "2.0"
docs.SwaggerInfo.BasePath = "/"
if configs.Env.APP_ENV == "production" {
docs.SwaggerInfo.Host = fmt.Sprintf("%v", configs.Env.HOST)
docs.SwaggerInfo.Schemes = []string{"https"}
} else {
docs.SwaggerInfo.Host = fmt.Sprintf("%v%v", configs.Env.HOST, port)
docs.SwaggerInfo.Schemes = []string{"http"}
}
// Middlewares
app.Use(logger.New(), helmet.New())
// Routes
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(map[string]interface{}{
"statusCode": fiber.StatusOK,
"message": "Welcome to Go-buks API",
"techs": []string{"Fiber", "PostgreSQL"},
})
})
app.Get("/docs/*", swagger.HandlerDefault)
routes.Routes(app)
// Running Server
app.Listen(port)
}