-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (58 loc) · 1.67 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
68
package main
import (
"github.com/aryanicosa/go-fiber-rest-api/pkg/configs"
"github.com/aryanicosa/go-fiber-rest-api/pkg/middleware"
"github.com/aryanicosa/go-fiber-rest-api/pkg/routes"
"github.com/aryanicosa/go-fiber-rest-api/pkg/utils"
"github.com/aryanicosa/go-fiber-rest-api/platform/database"
"github.com/aryanicosa/go-fiber-rest-api/platform/migrations"
"github.com/joho/godotenv"
"log"
"os"
"github.com/gofiber/fiber/v2"
)
// @title Fiber Example API
// @version 1.0
// @description This is a sample swagger for Go Fiber Rest API
// @contact.name API Support
// @contact.email [email protected]
// @BasePath /
// @securityDefinitions.basic BasicAuth
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
// Define Fiber config.
config := configs.FiberConfig()
// define a new fiber app with config
app := fiber.New(config)
// middlewares
middleware.FiberMiddleware(app)
err := godotenv.Load(".env")
if err != nil {
log.Fatal(err)
}
// init connect to db
_, errInitDb := database.InitDBConnection()
if errInitDb != nil {
log.Fatal("could not load database")
}
// migration
migrationFileSource := os.Getenv("SQL_SOURCE_PATH")
err = migrations.Migrate(migrationFileSource)
if err != nil {
log.Fatal("database migration fail")
}
// Routes.
routes.SwaggerRoute(app) // Register a route for API Docs (Swagger).
routes.UsersRoutes(app)
routes.BooksRoutes(app)
routes.MiscRoutes(app)
routes.NotFoundRoute(app) // Register route for 404 Error.
// Start server (with or without graceful shutdown).
if os.Getenv("STAGE_STATUS") == "dev" {
utils.StartServer(app)
} else {
utils.StartServerWithGracefulShutdown(app)
}
}