-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
102 lines (85 loc) · 2.88 KB
/
serve.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
99
100
101
102
package opts
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/mrexmelle/connect-emp/internal/account"
"github.com/mrexmelle/connect-emp/internal/career"
"github.com/mrexmelle/connect-emp/internal/config"
"github.com/mrexmelle/connect-emp/internal/grading"
"github.com/mrexmelle/connect-emp/internal/localerror"
"github.com/mrexmelle/connect-emp/internal/titling"
"github.com/spf13/cobra"
httpSwagger "github.com/swaggo/http-swagger"
"go.uber.org/dig"
)
func EnableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}
func Serve(cmd *cobra.Command, args []string) {
container := dig.New()
container.Provide(config.NewRepository)
container.Provide(grading.NewRepository)
container.Provide(titling.NewRepository)
container.Provide(account.NewService)
container.Provide(career.NewService)
container.Provide(config.NewService)
container.Provide(grading.NewService)
container.Provide(localerror.NewService)
container.Provide(titling.NewService)
container.Provide(account.NewController)
container.Provide(grading.NewController)
container.Provide(titling.NewController)
process := func(
configService *config.Service,
accountController *account.Controller,
gradingController *grading.Controller,
titlingController *titling.Controller,
) {
r := chi.NewRouter()
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://localhost:3000"},
AllowedMethods: []string{"GET", "PATCH", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
if configService.GetProfile() == "local" {
r.Mount("/swagger", httpSwagger.Handler(
httpSwagger.URL(fmt.Sprintf("http://localhost:%d/swagger/doc.json", configService.GetPort())),
httpSwagger.UIConfig(map[string]string{
"defaultModelsExpandDepth": "-1",
}),
))
}
r.Route("/gradings", func(r chi.Router) {
r.Post("/", gradingController.Post)
r.Get("/{id}", gradingController.Get)
r.Patch("/{id}", gradingController.Patch)
r.Delete("/{id}", gradingController.Delete)
})
r.Route("/titlings", func(r chi.Router) {
r.Post("/", titlingController.Post)
r.Get("/{id}", titlingController.Get)
r.Patch("/{id}", titlingController.Patch)
r.Delete("/{id}", titlingController.Delete)
})
r.Route("/accounts", func(r chi.Router) {
r.Get("/{ehid}/profile", accountController.GetProfile)
r.Get("/{ehid}/career", accountController.GetCareer)
})
err := http.ListenAndServe(fmt.Sprintf(":%d", configService.GetPort()), r)
if err != nil {
panic(err)
}
}
if err := container.Invoke(process); err != nil {
panic(err)
}
}
var ServeCmd = &cobra.Command{
Use: "serve",
Short: "Start connect-emp server",
Run: Serve,
}