-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
76 lines (64 loc) · 1.74 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
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v7"
"github.com/joho/godotenv"
"jwt-app/auth"
"jwt-app/handler"
"jwt-app/middleware"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
}
func NewRedisDB(host, port, password string) *redis.Client {
redisClient := redis.NewClient(&redis.Options{
Addr: host + ":" + port,
Password: password,
DB: 0,
})
return redisClient
}
func main() {
appAddr := ":" + os.Getenv("PORT")
//redis details
redis_host := os.Getenv("REDIS_HOST")
redis_port := os.Getenv("REDIS_PORT")
redis_password := os.Getenv("REDIS_PASSWORD")
redisClient := NewRedisDB(redis_host, redis_port, redis_password)
var rd = auth.NewAuth(redisClient)
var tk = auth.NewToken()
var service = handlers.NewProfile(rd, tk)
var router = gin.Default()
router.POST("/login", service.Login)
router.POST("/todo", middleware.TokenAuthMiddleware(), service.CreateTodo)
router.POST("/logout", middleware.TokenAuthMiddleware(), service.Logout)
router.POST("/refresh", service.Refresh)
srv := &http.Server{
Addr: appAddr,
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
//Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}