Skip to content

A minimal HTTP server implemented using Go’s net/http package. Implemented to understand how handlers, middlewares, concurrency, and context propagation work in Go.

License

Notifications You must be signed in to change notification settings

aDiThYa-808/golang-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golang-http-server

⚠️ 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.

Features (in the current stage)

Endpoints:

  • /: 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 example http://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 using os.CreateTemp and copies the file to it using io.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 by google/uuid to maintain unique file names. Performs clean up when the function returns.

Middlewares:

  • 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 using atomic.AddInt64() to keep track of the metrics.

  • MaxBodySize(): Limits the incoming request's body size to 50 << 20 bytes in order to prevent DoS

Packages used

  • net/http
  • context
  • sync/atomic
  • path/filepath
  • encoding/json
  • github.com/google/uuid
  • strconv
  • regexp
  • strings
  • os
  • io
  • time
  • log

Important Note !

This project is still under development. The README will be updated with new features and endpoints in the coming days.

About

A minimal HTTP server implemented using Go’s net/http package. Implemented to understand how handlers, middlewares, concurrency, and context propagation work in Go.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages