-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (72 loc) · 1.9 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
package main
import (
"log"
"net/http"
"os"
"firebase.google.com/go/v4/auth"
"github.com/cprime50/fire-go-auth/role"
"github.com/cprime50/fire-go-auth/middleware"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
loadEnv()
// Initialize Firebase authentication middleware
client, err := middleware.InitAuth()
if err != nil {
log.Fatalf("Error initializing Firebase auth: %v", err)
}
r := gin.Default()
r.Use(cors.Default())
// Register routes
RegisterRoutes(r, client)
RegisterAdminRoutes(r, client)
// Set port
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Gin server is running on port %s", port)
if err := r.Run(":" + port); err != nil {
log.Fatalf("Failed to start Gin server: %v", err)
}
}
func loadEnv() {
err := godotenv.Load("./.env")
if err != nil {
log.Fatal("Error loading .env file", err)
}
log.Println(".env file loaded successfully")
}
func RegisterRoutes(r *gin.Engine, client *auth.Client) {
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "fire-go-auth success")
})
r.GET("/user", middleware.Auth(client), func(c *gin.Context) {
user, ok := c.Get("user")
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "User details not found"})
return
}
userDetails := user.(*middleware.User)
c.JSON(http.StatusOK, userDetails)
})
}
// Admin routes
func RegisterAdminRoutes(r *gin.Engine, client *auth.Client) {
adminService := role.NewAdminService(client)
adminRoutes := r.Group("/admin")
adminRoutes.Use(middleware.Auth(client), middleware.RoleAuth("admin"))
{
adminRoutes.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "admin")
})
adminRoutes.POST("/make", func(ctx *gin.Context) {
role.MakeAdminHandler(ctx, adminService)
})
adminRoutes.DELETE("/remove", func(ctx *gin.Context) {
role.RemoveAdminHandler(ctx, adminService)
})
}
}