-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (104 loc) · 3.28 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
package main
import (
"io"
"net/http"
"os"
"github.com/gireeshcse/go-gin/model"
"gorm.io/gorm"
"github.com/gin-gonic/gin"
"github.com/gireeshcse/go-gin/config"
"github.com/gireeshcse/go-gin/controller"
"github.com/gireeshcse/go-gin/middlewares"
"github.com/gireeshcse/go-gin/service"
)
var (
userService service.UserService = service.New()
userController controller.UserController = controller.New(userService)
jwtService service.JWTService = service.NewJWTService(config.ISSUER, config.SECRET, config.EXPIRATIONTIME)
)
func setupLogOutput() {
f, _ := os.Create("server.log")
gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}
func main() {
// r := web.SetupRouter()
setupLogOutput()
server := gin.New()
model.InitiateApp()
server.Static("/css", "./templates/css")
server.LoadHTMLGlob("templates/*.html")
server.Use(gin.Recovery(), middlewares.Logger())
apiRoutes := server.Group("/api")
{
apiAuthRoutes := apiRoutes.Group("/admin")
apiAuthRoutes.Use(middlewares.BasicAuth())
{
apiAuthRoutes.GET("/users", func(ctx *gin.Context) {
ctx.JSON(200, userController.FindAll())
})
}
apiRoutes.POST("/users", func(ctx *gin.Context) {
user, err := userController.Save(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
ctx.JSON(200, user)
}
})
apiRoutes.POST("/login", func(ctx *gin.Context) {
user, err := userController.Login(ctx)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
} else {
var roles = []string{"Basic", "Authenticated"}
token := jwtService.GenerateToken(user.Username, roles)
ctx.JSON(200, gin.H{"user": user, "token": token})
}
})
apiJWTAuthRoutes := apiRoutes.Group("/auth")
apiJWTAuthRoutes.Use(middlewares.JWTAuth())
{
apiJWTAuthRoutes.GET("/user", func(ctx *gin.Context) {
token, _ := ctx.Get("token")
ctx.JSON(200, gin.H{"token": token})
})
}
apiDBAuthRoutes := apiRoutes.Group("/db")
apiDBAuthRoutes.Use(middlewares.MySQLDB())
{
apiDBAuthRoutes.GET("/test", func(ctx *gin.Context) {
ctx.JSON(200, gin.H{"db": "Connection to db is ok"})
})
// curl http://localhost:8080/api/db/users
apiDBAuthRoutes.GET("/users", func(ctx *gin.Context) {
db, _ := ctx.Get("db")
database := db.(*gorm.DB)
rows := map[string]interface{}{}
database.Table("users").Find(&rows)
ctx.JSON(200, gin.H{"db": rows})
})
// curl http://localhost:8080/api/db/migrate
apiDBAuthRoutes.GET("/migrate", func(ctx *gin.Context) {
db, _ := ctx.Get("db")
database := db.(*gorm.DB)
database.AutoMigrate(&model.User{})
database.AutoMigrate(&model.Company{})
ctx.JSON(200, gin.H{"db": "migration success"})
})
apiDBAuthRoutes.GET("/create", func(ctx *gin.Context) {
db, _ := ctx.Get("db")
database := db.(*gorm.DB)
company := model.Company{Name: "Google", City: "California", State: "California", Country: "USA"}
database.Create(&company)
user := model.User{Name: "Ram", Username: "ram", Email: "ram@test.com", Password: "ram123", CompanyID: uint64(company.ID)}
database.Create(&user)
ctx.JSON(200, gin.H{"db": user})
})
}
}
viewRoutes := server.Group("/view")
{
viewRoutes.GET("/users", userController.ShowAll)
}
server.Run(":8080")
}