-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (75 loc) · 2.78 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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/mftakhullaziz/gorest/config"
_ "github.com/mftakhullaziz/gorest/config/swagger_docs"
api "github.com/mftakhullaziz/gorest/handler"
"github.com/mftakhullaziz/gorest/middleware"
"github.com/mftakhullaziz/gorest/repository"
"github.com/mftakhullaziz/gorest/service"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
)
var (
db = config.SetupDatabaseConnection()
userRepository = repository.NewUserRepository(db)
bookRepository = repository.NewBookRepository(db)
jwtService = service.NewJWTService()
userService = service.NewUserService(userRepository)
authService = service.NewAuthService(userRepository)
bookService = service.NewBookService(bookRepository)
authController = api.NewAuthController(authService, jwtService)
userController = api.NewUserController(userService, jwtService)
bookController = api.NewBookController(bookService, jwtService)
)
// @title Go Rest - Spec Documentation 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 /handler/v1
// @securityDefinitions.basic BasicAuth
func main() {
defer config.CloseDatabaseConnection(db)
r := gin.Default()
v1 := r.Group("/handler/v1")
{
authRoutes := v1.Group("/authenticate")
{
authRoutes.POST("/signIn", authController.Login)
authRoutes.POST("/#", authController.Register)
}
dataUserRoutes := v1.Group("/user")
{
dataUserRoutes.GET("/", userController.AllUser)
}
userRoutes := v1.Group("/user", middleware.AuthorizeJWT(jwtService))
{
userRoutes.GET("/profile/:id", userController.Profile)
userRoutes.PUT("/profile", userController.Update)
}
bookRoutes := v1.Group("/books", middleware.AuthorizeJWT(jwtService))
{
bookRoutes.GET("/", bookController.All)
bookRoutes.POST("/add/", bookController.Insert)
bookRoutes.GET("/fetch/:id", bookController.FindByID)
bookRoutes.PUT("/update/:id", bookController.Update)
bookRoutes.DELETE("/delete/:id", bookController.Delete)
}
}
// r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
fmt.Println("Documentation API : http://localhost:8080/swagger/index.html")
err := r.Run()
if err != nil {
return
}
// listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}