-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (63 loc) · 2.08 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
package main
import (
"fmt"
"vinpel/golang-sample-api-jwt/common/controllers"
"vinpel/golang-sample-api-jwt/common/database"
"vinpel/golang-sample-api-jwt/common/middlewares"
"vinpel/golang-sample-api-jwt/docs"
"vinpel/golang-sample-api-jwt/services/ping"
"vinpel/golang-sample-api-jwt/services/user"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files" // swagger embed files
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
)
// @title golang-sample-api-jwt
// @version 1.0
// @description Sample API endpoint in golang
// @contact.name API Support
// @contact.url https://github.com/vinpel/golang-sample-api-jwt/issues
// @license.name MIT
// @license.url https://fr.wikipedia.org/wiki/Licence_MIT
// @produce json
// @accept json
// @host localhost:8000
// @BasePath /
// @securitydefinitions.apikey JWTToken
// @in header
// @name Authorization
func main() {
// Initialize Database
//get := utils.GetEnvWithKey
USER := "root" //get("DB_USER")
PASS := "root" //get("DB_PASS")
HOST := "localhost" //get("DB_HOST")
PORT := "3306" //get("DB_PORT")
DBNAME := "myDatabase" // get("DB_NAME")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/?charset=utf8&parseTime=True&loc=Local", USER, PASS, HOST, PORT)
database.Connect(dsn, DBNAME)
database.Migrate()
docs.SwaggerInfo.BasePath = "/"
// Initialize Router
router := initRouter()
router.Run("localhost:8000")
}
func initRouter() *gin.Engine {
router := gin.Default()
if gin.Mode() != gin.ReleaseMode {
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}
api := router.Group("/api")
{
token := api.Group("/token")
{
token.POST("/from-user", controllers.GenerateTokenFromUser)
token.POST("/from-refresh", controllers.GenerateTokenFromRefreshToken)
}
secured := api.Group("/secured").Use(middlewares.Auth())
{
secured.GET("/ping", ping.Ping)
}
}
user.RegisterRoutes(api)
return router
}