-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (60 loc) · 2.16 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
package main
import (
"SocialMedia/server/database"
"SocialMedia/server/restapi"
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/gofiber/fiber/v2"
)
var (
SERVER_URI = os.Getenv("SERVER_URI")
MONGO_URI = os.Getenv("MONGO_URI")
MONGO_DATABASE = os.Getenv("MONGO_DATABASE")
)
func main() {
if SERVER_URI == "" || MONGO_URI == "" || MONGO_DATABASE == "" {
log.Fatalln("Environment variables missing...")
}
database.ConnectDatabase(MONGO_URI, MONGO_DATABASE)
app := fiber.New()
app.Post("/api/register", restapi.Register)
app.Post("/api/login", restapi.Login)
app.Post("/api/logout", restapi.Logout)
app.Get("/api/feed", restapi.GetFeed)
app.Get("/api/feed/global", restapi.GetGlobalFeed)
app.Get("/api/me", restapi.GetCurrentUser)
app.Get("/api/users/:username", restapi.GetUser)
app.Get("/api/users-id/:id", restapi.GetUserByID)
app.Get("/api/users/:username/likes", restapi.GetUserLikes)
app.Get("/api/users/:username/posts", restapi.GetUserPosts)
app.Post("/api/users/:username/posts", restapi.CreatePost)
app.Post("/api/users/:username/follow", restapi.FollowUser)
app.Delete("/api/users/:username/follow", restapi.UnfollowUser)
app.Get("/api/users/:username/following", restapi.GetUserFollowing)
// app.Get("/api/users/:username/followers", restapi.GetUserFollowers)
app.Get("/api/posts/:post_id", restapi.GetPost)
// app.Delete("/api/posts/:post_id", restapi.DeletePost)
app.Get("/api/posts/:post_id/replies", restapi.GetReplies)
app.Get("/api/posts/:post_id/author", restapi.GetAuthor)
// app.Get("/api/posts/:post_id/likes", restapi.GetUserPosts)
app.Post("/api/posts/:post_id/like", restapi.AddLike)
app.Delete("/api/posts/:post_id/like", restapi.RemoveLike)
app.Static("/assets", "./web/dist/assets")
app.Static("*", "./web/dist/index.html")
go log.Fatalln(app.Listen(SERVER_URI))
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
log.Println("Gracefully shutting down...")
if err := app.Shutdown(); err != nil {
log.Println(err)
}
log.Println("Running cleanup tasks...")
if err := database.Mongo.Client().Disconnect(context.TODO()); err != nil {
log.Println(err)
}
log.Println("Done")
}