⚠️ This is NOT A PRODUCTION PROJECT. It's a personal project I'm building to practice and become proficient in Go
A minimal HTTP server implemented using Go’s net/http package. Implemented to understand how handlers, middleware, concurrency, and context propagation work in Go.
-
/
: endpoint that is protected by an authentication middleware. It performs no task other than printing a message. -
/stats
: endpoint that returns the request metrics like total requests, successful requests and Unauthorized requests in JSON. -
/work
: endpoint that simulates a CPU bound work with context cancellation. Server stops computation immediately when the client disconnects. It also has an optional query "limit=" to set the number of computations that must be performed.
For examplehttp://localhost:4000/work?limit=1000
will compute the first 1000 prime numbers. -
/upload
: Recieves the file in the POST request form field. Creates a temporary file with a unique name in the./uploads
directory usingos.CreateTemp
and copies the file to it usingio.Copy()
. Renames the temp file using os.Rename() with a safe name created by a helper method in the format./uploads/uuid_originalFileName.extension
.uuid
is a unique ID generated bygoogle/uuid
to maintain unique file names. Performs clean up when the function returns.
-
AuthMiddleware()
: protects the endpoints and returns 401 if the Basic Auth credentials are invalid. -
LoggingMiddleware()
: logs the request's method, path, timestamp and time taken to complete the request. -
StatsMiddleware()
: the outermost middleware that checks the request's status code and updates the in-memory counters usingatomic.AddInt64()
to keep track of the metrics. -
MaxBodySize()
: Limits the incoming request's body size to 50 << 20 bytes in order to prevent DoS
net/http
context
sync/atomic
path/filepath
encoding/json
github.com/google/uuid
strconv
regexp
strings
os
io
time
log
This project is still under development. The README will be updated with new features and endpoints in the coming days.