-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
97 lines (79 loc) · 2.68 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
package main
import (
"fmt"
"os"
docs "github.com/DislikesSchool/EduPage2-server/docs"
"github.com/DislikesSchool/EduPage2-server/edupage"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/robfig/cron/v3"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
nrgin "github.com/newrelic/go-agent/v3/integrations/nrgin"
)
type ClientData struct {
CrJobId cron.EntryID
Client *edupage.EdupageClient
}
var clients = make(map[string]*ClientData)
var cr *cron.Cron
// @title EduPage2 API
// @version 1.0
// @description This is the backend for the EduPage2 app.
// @BasePath /
// @SecurityDefinition Bearer
// @Description JWT authorization token
// @Type apiKey
// @In header
// @Name Authorization
func main() {
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://9f278010f63fd37cc43bee40a6d69aa6@o4504950085976064.ingest.sentry.io/4505752992743424",
EnableTracing: false,
TracesSampleRate: 1.0,
}); err != nil {
fmt.Printf("Sentry initialization failed: %v", err)
}
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("EduPage2-server"),
newrelic.ConfigLicense("eu01xx4155dca9b59d668e2cc9fc4e98FFFFNRAL"),
newrelic.ConfigAppLogForwardingEnabled(true),
)
if err != nil {
fmt.Println("NewRelic initialization failed:", err)
}
key := os.Getenv("JWT_SECRET_KEY")
if key == "" && gin.Mode() == gin.ReleaseMode {
fmt.Println("\033[0;31mERROR\033[0m: No JWT_SECRET_KEY environment variable found. Use the JWT_SECRET_KEY environment variable to set the secret key.")
panic("No JWT_SECRET_KEY environment variable found")
}
cr = cron.New()
router := gin.Default()
router.Use(nrgin.Middleware(app))
api := router.Group("/api")
api.Use(authMiddleware())
docs.SwaggerInfo.BasePath = "/"
router.Use(sentrygin.New(sentrygin.Options{}))
router.POST("/login", LoginHandler)
router.GET("/validate-token", ValidateTokenHandler)
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
api.GET("/timeline", TimelineHandler)
api.GET("/timeline/recent", RecentTimelineHandler)
api.GET("/timetable", TimetableHandler)
api.GET("/timetable/recent", RecentTimetableHangler)
api.GET("/subject/:id", SubjectHandler)
api.GET("/teacher/:id", TeacherHandler)
api.GET("/classroom/:id", ClassroomHandler)
api.GET("/periods", PeriodsHandler)
api.GET("/timelineitem/:id", TimelineItemHandler)
api.GET("/recipients", RecipientsHandler)
api.POST("/message", SendMessageHandler)
router.POST("/icanteen", ICanteenHandler)
router.GET("/test", func(c *gin.Context) {
c.Status(200)
})
cr.Start()
router.Run()
}