This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
98 lines (80 loc) · 3.65 KB
/
server.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
package breadboard
import (
"context"
"log"
"net/http"
"os"
"time"
database "breadboard/database"
h "breadboard/handlers"
m "breadboard/middleware"
"github.com/go-co-op/gocron"
"github.com/gorilla/mux"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/rs/cors"
)
type server struct {
pool *pgxpool.Pool
router *mux.Router
}
// NewServer returns a new app instance.
func NewServer() *server {
pool, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalln("Unable to connect to database:\n", err)
}
// !TODO: Make separate interface to initialise database
database.Init(pool)
// Initialize cronjob for fetching announcements
cron := gocron.NewScheduler(time.UTC)
cron.Every(1).Day().At("00:00").Do(func() {
h.FetchAnnouncements(pool)
})
cron.StartAsync()
s := server{pool: pool, router: mux.NewRouter().StrictSlash(true)}
s.setRouters()
return &s
}
// Run executes the app to listen on a given port and serve the routers.
func (s *server) Run() {
c := cors.New(cors.Options{
AllowCredentials: true,
AllowedHeaders: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedOrigins: []string{"http://localhost:3000", "https://nkss-website.up.railway.app", "https://nkss.getpsyched.dev"},
})
log.Fatalln(http.ListenAndServe(":"+os.Getenv("PORT"), c.Handler(s.router)))
}
// setRouters maps endpoints to the functions they must route to.
func (s *server) setRouters() {
s.router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Welcome to NKSS' API!"))
})
// Status
s.router.Handle("/status/student/discord", h.GetDiscordLinkStatus(s.pool)).Methods("GET")
// Announcements
s.router.HandleFunc("/announcements", h.GetAnnouncements(s.pool)).Methods("GET")
// Courses
s.router.HandleFunc("/courses", h.GetCourses(s.pool)).Methods("GET")
s.router.HandleFunc("/courses", m.Authenticator(h.CreateCourse(s.pool))).Methods("POST")
s.router.HandleFunc("/courses/{code}", h.GetCourse(s.pool)).Methods("GET")
// Clubs
s.router.Handle("/clubs", h.GetClubs(s.pool)).Methods("GET")
s.router.Handle("/clubs/{name}", h.GetClub(s.pool)).Methods("GET")
s.router.Handle("/clubs/{name}/faculty", h.GetClubFaculty(s.pool)).Methods("GET")
s.router.Handle("/clubs/{name}/faculty", m.Authenticator(h.CreateClubFaculty(s.pool))).Methods("POST")
s.router.Handle("/clubs/{name}/faculty/{fname}", m.Authenticator(h.DeleteClubFaculty(s.pool))).Methods("DELETE")
s.router.Handle("/clubs/{name}/members", m.Authenticator(h.ReadClubMembers(s.pool))).Methods("GET")
s.router.Handle("/clubs/{name}/members", m.Authenticator(h.CreateClubMemberBulk(s.pool))).Methods("POST")
s.router.Handle("/clubs/{name}/members/{roll}", m.Authenticator(h.CreateClubMember(s.pool))).Methods("POST")
s.router.Handle("/clubs/{name}/members/{roll}", m.Authenticator(h.UpdateClubMember(s.pool))).Methods("PUT")
s.router.Handle("/clubs/{name}/members", m.Authenticator(h.DeleteClubMemberBulk(s.pool))).Methods("DELETE")
s.router.Handle("/clubs/{name}/members/{roll}", m.Authenticator(h.DeleteClubMember(s.pool))).Methods("DELETE")
s.router.Handle("/clubs/{name}/socials", h.GetClubSocials(s.pool)).Methods("GET")
s.router.Handle("/clubs/{name}/socials", m.Authenticator(h.CreateClubSocial(s.pool))).Methods("POST")
s.router.Handle("/clubs/{name}/socials/{type}", m.Authenticator(h.UpdateClubSocials(s.pool))).Methods("PUT")
s.router.Handle("/clubs/{name}/socials/{type}", m.Authenticator(h.DeleteClubSocial(s.pool))).Methods("DELETE")
// Students
s.router.Handle("/hostels", h.GetHostels(s.pool)).Methods("GET")
s.router.Handle("/students/{id}", m.Authenticator(h.GetStudent(s.pool))).Methods("GET")
}