-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
119 lines (104 loc) · 2.59 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
package main
import (
"database/sql"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/mohfahrur/interop-service-c/domain/database"
"github.com/joho/godotenv"
googleD "github.com/mohfahrur/interop-service-c/domain/google"
entity "github.com/mohfahrur/interop-service-c/entity"
"github.com/mohfahrur/interop-service-c/middleware"
ticketUC "github.com/mohfahrur/interop-service-c/usecase/ticket"
userUC "github.com/mohfahrur/interop-service-c/usecase/user"
)
func main() {
// load .env file from given path
// we keep it empty it will load .env from current directory
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
log.SetFlags(log.Llongfile)
spreadsheetID := os.Getenv("spreadsheetID")
dbConfig := os.Getenv("dbConfig")
pwd, err := os.Getwd()
if err != nil {
log.Println(err)
return
}
credentialsFile, err := ioutil.ReadFile(pwd + "/credential.json")
if err != nil {
log.Println(err)
return
}
db, err := sql.Open("mysql", dbConfig)
if err != nil {
log.Println("Failed to connect to the database:", err)
return
}
defer db.Close()
userDomain := database.NewUserDomain(db)
googleDomain := googleD.NewGoogleDomain(credentialsFile, spreadsheetID)
ticketUsecase := ticketUC.NewTicketUsecase(*googleDomain)
userUsecase := userUC.NewUserUsecase(*userDomain)
r := gin.Default()
r1 := r.Group("/v1")
r1.Use(middleware.AuthAndAuthorize(*userUsecase))
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong from service c",
})
return
})
r1.GET("/get-users/:id", func(c *gin.Context) {
datas, err := userUsecase.UserDomain.GetUserInfo(c.Param("id"))
if err != nil {
log.Println(err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "bad request",
})
return
}
c.JSON(http.StatusOK, datas)
return
})
r1.GET("/get-users", func(c *gin.Context) {
datas, err := userUsecase.UserDomain.GetUsers()
if err != nil {
log.Println(err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "bad request",
})
return
}
c.JSON(http.StatusOK, datas)
return
})
r.POST("/update-data", func(c *gin.Context) {
var req entity.UpdateSheetRequest
err := c.BindJSON(&req)
if err != nil {
log.Println(err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "bad request",
})
return
}
err = ticketUsecase.UpdateSheet(req)
if err != nil {
log.Println(err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "bad request",
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "success",
})
return
})
r.Run(":5002")
}