-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (113 loc) · 3.09 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"database/sql"
"errors"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
"log"
"main/controller"
"main/db"
_ "main/docs"
"main/env"
"main/messaging"
"main/util"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
// @title cr24 Transaction API
// @version 1.0
// @description API for transaction management for cr24 project
// @termsOfService http://swagger.io/terms/
// @contact.name David Slatinek
// @contact.url https://github.com/david-slatinek
// @accept json
// @produce json
// @schemes http
// @license.name GNU General Public License v3.0
// @license.url https://www.gnu.org/licenses/gpl-3.0.html
// @securityDefinitions.apikey JWT
//@in header
//@name Authorization
// @host localhost:8085
// @BasePath /api/v1
func main() {
err := env.Load("env/.env")
if err != nil {
log.Fatalf("failed to load env variables: %v", err)
}
uri := os.Getenv("MYSQL_URL")
if uri == "" {
log.Fatal("MYSQL_URL is not set")
}
mysqlDB, err := sql.Open("mysql", os.Getenv("MYSQL_URL"))
if err != nil {
log.Fatalf("error with sql.Open: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = mysqlDB.PingContext(ctx)
if err != nil {
log.Fatalf("ping error: %v", err)
}
mysqlDB.SetConnMaxLifetime(time.Minute * 3)
mysqlDB.SetMaxOpenConns(10)
mysqlDB.SetMaxIdleConns(10)
defer func(mysqlDB *sql.DB) {
if err := mysqlDB.Close(); err != nil {
log.Printf("Close() error: %v", err)
}
}(mysqlDB)
transactionController := controller.TransactionController{
DB: &db.TransactionDB{
DB: mysqlDB,
},
}
gin.SetMode(os.Getenv("GIN_MODE"))
router := gin.Default()
msg := messaging.Messaging{}
err = msg.Init()
if err != nil {
log.Printf("error with messaging: %s\n", err)
} else {
router.Use(msg.WriteInfo).Use(msg.WriteError)
defer msg.Close()
}
router.Use(util.CORS)
api := router.Group("api/v1").Use(util.ValidateToken)
{
api.POST("/transaction", transactionController.Create)
api.GET("/types", transactionController.GetTypes)
api.GET("/transaction/:accountID/:type", transactionController.GetAll)
api.DELETE("/transaction/:transactionID", transactionController.Delete)
api.DELETE("/transactions/:accountID", transactionController.DeleteForAccount)
}
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
srv := &http.Server{
Addr: ":8085",
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: router,
}
go func() {
log.Println("server is up at: " + srv.Addr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("ListenAndServe() error: %s\n", err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM)
<-c
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("Shutdown() error: %v", err)
}
log.Println("shutting down")
}