-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (68 loc) · 2.05 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
package main
import (
"go-3-db-digi/handler"
"go-3-db-digi/middleware"
"log"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func main() {
// Env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file", err)
}
// Database
db := NewDatabase()
sqlDB, err := db.DB()
if err != nil {
log.Fatal("failed to get DB from GORM:", err)
}
defer sqlDB.Close()
// secret-key
signingKey := os.Getenv("SIGNING_KEY")
// import "github.com/gin-contrib/cors"
r := gin.Default()
config := cors.DefaultConfig()
config.AddAllowHeaders("Authorization") // Allow Header: Authorization
config.AllowAllOrigins = true
r.Use(cors.New(config))
// grouping route with /auth
authHandler := handler.NewAuth(db, []byte(signingKey))
authRoute := r.Group("/auth")
authRoute.POST("/login", authHandler.Login)
authRoute.POST("/upsert", authHandler.Upsert)
// grouping route with /account
accountHandler := handler.NewAccount(db)
accountRoutes := r.Group("/account")
accountRoutes.POST("/create", accountHandler.Create)
accountRoutes.GET("/read/:id", accountHandler.Read)
accountRoutes.PATCH("/update/:id", accountHandler.Update)
accountRoutes.DELETE("/delete/:id", accountHandler.Delete)
accountRoutes.GET("/list", accountHandler.List)
accountRoutes.GET("/my", middleware.AuthMiddleware(signingKey), accountHandler.My)
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func NewDatabase() *gorm.DB {
// dsn := "host=localhost port=5432 user=postgres dbname=digi sslmode=disable TimeZone=Asia/Jakarta"
dsn := os.Getenv("DATABASE")
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatal(err)
}
sqlDB, err := db.DB()
if err != nil {
log.Fatalf("failed to get DB object: %v", err)
}
var currentDB string
err = sqlDB.QueryRow("SELECT current_database()").Scan(¤tDB)
if err != nil {
log.Fatalf("failed to query current database: %v", err)
}
log.Printf("Current Database: %s\n", currentDB)
return db
}