-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (103 loc) · 3.51 KB
/
main.go
File metadata and controls
120 lines (103 loc) · 3.51 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// @title GDSC CTF API
// @version 1.0
// @description GDSC CTF API
// @host localhost:3333
// @BasePath /
package main
import (
"fmt"
"net/http"
"os"
"github.com/GDSC-Phenikaa/ctf-backend/db"
_ "github.com/GDSC-Phenikaa/ctf-backend/docs" // swagger docs
"github.com/GDSC-Phenikaa/ctf-backend/env"
"github.com/GDSC-Phenikaa/ctf-backend/helpers"
"github.com/GDSC-Phenikaa/ctf-backend/middlewares"
"github.com/GDSC-Phenikaa/ctf-backend/models"
"github.com/GDSC-Phenikaa/ctf-backend/routes"
"github.com/GDSC-Phenikaa/ctf-backend/routes/challenges/admin"
"github.com/GDSC-Phenikaa/ctf-backend/routes/challenges/user"
lmsadmin "github.com/GDSC-Phenikaa/ctf-backend/routes/lms/admin"
lmsuser "github.com/GDSC-Phenikaa/ctf-backend/routes/lms/user"
"github.com/GDSC-Phenikaa/ctf-backend/sessions"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joho/godotenv"
httpSwagger "github.com/swaggo/http-swagger"
"gorm.io/gorm"
)
func initialize() (*gorm.DB, error) {
// Initialize the database connection
database, err := db.Connect()
if err != nil {
panic(err)
}
// Ensure the database is migrated
if err := database.AutoMigrate(
&models.User{},
&models.Note{},
&models.Challanges{},
&models.Solves{},
&models.Module{},
&models.Lesson{},
&models.VideoSegment{},
&models.Question{},
&models.QuestionSolve{},
&models.Workspace{},
); err != nil {
panic(err)
}
return database, nil
}
func main() {
helpers.ParseFlags()
_ = godotenv.Load()
if _, err := os.Stat(".env"); err != nil {
helpers.Error("Failed to load .env file: %v", err)
os.Exit(1)
} else {
helpers.Information("Loaded environment variables from .env file")
}
helpers.Information("Starting GDSC CTF API on port %s", env.Port())
sessions.InitSessionStore()
database, err := initialize()
if err != nil {
panic(err)
}
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(helpers.CORSMiddleware)
r.Options("/*", helpers.CORSOptionsHandler)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
if env.IsDebug() {
helpers.Warning("Debug mode is enabled. Swagger documentation is available.")
r.Get("/swagger/*", httpSwagger.WrapHandler)
}
r.Mount("/user", routes.UserRoutes(database))
r.With(middlewares.AuthMiddleware).Get("/profile", routes.ProfileHandler(database))
r.Mount("/admin", admin.AdminRoutes(database))
r.Mount("/admin/lms", lmsadmin.AdminLMSRoutes(database))
r.Mount("/scoreboard", routes.ScoreboardRoutes(database))
r.Mount("/user/challenges", user.UserChallengesRoutes(database))
r.Mount("/user/lms", lmsuser.UserLMSRoutes(database))
r.Mount("/secret", routes.SecretRoutes())
r.Mount("/certificate", routes.CertificateRoutes())
r.Mount("/workspace", routes.WorkspaceRoutes(database))
// Bridge for VNC WebSockets that expect to be at the root.
// Needs AuthMiddleware to read the workspace_token cookie.
r.With(middlewares.AuthMiddleware).HandleFunc("/websockify*", routes.WorkspaceProxy(database))
helpers.Information("Database type: %s", env.DbType())
helpers.Information("Database name: %s", env.DbName())
if env.DbType() == "postgres" {
helpers.Information("Database DSN: %s", env.DbDsn())
}
helpers.Success("API is running on http://localhost:%s", env.Port())
if env.IsDebug() {
helpers.Success("Swagger documentation is available at http://localhost:%s/swagger/index.html", env.Port())
}
http.ListenAndServe(fmt.Sprintf(":%s", env.Port()), r)
}