Go service as a web server
By: Petr Jahoda
Tags: Golang, Go, Docker, Tutorial, Programming

Prepare our project

The goal of today’s article is to improve the service to make it a simple web-server service. Service, that will serve CSS, Javascript and HMTL files (and of course you can add fonts, data and other files, as you like).

Create directories

Begin with creating three new directories(folders) in the main project directory. You can see the result on the left side of the screenshot: three new directories: css, html and js.

Add new line to your import block: github.com/julienschmidt/httprouter . And again, press ALT+ENTER and import this module.

Side note: you can use Go’s built-in routing and/or you can use many others routers. I tend to use julienschmidt’s httprouter for three reasons: 1) it is simple to use 2) it is really fast 3) another part has a SSE functionality, which I use a lot (more on that in another article)

Updating the code in main.go

After importing the module we will focus on that run() function. Please delete all the code that you have here from before, and insert two new lines.

First line will create router instance (variable) and the second line will start this web server, running as service, on port 81. You can now test running from Goland, just to make sure, everything is working properly.

You will notice, that serviceIsRunning variable has red underline, because we are not using it now. But we will.

Side note: you can use almost any other number for port. If you omit the number like this http.ListenAndServe(“:”, router) , or even like this http.ListenAndServe(“”, router) , the web server will start on default port 80

To have a better information about starting your webserver service, update the run() function to display an error message, if any.

Here is a screenshot to show you, what you should see, if a port is in use. So, in my code I need to use different port. 82, for example.

To serve files to user, we need to add two other lines. One line is for serving css files from that css directory we created. The second one is serving javascript files.

Maybe you are asking, why we are not adding another line for serving html files. And of course, you can add this line in here, so you can the access all html files in a browser, like http://localhost:81/homepage.html. But we will use different approach, that allows us to modify that html file, before we send it to the browser.

Adding new files

Now create three files: homepage.css, homepage.js and homepage.html. And create them in proper directories. Also create fourth file homepage.go in the main project folder, like you see on the screenshot.

Side note: I prefer this approach (more files, same name, proper directories), because when looking for bugs on a specific page (like homepage.html), I can immediately go for all “partner” files, homepage.go, homepage.css and homepage.js files in this case. And I do not care about any other files, because “by name” they are for another page.

Update homepage.html file, so it looks like below. Add two lines, that imports css and javascript from proper directories, change the title, add two new h1 headings and one button, so we can then test, that everything is working.

Update homepage.css file, just to be sure that the page loads it. Of course, you can play with css like you want. In this example I made it simple like this:

Update homepage.js file, again, to be sure, that it will be loaded and it will work.

Code below will grab for a button with id test-button and link it with a function that will display alert message. This function listens (hence the addEventListener ) for a type click . So when you click, an alert with text Button clicked appears. Plain and simple.

Update main.go file, so finally we can serve that homepage.html to the browser. Update the run() function with new line. To make it simple, is says: when you open http://localhost:81 in the browser, it will call the serveHomepage function.