-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (71 loc) · 2.79 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
78
79
80
81
82
83
84
85
package main
import (
"context"
"log"
_ "moecods/quiz/docs"
"moecods/quiz/participant"
"moecods/quiz/quiz"
"net/http"
"github.com/gorilla/mux"
httpSwagger "github.com/swaggo/http-swagger"
)
// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8020
// @BasePath /v1
func main() {
client := ConnectToDB()
defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
log.Fatal(err)
}
}()
quizCollection := GetQuizCollection()
quizRepo := quiz.NewQuizRepository(quizCollection)
quizService := *quiz.NewQuizService(quizRepo)
quizHandler := quiz.NewQuizHandler(quizService, quizRepo)
r := mux.NewRouter()
registerSwagger(r)
r.HandleFunc("/v1/quizzes", recoverHandler(quizHandler.GetQuizzesHandler)).Methods(http.MethodGet)
r.HandleFunc("/v1/quizzes", recoverHandler(quizHandler.AddQuizHandler)).Methods(http.MethodPost)
r.HandleFunc("/v1/quizzes/{id}", recoverHandler(quizHandler.UpdateQuizHandler)).Methods(http.MethodPut)
r.HandleFunc("/v1/quizzes/{id}", recoverHandler(quizHandler.DeleteQuizHandler)).Methods(http.MethodDelete)
r.HandleFunc("/v1/quizzes/{id}", recoverHandler(quizHandler.GetQuizHandler)).Methods(http.MethodGet)
participantCollection := GetParticipantCollection()
participantRepo := participant.NewParticipantRepository(participantCollection)
participantHandler := participant.NewParticipantHandler(*participantRepo)
r.HandleFunc("/v1/participants/answers", recoverHandler(participantHandler.SaveParticipantsAnswersHandler)).Methods(http.MethodPost)
r.HandleFunc("/v1/participants/register", recoverHandler(participantHandler.RegisterParticipantsHandler)).Methods(http.MethodPost)
r.HandleFunc("/v1/quizzes/{id}/participants", recoverHandler(participantHandler.GetParticipantsByQuizIDHandler)).Methods(http.MethodGet)
http.Handle("/", r)
err := http.ListenAndServe(":8020", nil)
if err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}
log.Println("Starting server on :8020")
}
func registerSwagger(r *mux.Router) {
r.PathPrefix("/swagger/").Handler(httpSwagger.Handler(
httpSwagger.URL("http://localhost:8020/swagger/doc.json"),
httpSwagger.DeepLinking(true),
httpSwagger.DocExpansion("none"),
httpSwagger.DomID("swagger-ui"),
)).Methods(http.MethodGet)
}
func recoverHandler(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
log.Printf("Recovered from panic: %v", rec)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}()
h(w, r)
}
}