-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
264 lines (216 loc) · 8.81 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync/atomic"
"time"
)
// key defines a type for context keys used in the application.
type key int
const (
requestIDKey key = 0 // requestIDKey is used to store the request ID in the context.
)
var (
config Config // config holds the configuration settings for the application.
logger *log.Logger // logger is the default logger used.
healthy int32 // healthy indicates the health status of the application.
)
// mustInitialize sets up the configuration and performs necessary checks.
// Logs any errors and exits if encountered.
func mustInitialize() {
logger = log.New(os.Stdout, "http: ", log.LstdFlags)
config = newConfig()
fi, err := os.Stat(config.dir)
if err != nil {
logger.Fatalf("Error checking configured directory: %v", err)
}
if !fi.IsDir() {
logger.Fatalf("Configured path is not a directory: %s", config.dir)
}
}
func main() {
mustInitialize()
logger.Printf("Initialization completed successfully; Server config: %s", config)
srv := newServer(logger, config, nil)
httpServer := &http.Server{
Addr: config.listenAddr,
Handler: srv,
ErrorLog: logger,
ReadTimeout: config.readTimeout,
WriteTimeout: config.writeTimeout,
IdleTimeout: config.idleTimeout,
}
ctx := context.Background()
run(ctx, logger, httpServer)
}
// Config holds the configuration settings for the application.
type Config struct {
dir string // dir is the directory where files are saved.
listenAddr string // listenAddr on which the server listens.
formUploadField string // formUploadField is the name of the form field used for file uploads.
uploadEndpoint string // uploadEndpoint is the path the to file upload endpoint.
maxInMemorySize int64 // maxInMemorySize bytes of the file parts are stored in memory, with the remainder stored on disk in temporary files.
readTimeout time.Duration // readTimeout is the timeout value for reading the request
writeTimeout time.Duration // writeTimeout is the timeout value for writing the response
idleTimeout time.Duration // idleTimeout is the timeout for keeping idle connections
}
func (c Config) String() string {
return fmt.Sprintf(
"Config{dir: %s, listenAddr: %s, formUploadField: %s, uploadEndpoint: %s, maxInMemorySize: %dB, readTimeout: %v, writeTimeout: %v, idleTimeout: %v}",
c.dir, c.listenAddr, c.formUploadField, c.uploadEndpoint, c.maxInMemorySize, c.readTimeout, c.writeTimeout, c.idleTimeout,
)
}
// newConfig parses command-line flags and returns a Config instance.
func newConfig() Config {
c := Config{}
flag.StringVar(&c.dir, "dir", "/tmp", "A path to the directory where files are saved to (default: '/tmp').")
flag.StringVar(&c.listenAddr, "listen-addr", ":3000", "Address for the server to listen on, in the form 'host:port'. (default: ':3000').")
flag.StringVar(&c.formUploadField, "form-field", "upload", "The name of the form field used for file uploads (default: 'upload').")
flag.StringVar(&c.uploadEndpoint, "upload-endpoint", "/upload", "The path to the upload API endpoint (default: '/upload').")
flag.Int64Var(&c.maxInMemorySize, "max-size", 10, "The maximum memory size (in megabytes) for storing part files in memory (default: 10).")
flag.DurationVar(&c.readTimeout, "read-timeout", 15*time.Second, "Timeout for reading the request (default: '15s').")
flag.DurationVar(&c.writeTimeout, "write-timeout", 15*time.Second, "Timeout for writing the response (default: '15s').")
flag.DurationVar(&c.idleTimeout, "idle-timeout", 60*time.Second, "Timeout for keeping idle connections (default: '60s').")
flag.Parse()
c.maxInMemorySize <<= 20 // convert to MB
return c
}
// newServer creates a new HTTP server with middleware.
func newServer(logger *log.Logger, config Config, nextRequestID RequestIDFunc) http.Handler {
mux := http.NewServeMux()
addRoutes(mux, config)
var handler http.Handler = mux
handler = NewLoggingMiddleware(logger)(handler)
handler = NewTracingMiddleware(nextRequestID)(handler)
return handler
}
// addRoutes configures the routes for the HTTP server.
func addRoutes(mux *http.ServeMux, config Config) {
mux.Handle("/", http.NotFoundHandler())
mux.Handle("/healthz", healthz())
mux.Handle(config.uploadEndpoint, upload(config.dir, config.formUploadField, config.maxInMemorySize))
}
// healthz returns an HTTP handler that checks the health status of the application.
// It responds with 200 OK if the application is healthy, and 503 Service Unavailable otherwise.
func healthz() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&healthy) == 1 {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
})
}
// upload handles file uploads from multipart forms.
func upload(baseDir, formFileFieldName string, maxFileSize int64) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
err := r.ParseMultipartForm(maxFileSize)
if err != nil {
logger.Printf("Error parsing multipart form: %v", err)
http.Error(w, "Could not parse multipart form", http.StatusBadRequest)
return
}
file, handler, err := r.FormFile(formFileFieldName)
if err != nil {
logger.Printf("Error retrieving file from form: %v", err)
http.Error(w, "Could not get file from form", http.StatusBadRequest)
return
}
defer file.Close()
// Create a new file in the uploads directory
path := filepath.Join(baseDir, handler.Filename)
dst, err := os.Create(path)
if err != nil {
logger.Printf("Error creating file on disk: %v", err)
http.Error(w, "Could not create file on disk", http.StatusInternalServerError)
return
}
defer dst.Close()
// Copy the uploaded file to the new file
_, err = io.Copy(dst, file)
if err != nil {
log.Printf("Error saving file: %v", err)
http.Error(w, "Could not save file", http.StatusInternalServerError)
return
}
logger.Printf("File uploaded successfully: %s\n", handler.Filename)
fmt.Fprintf(w, "File uploaded successfully: %s\n", handler.Filename)
})
}
// Middleware is a function that wraps [http.Handler]s
// proving functionality before or/and after execution
// of the h handler.
type Middleware func(h http.Handler) http.Handler
// NewLoggingMiddleware creates a middleware that logs HTTP requests.
func NewLoggingMiddleware(logger *log.Logger) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func(start time.Time) {
elapsed := time.Since(start)
requestID, ok := r.Context().Value(requestIDKey).(string)
if !ok {
requestID = "unknown"
}
logger.Println(requestID, r.Method, r.URL.Path, elapsed, r.RemoteAddr, r.UserAgent())
}(time.Now())
next.ServeHTTP(w, r)
})
}
}
// RequestIDFunc is a function type for generating unique request IDs,
// used in the tracing middleware [NewTracingMiddleware].
type RequestIDFunc func() string
// defaultRequestIDFunc generates a unique request ID based on the current time.
// It is the default [RequestIDFunc] used if none is provided for the tracing middleware.
func defaultRequestIDFunc() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
// NewTracingMiddleware creates a middleware that sets and
// propagates a request ID through the request context and response header.
func NewTracingMiddleware(requestIDFunc RequestIDFunc) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestID := r.Header.Get("X-Request-Id")
if len(requestID) == 0 {
if requestIDFunc != nil {
requestID = requestIDFunc()
} else {
requestID = defaultRequestIDFunc()
}
}
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
w.Header().Set("X-Request-Id", requestID)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// run starts the HTTP server and handles graceful shutdown.
func run(ctx context.Context, logger *log.Logger, httpServer *http.Server) {
go func() {
logger.Printf("listening on %s\n", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "error listening and serving: %s\n", err)
}
}()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
<-ctx.Done()
logger.Println("Shutting down gracefully, press Ctrl+C again to force")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
fmt.Fprintf(os.Stderr, "error shutting down http server: %s\n", err)
}
logger.Println("Server shut down successfully")
}