-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (52 loc) · 1.5 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
package main
import (
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/marian0/expenses-go-api/common"
"github.com/marian0/expenses-go-api/components/user"
"github.com/marian0/expenses-go-api/config"
"github.com/marian0/expenses-go-api/database"
"github.com/marian0/expenses-go-api/middlewares"
"gorm.io/gorm"
)
//DB - global variable to represent the DB connection
var DB *gorm.DB
func init() {
//Load environment variables
err := godotenv.Load()
common.LogFatal(err)
//Connect to postgres
DB = database.ConnectToPosgres()
//Run migrations engine to keep DB up to date
database.MigrateDatabase(DB)
}
func main() {
log.Println("Hello Expeses Api")
userAPI := user.NewUserAPI(DB)
router := gin.Default()
// configure firebase
firebaseAuth := config.SetupFirebase()
// set firebase auth to gin context with a middleware to all incoming request
router.Use(func(c *gin.Context) {
c.Set("firebaseAuth", firebaseAuth)
})
router.GET("/login", func(c *gin.Context) {
common.RespondSuccess(c, http.StatusOK, gin.H{"message": "This endpoint should be public!"})
})
// using the auth middleware to validate api requests
router.Use(middlewares.AuthMiddleware)
{
router.GET("/users", userAPI.FindAll)
router.GET("/users/:id", userAPI.FindByID)
router.POST("/users", userAPI.Create)
router.PUT("/users/:id", userAPI.Update)
router.DELETE("/users/:id", userAPI.Delete)
}
err := router.Run(":" + os.Getenv("APP_PORT"))
if err != nil {
panic(err)
}
}