A simple HTTP server for static websites written from scratch with thread pools.
I decided to write this very simple HTTP server from scratch without using any external packages or the core http library just to learn Go.
The server doesn't need any external packages, just go get
this repo and you are ready to go.
go get github.com/MohamedBassem/servgo
go get github.com/MohamedBassem/servgo/servgo
Using the library is very simple:
server := servgo.NewServer(address, numberOfWorkers, maxQueuedConnecions)
server.SetGetHandler(handleGetRequest)
err := server.Run()
and the handler is a function with the following signature:
func handleGetRequest(req servgo.Request) servgo.Response
You can check the executable's source code for an example: https://github.com/MohamedBassem/servgo/blob/master/servgo/main.go
$ servgo -h
Usage of ./servgo
-addr=":8080": The port to which the server will listen
-max-queued=1024: [Optional] The maximum number of connections that can be queued in the server
-num-workers=2: [Optional] Number of workers serving the requests
-root-dir="nil": [Required] The root dir for serving the files
$ servgo --root-dir example # Will start a server listening to port 8080 and serving files from the example directory
- The server currently supports only
GET
andHEAD
requests. - The server spawns
--num-workers
workers to serve the incoming requests. - If a file is not specified in the request path
index.html
is served. - Server logs are printed to stdout, if you want to log them to a file you can pipe them.