-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (74 loc) · 1.84 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
_ "API-ECHO-MONGODB/docs"
"API-ECHO-MONGODB/mongodb"
"API-ECHO-MONGODB/router"
"context"
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
)
// @title REST Echo Base
// @version 1.0
// @description This is an API base without database interaction
// @contact.name My linkedin profile
// @contact.url https://www.linkedin.com/in/joaoschoen/
// @contact.email [email protected]
func main() {
// Environment config
if err := godotenv.Load(".env"); err != nil {
log.Println("No .env file found")
os.Exit(1)
}
// PORT
PORT, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
PORT = 8080
}
// DEBUG MODE
DEBUG, err := strconv.ParseBool(os.Getenv("DEBUG"))
if err != nil {
DEBUG = false
}
// TOKEN SECRET
TOKEN_SECRET := os.Getenv("TOKEN_SECRET")
if TOKEN_SECRET == "" {
log.Fatal("No encryption secret set up, you must set 'TOKEN_SECRET' environment variable.")
}
// DB CONNECTION
client := mongodb.Connect()
defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
// Echo instance
Server := echo.New()
// Middleware stack
Server.Use(middleware.CORS())
Server.Use(middleware.Secure())
Server.Use(middleware.RequestID())
Server.Use(middleware.Logger())
Server.Pre(middleware.RemoveTrailingSlash())
Server.Use(middleware.Recover())
// Routes
router.InitRoutes(Server)
// Print routes for debbuging
if DEBUG {
data, err := json.MarshalIndent(Server.Routes(), "", " ")
if err != nil {
panic(err)
}
os.WriteFile("routes.json", data, 0644)
// Add swagger ui handler
Server.GET("/swagger/*", echoSwagger.WrapHandler)
}
// Initialize server
Server.Logger.Fatal(Server.Start(fmt.Sprint(":", PORT)))
}