-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
140 lines (116 loc) · 3.57 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
140
package main
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "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 Account API
// @version 1.0
// @description API for account 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:8080
// @BasePath /api/v1
func main() {
err := env.Load("env/.env")
if err != nil {
log.Fatalf("failed to load env variables: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(os.Getenv("REGION")),
config.WithEndpointResolver(aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{URL: "http://dynamodb:8000"}, nil
})),
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: os.Getenv("AWS_ACCESS_KEY_ID"),
SecretAccessKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
},
}),
)
if err != nil {
log.Fatalf("failed to load SDK config, %s", err)
}
accountController := controller.AccountController{
DB: &db.AccountDB{
Client: dynamodb.NewFromConfig(cfg),
},
}
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).Use(util.UploadStat)
api := router.Group("api/v1").Use(util.ValidateToken)
{
api.POST("/account", accountController.Create)
api.GET("/accounts/:type", accountController.GetAll)
api.GET("/accounts/:type/transactions", accountController.GetAllWithTransactions)
api.GET("/account/:accountID", accountController.GetAccount)
api.PATCH("/account/:accountID/deposit", accountController.Deposit)
api.PATCH("/account/:accountID/withdraw", accountController.Withdraw)
api.PATCH("/account/:accountID/close", accountController.Close)
api.DELETE("/account/:accountID", accountController.Delete)
}
router.GET("api/v1/login", util.RandomToken)
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
srv := &http.Server{
Addr: ":8080",
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: %s\n", err)
}
log.Println("shutting down")
}