-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
52 lines (42 loc) · 1.1 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
package main
import (
"net/http"
"net/http/httputil"
"net/url"
"api_microservice/controller"
"api_microservice/middleware"
"api_microservice/service"
"github.com/gin-gonic/gin"
)
var (
jwtService service.JWTService = service.NewJWTService()
loginController controller.LoginController = controller.NewLoginController(jwtService)
)
func main() {
router := gin.New()
// Create reverse proxy
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "http",
Host: "127.0.0.1:5000",
})
// Test endpoint connectivity
router.GET("/", func(ctx *gin.Context) {
ctx.JSON(http.StatusAccepted, gin.H{"data": "App up and running"})
})
// endpoint to login and generate JWT Token
router.POST("/login", func(ctx *gin.Context) {
token := loginController.Login(ctx)
if token != "" {
ctx.JSON(http.StatusOK, gin.H{
"token": token,
})
} else {
ctx.JSON(http.StatusUnauthorized, nil)
}
})
// Route requests to python backend
router.Any("/api/*path", middleware.AuthorizeToken(), func(ctx *gin.Context) {
proxy.ServeHTTP(ctx.Writer, ctx.Request)
})
router.Run(":8005")
}