-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (70 loc) · 2.54 KB
/
main.go
File metadata and controls
84 lines (70 loc) · 2.54 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
package main
import (
"database/sql"
"log"
"os"
"github.com/gin-gonic/gin"
_ "github.com/jackc/pgx/v5/stdlib"
"CodeSCE/internal/core/repositories/postgres"
"CodeSCE/internal/core/services"
"CodeSCE/internal/db"
"CodeSCE/internal/handlers"
)
func main() {
dsn := os.Getenv("DATABASE_URL")
if dsn == "" {
log.Fatal("DATABASE_URL environment variable is required")
}
database, err := sql.Open("pgx", dsn)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
defer database.Close()
if err := database.Ping(); err != nil {
log.Fatalf("failed to ping database: %v", err)
}
if err := db.InitSchema(database); err != nil {
log.Fatalf("failed to initialize schema: %v", err)
}
assessmentRepo := postgres.NewAssessmentRepository(database)
questionRepo := postgres.NewQuestionRepository(database)
testCaseRepo := postgres.NewTestCaseRepository(database)
inviteRepo := postgres.NewInviteRepository(database)
attemptRepo := postgres.NewAttemptRepository(database)
answerRepo := postgres.NewAnswerRepository(database)
submissionRepo := postgres.NewSubmissionRepository(database)
handlers.SetAssessmentService(services.NewAssessmentService(assessmentRepo, questionRepo, testCaseRepo))
handlers.SetInviteService(services.NewInviteService(inviteRepo, attemptRepo, assessmentRepo))
handlers.SetAttemptService(services.NewAttemptService(
attemptRepo,
answerRepo,
questionRepo,
submissionRepo,
assessmentRepo,
))
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
api := r.Group("/api")
api.POST("/assessments", handlers.CreateAssessment)
api.GET("/assessments", handlers.ListAssessments)
api.GET("/assessments/:id", handlers.GetAssessment)
api.POST("/assessments/:id/questions", handlers.AddQuestion)
api.POST("/assessments/:id/questions/:qid/test-cases", handlers.AddTestCases)
api.POST("/assessments/:id/invites", handlers.CreateInvite)
api.GET("/invites", handlers.ListInvites)
api.GET("/invites/:token", handlers.ValidateInvite)
api.POST("/invites/:token/start", handlers.StartAttempt)
api.GET("/attempts", handlers.ListAttempts)
api.GET("/attempts/:id", handlers.GetAttempt)
api.GET("/attempts/:id/questions", handlers.GetAttemptQuestions)
api.POST("/attempts/:id/answers", handlers.SaveAnswers)
api.POST("/attempts/:id/submit", handlers.SubmitAttempt)
api.POST("/attempts/:id/questions/:qid/submissions", handlers.CreateSubmission)
api.GET("/submissions/:id", handlers.GetSubmission)
api.POST("/attempts/:id/questions/:qid/run", handlers.RunCode)
r.Run(":6767")
}