generated from CeoFred/osalobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (111 loc) · 3.53 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/CeoFred/gin-boilerplate/constants"
"github.com/CeoFred/gin-boilerplate/database"
"github.com/CeoFred/gin-boilerplate/internal/helpers"
"github.com/CeoFred/gin-boilerplate/internal/routes"
// "github.com/CeoFred/gin-boilerplate/internal/repository"
_ "golang.org/x/text"
docs "github.com/CeoFred/gin-boilerplate/docs"
apitoolkit "github.com/apitoolkit/apitoolkit-go"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// @title Gin Boilerplare
// @version 1.0
// @description Swagger API documentation for Gin Boilerplare API
// @termsOfService http://swagger.io/terms/
// @contact.name Johnson Awah Alfred
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host https://example.com
// @BasePath /api/v1
func main() {
g := gin.Default()
docs.SwaggerInfo.BasePath = "/api/v1"
constant := constants.New()
ctx := context.Background()
v := constants.New()
// Initialize the client using your apitoolkit.io generated apikey
apitoolkitClient, err := apitoolkit.NewClient(ctx, apitoolkit.Config{APIKey: v.APIToolkitKey})
if err != nil {
// Handle the error
// panic(err)
log.Println(err)
} else {
g.Use(apitoolkitClient.GinMiddleware)
}
// Parse command-line flags
flag.Parse()
g.Static("/assets", "./static/public")
g.Static("/templates", "./templates")
// Middleware
g.Use(gin.CustomRecovery(func(c *gin.Context, recovered any) {
if err, ok := recovered.(string); ok {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
}
c.AbortWithStatus(http.StatusInternalServerError)
}))
g.Use(gin.Logger())
g.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// your custom format
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
g.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:*"}, // add more origins
AllowMethods: []string{"PUT", "PATCH", "GET", "POST", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
g.MaxMultipartMemory = 8 << 20
dbConfig := database.Config{
Host: v.DbHost,
Port: v.DbPort,
Password: v.DbPassword,
User: v.DbUser,
DBName: v.DbName,
}
database.Connect(&dbConfig)
connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", v.DbUser, v.DbPassword, v.DbHost, v.DbPort, v.DbName)
database.RunManualMigration(connStr)
// Set up Swagger documentation
docs.SwaggerInfo.BasePath = "/api/v1"
url := ginSwagger.URL("/swagger/doc.json") // Specify the URL for Swagger JSON
g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler, url))
g.GET("/api/v1/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
v1 := g.Group("/api/v1")
routes.Routes(v1, database.DB)
g.NoRoute(func(c *gin.Context) {
helpers.ReturnError(c, "Something went wrong", fmt.Errorf("Route not found"), http.StatusNotFound)
})
port := os.Getenv("PORT")
if port == "" {
port = constant.Port
}
// Listen on port set in .env
log.Fatal(g.Run(":" + port))
}