-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (82 loc) · 2.59 KB
/
main.go
File metadata and controls
99 lines (82 loc) · 2.59 KB
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
package main
import (
"context"
"fmt"
"log"
"time"
firebase "firebase.google.com/go"
jwtMiddleware "github.com/auth0/go-jwt-middleware"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"google.golang.org/api/option"
)
var (
app *firebase.App
repo *Repo
corsMiddleware = cors.New(cors.Config{
// // AllowOrigins: []string{"https://wheypal.com", "http://localhost:8080"},
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
AllowHeaders: []string{"Authorization", "Origin", "Content-Length", "Content-Type"},
// AllowOrigins: []string{"*"},
// AllowMethods: []string{"*"},
// AllowHeaders: []string{"*"},
AllowCredentials: true,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
})
)
const (
sqlConnString = "postgres://allen:jzx5sWtgykoGDe8Y@free-tier.gcp-us-central1.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full&sslrootcert=secrets/cc-ca.crt&options=--cluster=nimble-vole-835"
)
func main() {
fmt.Println("Starting Server")
r := gin.Default()
r.Use(corsMiddleware)
opt := option.WithCredentialsFile("secrets/firebase-key.json") //TO DO UPDATE KEY
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing app: %v", err)
}
repo = NewRepo(sqlConnString)
authMiddleware := func() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := context.Background()
idToken, _ := jwtMiddleware.FromAuthHeader(c.Request)
// fmt.Println(c.Request, idToken)
client, err := app.Auth(ctx)
if err != nil {
log.Printf("error getting Auth client: %v\n", err)
c.JSON(401, err)
return
}
token, err := client.VerifyIDToken(ctx, idToken)
if err != nil {
log.Printf("error verifying ID token: %v\n", err)
c.JSON(401, err)
return
}
// log.Printf("Verified ID token: %v\n", token)
c.Set("token", token)
c.Set("uid", token.UID)
// log.Printf("UID %v", token.UID)
}
}
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.GET("/auth", authMiddleware(), func(c *gin.Context) {
c.JSON(200, gin.H{
"message": c.MustGet("token"),
})
})
r.GET("/profile", authMiddleware(), getProfile)
r.PUT("/profile", authMiddleware(), updateProfile)
r.GET("/discover", authMiddleware(), getLoc)
r.POST("/discover", authMiddleware(), postLoc)
r.DELETE("/match", authMiddleware(), deleteMatch)
r.GET("/match", authMiddleware(), getMatches)
// r.POST("/endpoint", authMiddleware(), endpointDandler)
r.Run(":8081") // listen and serve on 0.0.0.0:8081 (for windows "localhost:8081")
}