-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·113 lines (101 loc) · 3.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
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
package main
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"local/auth-svc/auth"
handlers "local/auth-svc/handler"
"local/auth-svc/middleware"
accounts "local/auth-svc/services"
"local/auth-svc/sqldb"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v7"
"github.com/joho/godotenv"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
}
func setEncryptionKeyEnv() {
bytes := make([]byte, 32) //generate a random 32 byte key for AES-256
if _, err := rand.Read(bytes); err != nil {
panic(err.Error())
}
key := hex.EncodeToString(bytes) //encode key in bytes to string
os.Setenv("AESKEY", key)
fmt.Println("key :", os.Getenv("AESKEY"))
}
func NewRedisDB(host, port, password string) *redis.Client {
redisClient := redis.NewClient(&redis.Options{
Addr: host + ":" + port,
Password: password,
DB: 0,
})
return redisClient
}
func main() {
// setEncryptionKeyEnv()
sqldb.InitializeDatabase()
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)
allowedHost := os.Getenv("ALLOWED")
idp_ui := os.Getenv("IDP_UI")
var router = gin.Default()
router.Use(cors.New(cors.Config{
AllowOrigins: []string{allowedHost, idp_ui, "http://localhost:4200"},
AllowMethods: []string{"POST", "HEAD", "OPTIONS", "GET", "PUT"},
AllowHeaders: []string{"Access-Control-Allow-Headers", "Access-Control-Allow-Origin", "Origin", "Accept", "X-Requested-With", "Content-Type", "Authorization", "Access-Control-Request-Method", "Access-Control-Request-Headers"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
router.POST("/api/v1/login", service.SendLoginCookie)
router.POST("/api/v1/logout", service.LogoutSession)
router.POST("/api/v1/refresh", service.RefreshSession)
router.POST("/api/v1/account/new", middleware.TokenAuthMiddleware(), accounts.AddAccount)
router.POST("/api/v1/account/update", middleware.TokenAuthMiddleware(), accounts.ModifyAccount)
router.POST("/api/v1/account/remove", middleware.TokenAuthMiddleware(), accounts.RemoveAccount)
router.POST("/api/v1/account/list", middleware.TokenAuthMiddleware(), accounts.ListAccounts)
router.POST("/api/v1/account/find", middleware.TokenAuthMiddleware(), accounts.FindUser)
router.POST("/api/v1/account/toggle", middleware.TokenAuthMiddleware(), accounts.ToggleAccount)
router.POST("/api/v1/account/set", middleware.TokenAuthMiddleware(), accounts.SetPassword)
router.POST("/api/v1/role/new", middleware.TokenAuthMiddleware(), accounts.AddRole)
router.POST("/api/v1/role/update", middleware.TokenAuthMiddleware(), accounts.ModifyRole)
router.POST("/api/v1/role/remove", middleware.TokenAuthMiddleware(), accounts.RemoveRole)
router.POST("/api/v1/role/list", middleware.TokenAuthMiddleware(), accounts.ListRoles)
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")
}