Home File Server on Golang

Home File server is a system which host files on your computer, on the local network (Wifi) and other devices on the same network can access those file by following the URL

This post will help you build the file server from scratch.
So lets get started
  1. First create a directory which will have your project
  2. In this directory create a folder and name it as publicFolder. This folder will contain all the files to be hosted to the local network
  3. Now, create a file (and name it lets say ubuntuserver.go). 

Now Include the following imports in your go file (ubuntuserver.go)

Import File

"fmt" : fmt package is for simply displaying text
"io/ioutil" :  this is used to Read the directory publicFolder
"net/http" : this will be used for routing
"io" : This will mostly be used for writing the file to response writer

Lets first see the main function

We will be listening to the port 8080 , with Default ServeMux which is indicated by a nil in the parameters of ListenAndServe()

http.HandleFunc() will handle our request at /getFile and will display list of all files inside publicFolder

Now this public folder needs to be served to the user , this is done by http.Handle()

http.Handle() will be called at /getFile/publicFolder/
Now Handle will serve all the file in the particular Folder,
But /getFile/publicFolder/ is not a directory in our case, so we have to provide the directory to be served when user enters /getFile/publicFolder/

http.StripPrefix will strip the string mentioned in the first paramater from the url and http.FileServer() will provide the path of directory which has to be served


Lets get into the detail of how getFile is working


This is the getFile function which is handling the request

Now here we will read every file inside the publicFolder directory and put it in a list inside the html in the form of <a> </a> tags, this forms a list of files urls

Please refer to the file above for better understanding

Now we write this file to the response writer


  1. First place few files inside the public folder
  2. Now run the program with go run ubuntuserver.go
  3. Now in another device on the same network, open the browser and enter the url <hosting device internal ip addresss>:8008/getFile
  4. The Ip address of the device can be found by openging terminal and typing ifconfig
  5. You will see under wlan0 inet address  something like 192.168.1.4, This is your internal url
  6. the Complete url will be 192.168.1.4:8008/getFile
Here is the whole file

Comments