-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
149 lines (129 loc) · 4.1 KB
/
config.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package presentation
import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure"
"github.com/savannahghi/onboarding/pkg/onboarding/usecases"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/savannahghi/onboarding/pkg/onboarding/presentation/graph"
"github.com/savannahghi/onboarding/pkg/onboarding/presentation/graph/generated"
"github.com/savannahghi/onboarding/pkg/onboarding/presentation/rest"
"github.com/savannahghi/serverutils"
log "github.com/sirupsen/logrus"
)
const (
mbBytes = 1048576
serverTimeoutSeconds = 120
)
// AllowedOrigins is list of CORS origins allowed to interact with
// this service
var AllowedOrigins = []string{
"https://healthcloud.co.ke",
"https://bewell.healthcloud.co.ke",
"http://localhost:5000",
}
var allowedHeaders = []string{
"Authorization", "Accept", "Accept-Charset", "Accept-Language",
"Accept-Encoding", "Origin", "Host", "User-Agent", "Content-Length",
"Content-Type",
}
// Router sets up the ginContext router
func Router(ctx context.Context) (*mux.Router, error) {
fc := &firebasetools.FirebaseClient{}
firebaseApp, err := fc.InitFirebase()
if err != nil {
return nil, err
}
infrastructure, err := infrastructure.NewInfrastructureInteractor()
if err != nil {
return nil, err
}
// Initialize base (common) extension
baseExt := extension.NewBaseExtensionImpl()
pinExt := extension.NewPINExtensionImpl()
usecases := usecases.NewUsecasesInteractor(infrastructure, baseExt, pinExt)
h := rest.NewHandlersInterfaces(infrastructure, usecases)
r := mux.NewRouter() // gorilla mux
r.Use(otelmux.Middleware(serverutils.MetricsCollectorService("onboarding")))
r.Use(
handlers.RecoveryHandler(
handlers.PrintRecoveryStack(true),
handlers.RecoveryLogger(log.StandardLogger()),
),
) // recover from panics by writing a HTTP error
r.Use(serverutils.RequestDebugMiddleware())
// Add Middleware that records the metrics for HTTP routes
r.Use(serverutils.CustomHTTPRequestMetricsMiddleware())
SharedRoutes(h, r)
// Graphql route
authR := r.Path("/graphql").Subrouter()
authR.Use(firebasetools.AuthenticationMiddleware(firebaseApp))
authR.Methods(
http.MethodPost,
http.MethodGet,
).HandlerFunc(GQLHandler(ctx, usecases))
return r, nil
}
// PrepareServer starts up a server
func PrepareServer(ctx context.Context, port int, allowedOrigins []string) *http.Server {
// start up the router
r, err := Router(ctx)
if err != nil {
serverutils.LogStartupError(ctx, err)
}
// start the server
addr := fmt.Sprintf(":%d", port)
h := handlers.CompressHandlerLevel(r, gzip.BestCompression)
h = handlers.CORS(
handlers.AllowedHeaders(allowedHeaders),
handlers.AllowedOrigins(allowedOrigins),
handlers.AllowCredentials(),
handlers.AllowedMethods([]string{"OPTIONS", "GET", "POST"}),
)(h)
h = handlers.CombinedLoggingHandler(os.Stdout, h)
h = handlers.ContentTypeHandler(h, "application/json", "application/x-www-form-urlencoded")
srv := &http.Server{
Handler: h,
Addr: addr,
WriteTimeout: serverTimeoutSeconds * time.Second,
ReadTimeout: serverTimeoutSeconds * time.Second,
}
log.Infof("Server running at port %v", addr)
return srv
}
//HealthStatusCheck endpoint to check if the server is working.
func HealthStatusCheck(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(true)
if err != nil {
log.Fatal(err)
}
}
// GQLHandler sets up a GraphQL resolver
func GQLHandler(ctx context.Context,
service usecases.Usecases,
) http.HandlerFunc {
resolver, err := graph.NewResolver(ctx, service)
if err != nil {
serverutils.LogStartupError(ctx, err)
}
server := handler.NewDefaultServer(
generated.NewExecutableSchema(
generated.Config{
Resolvers: resolver,
},
),
)
return func(w http.ResponseWriter, r *http.Request) {
server.ServeHTTP(w, r)
}
}