-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (65 loc) · 1.87 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
package main
import (
"database/sql"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/google/subcommands"
"github.com/joho/godotenv"
"github.com/snoeffels/mygo/api"
"github.com/snoeffels/mygo/config"
_ "github.com/snoeffels/mygo/docs"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"gorm.io/gorm"
"log"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /api/v1
// @query.collection.format multi
// @title Golang Project Template
// @description This is a template project for upcoming golang project ideas. It uses gorm as ORM with Postgres and also uses JWT auth middleware if GIN_MODE=release is set in .env / .env-docker
// @schemes http https
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
type dbHandlers struct {
db *gorm.DB
}
func main() {
var err error
var dbHandler dbHandlers
if err = godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
dbHandler.db, err = config.InitPostgres()
if err != nil {
panic(err)
}
sqlDB, _ := dbHandler.db.DB()
defer func(sqlDB *sql.DB) {
if err := sqlDB.Close(); err != nil {
log.Fatal("Could not close db connection")
}
}(sqlDB)
r := initRoutes(dbHandler)
log.Fatal(r.Run())
}
func initRoutes(dbHandler dbHandlers) *gin.Engine {
r := gin.Default()
conf := cors.DefaultConfig()
conf.AllowAllOrigins = true
r.Use(cors.New(conf))
todoAPI := initTodoAPI(dbHandler.db)
api.TodoRoute(r, todoAPI)
r.GET("/swagger-ui/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
return r
}