A simple http server library, build from scratch, using a TCP connection.
Check out /examples for example usage. Here
This project is not suppossed to be efficient, secure, or complete. It's just a simple project to learn go and networking.
- HTTP/1.1 server
- Routing with simple path matching
- Headers and path parameters parsing
- Demarshalling of request JSON body
- Marshalling of response JSON body
To create a server, you need to create a new instance of the HttpServer struct, and call StartHttpServer() method.
func main() {
var server http.HttpServer
server.Port = 8080
server.StartHttpServer()
}
To add request mappings to the server, you can use the http.AddRequestMapping() method, which receives the http server struct, a method, and a function.
http.AddRequestMapping(&server, http.GET, "/", func (request *CustomRequest) http.Result {
// code
})
The first struct argument is a custom struct that contains the request data.
The field named Body will be the body of the request, unmarsheled to a struct, if the request is body a json.
You can get header info and path params from the request using tags.
type CustomRequestBody struct {
Message string `json:"message"`
}
type CustomRequest struct {
Body CustomRequestBody
Header string `header:"header-name"`
Param string `param:"param-name"`
}
The response returned by the function can correspond to an error, or an OK response.
The OK response can contain a deserialized JSON, if created with a struct, or a string.
Example:
type CustomResponseBody struct {
Field string `json:"field"`
}
func getRoot(r *CustomRequest) http.Result {
err := validate(r.Body)
if err != nil {
return http.NewBadRequest("Validation failed")
}
return http.NewOK(CustomResponseBody{"Hello world"})
}
To run the tests, run:
cd src
go test
- TLS
- TCP server from scratch
- Error handling and better concurrency
- Websockets
- Content negotiation (Accepts)
- Static file serving (images, css, html)