-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (98 loc) · 3.19 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
114
115
116
117
118
119
120
121
122
123
package main
import (
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/liftconnect/handlers"
"github.com/liftconnect/models"
)
var AccessKeyID string
var SecretAccessKey string
var MyRegion string
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "http://localhost:3000")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS, GET, PUT, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func LoadEnv() {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
return
}
}
func GetEnvWithKey(key string) string {
return os.Getenv(key)
}
func ConnectAws() *session.Session {
AccessKeyID = GetEnvWithKey("AWS_ACCESS_KEY_ID")
SecretAccessKey = GetEnvWithKey("AWS_SECRET_ACCESS_KEY")
MyRegion = GetEnvWithKey("AWS_REGION")
sess, err := session.NewSession(
&aws.Config{
Region: aws.String(MyRegion),
Credentials: credentials.NewStaticCredentials(
AccessKeyID,
SecretAccessKey,
"", // a token will be created when the session it's used.
),
})
if err != nil {
log.Fatalf("Unable to create AWS session")
}
return sess
}
func main() {
LoadEnv()
sess := ConnectAws()
// Set the router as the default one shipped with Gin
router := gin.Default()
// Configure Cors
router.Use(CORSMiddleware())
// Configure AWS
router.Use(func(c *gin.Context) {
c.Set("sess", sess)
c.Next()
})
// Setup route group for the API
models.ConnectDataBase()
// Session
router.GET("/self", handlers.UserHandler)
router.POST("/self/login", handlers.LoginHandler)
router.GET("/self/logout", handlers.LogoutHandler)
api := router.Group("/api")
// Users
api.GET("/users/all/:id", handlers.GetUserHandler)
api.GET("/users/recommend/:id", handlers.RecommendedUserHandler)
api.GET("/users/:id", handlers.GetUserByIDHandler)
api.POST("/users/register", handlers.RegisterUserHandler)
api.POST("/users/personalrecords", handlers.CreatePersonalRecordsHandler)
api.POST("/users/:id/follow/:fid", handlers.FollowUserHandler)
// Posts
api.GET("/posts/:id", handlers.GetPostsByUserHandler)
api.POST("/posts", handlers.CreatePostHandler)
api.POST("/posts/comment", handlers.CreateCommentHandler)
api.POST("/posts/upload/:type", handlers.UploadFileHandler)
api.DELETE("/posts/:id", handlers.DeletePostHandler)
api.DELETE("posts/comment/:id", handlers.DeleteCommentHandler)
// Workouts
api.GET("/workouts/:uid", handlers.GetWorkoutsByUserHandler)
api.GET("/workouts/exercises", handlers.GetExercisesHandler)
api.GET("/workouts/exercises/target/:target", handlers.GetExercisesByTargetHandler)
api.GET("/workouts/exercises/name/:name", handlers.GetExercisesByNameHandler)
api.POST("/workouts", handlers.CreateWorkoutHandler)
// Start and run the server
router.Run(":8080")
}