Writing own web-server in Golang

A small Golang web-server I wrote as experiment over weekends.

I wrote a pretty neat web-server in Golang over a few weekends. Here are some details about the experiment.

The first question of course,

Why?

The main aim was to learn system fundamentals better.

The main aim was to learn system fundamentals better alongside learning a language properly, deep enough.

Considering the global trends I chose to go with Golang. As, tools that I am using such as Docker, Kubernetes are already written in it, I planned to contribute some time.

The reason to write a web-server was I am already flattered by Nginx. I wanted to learn inside of it that how the coding part works.

Execution

I started with learning Golang from random tutorials online alongside language docs. Completed 2-3 of them and all of them were text-based. Learning Golang took a while (3-4 weekends).

Then I literally Googled “simple web-server in Golang”. I took that piece of code and ran it. It worked! Then reading docs on functions and packages alongside Google worked well.

Over the weekends I wrote it enough capable to get following features:

  • Reverse Proxy
  • SSL support
  • Serve files based on URL
  • MIME types
  • 301/302 redirection
  • Handle 404s
  • Custom headers
  • Handling multiple domains

It is not production grade of course. But it does its works very well.

I tested it with all the features mentioned above. Yet I was not very confident about using it directly using in production as I might have missed a few cases and might have some vulnerabilities. So I configured it in a reverse proxy setup (with EasyEngine) in a Docker container. It is currently serving my other site h4ck3r.in.

It’s been 5 days now and it is working pretty well.

What next?

My aim was to learn internals better. What I did is used Go Packages and functions provided by them directly as I started to enjoy the top-level coding. I realized it in starting that I am diverting from the main goal but I continued as I was learning Golang too.

The next thing to do is to look into the code of the packages that I used.

And then, I am planning to add more features such as Nginx’s Redis and Proxy modules. Of course an option for a config file too and proper logging.

Code

The code is currently not open-sourced because it is very rough and might have some vulnerabilities.

Screenshots

Output of docker container ls
Golang web-server as x-proxied-for in response headers
404 response page

Yeah I named it Starship.

Comment your thoughts. You can also suggest an alternate name for web-server :]

Leave a Reply

Your email address will not be published. Required fields are marked *