-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
159 lines (139 loc) · 5.6 KB
/
http.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
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
filesclient "starter-pack-goa/api"
"starter-pack-goa/config"
authsvr "starter-pack-goa/gen/http/auth/server"
fileapisvr "starter-pack-goa/gen/http/fileapi/server"
filessvr "starter-pack-goa/gen/http/files/server"
jwttokensvr "starter-pack-goa/gen/http/jwt_token/server"
oauthsvr "starter-pack-goa/gen/http/o_auth/server"
openapisvr "starter-pack-goa/gen/http/openapi/server"
userssvr "starter-pack-goa/gen/http/users/server"
"sync"
"time"
goahttp "goa.design/goa/v3/http"
httpmdlwr "goa.design/goa/v3/http/middleware"
"goa.design/goa/v3/middleware"
)
// handleHTTPServer starts configures and starts a HTTP server on the given
// URL. It shuts down the server if any error is received in the error channel.
func handleHTTPServer(ctx context.Context, u *url.URL, api *ApiEndpoints, wg *sync.WaitGroup, errc chan error, logger *log.Logger, debug bool) {
// Get .env
cnf := config.New()
// Setup goa log adapter.
var (
adapter middleware.Logger
)
{
adapter = middleware.NewLogger(logger)
}
// Provide the transport specific request decoder and response encoder.
// The goa http package has built-in support for JSON, XML and gob.
// Other encodings can be used by providing the corresponding functions,
// see goa.design/implement/encoding.
var (
dec = goahttp.RequestDecoder
enc = goahttp.ResponseEncoder
)
// Build the service HTTP request multiplexer and configure it to serve
// HTTP requests to the service endpoints.
var mux goahttp.Muxer
{
mux = goahttp.NewMuxer()
}
// Wrap the endpoints with the transport specific layers. The generated
// server packages contains code generated from the design which maps
// the service input and output data structures to HTTP requests and
// responses.
var filePath string
if cnf.Mode == "PROD" {
filePath = "/go/"
} else {
filePath = "./"
}
var (
eh = errorHandler(logger)
openapiServer *openapisvr.Server = openapisvr.New(nil, mux, dec, enc, nil, nil, http.Dir("gen/http"))
usersServer *userssvr.Server = userssvr.New(api.usersEndpoints, mux, dec, enc, eh, nil)
filesServer *filessvr.Server = filessvr.New(api.filesEndpoints, mux, dec, enc, eh, nil, filesclient.FilesImportFileDecoderFunc)
jwtTokenServer *jwttokensvr.Server = jwttokensvr.New(api.jwtTokenEndpoints, mux, dec, enc, eh, nil)
fileapiServer *fileapisvr.Server = fileapisvr.New(nil, mux, dec, enc, nil, nil, http.Dir(filePath))
oAuthServer *oauthsvr.Server = oauthsvr.New(api.oAuthEndpoints, mux, dec, enc, eh, nil)
authServer *authsvr.Server = authsvr.New(api.authEndpoints, mux, dec, enc, eh, nil)
)
{
if debug {
servers := goahttp.Servers{
usersServer,
fileapiServer,
filesServer,
openapiServer,
jwtTokenServer,
authServer,
oAuthServer,
}
servers.Use(httpmdlwr.Debug(mux, os.Stdout))
}
}
// Configure the mux.
openapisvr.Mount(mux, openapiServer)
userssvr.Mount(mux, usersServer)
fileapisvr.Mount(mux, fileapiServer)
authsvr.Mount(mux, authServer)
filessvr.Mount(mux, filesServer)
jwttokensvr.Mount(mux, jwtTokenServer)
oauthsvr.Mount(mux, oAuthServer)
// Wrap the multiplexer with additional middlewares. Middlewares mounted
// here apply to all the service endpoints.
var handler http.Handler = mux
{
handler = httpmdlwr.Log(adapter)(handler)
handler = httpmdlwr.RequestID()(handler)
}
// Start HTTP server using default configuration, change the code to
// configure the server as required by your service.
var srv *http.Server = &http.Server{Addr: fmt.Sprintf(":%d", cnf.Port), Handler: handler}
logger.Printf(`
██████╗ ██████╗ █████╗ ██╗ ██╗ ██████╗ ███╗ ███╗
██╔════╝ ██╔═══██╗██╔══██╗ ╚██╗██╔╝ ██╔════╝ ████╗ ████║
██║ ███╗██║ ██║███████║ ╚███╔╝ ██║ ███╗██╔████╔██║
██║ ██║██║ ██║██╔══██║ ██╔██╗ ██║ ██║██║╚██╔╝██║
╚██████╔╝╚██████╔╝██║ ██║ ██╔╝ ██╗ ╚██████╔╝██║ ╚═╝ ██║
╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
`)
(*wg).Add(1)
go func() {
defer (*wg).Done()
// Start HTTP server in a separate goroutine.
go func() {
if cnf.SSL {
errc <- srv.ListenAndServeTLS(cnf.Security.Cert, cnf.Security.Key)
} else {
errc <- srv.ListenAndServe()
}
logger.Printf("HTTP server listening on %q", u.Host)
}()
<-ctx.Done()
logger.Printf("shutting down HTTP server at %q", u.Host)
// Shutdown gracefully with a 30s timeout.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
}()
}
// errorHandler returns a function that writes and logs the given error.
// The function also writes and logs the error unique ID so that it's possible
// to correlate.
func errorHandler(logger *log.Logger) func(context.Context, http.ResponseWriter, error) {
return func(ctx context.Context, w http.ResponseWriter, err error) {
id := ctx.Value(middleware.RequestIDKey).(string)
_, _ = w.Write([]byte("[" + id + "] encoding: " + err.Error()))
logger.Printf("[%s] ERROR: %s", id, err.Error())
}
}