Advertisement

If you need to share files from your local system with others, you’ve got plenty of options. For some of them you need a client installed locally and an account at a remote website. Fortunately there are easier options available as most programming languages come with an HTTP server. In most cases it’s able to serve files from a local directory and is sufficient for a lot use cases. In this article I am going to show you, how I solved the problem by “writing” a local web server in Go running on Linux, Windows and Mac OS X.

Preface

It’s quite common these days to share data from your local computer with others.

  • Students working on the same project
  • Developers distributing data for their test suites with colleagues
  • Presentators sharing their presentation with the audience
  • IT crowds sharing large data files on demand with others
  • And so on …

The application I’m going to show you is called local_webserver and is implemented in Go. It was originally written to be part of a packaged presentation given away to the audience after a talk. Such a presentation would have been created with middleman-presentation – a Rubygem to build presentations based on HTML and JavaScript powered by middleman and reveal.js.

The main reason why I chose Go for this project is its ability to produce statically linked binaries for all major operating systems – Linux, Windows, Mac OS X. I wanted to have a single binary for each supported platform the user could start from shell or clicking on it by using a graphical interface. And naturally I was a little bit curious, because this was my first Go-project.

For this article I’m going to concentrate on the following workflow – see Fig 1.

Share local files

Fig. 1: Share local files with your colleagues.

Getting started

Create working directory

First, create a directory named “www” wherever you want and make it your current working directory.

# Create directory
mkdir -p www

# Make "www" your current working directory
cd www

Create files

Now create some files in the directory “www”.

touch test1.txt
touch test2.txt
echo "Hello World" > test3.txt

Pull down pre-compiled webserver

After that, pull down one of the pre-compiled binaries from here matching your operating system and store it in the previously created directory as local_webserver. Please make sure, that you instruct your downloader to follow redirections – this is the -L-flag for curl.

# Example for Linux on 64bit hardware
curl -L -o local_webserver https://github.com/fedux-org/data/raw/gh-pages/downloads/local_webserver/server.linux.amd64
# =>   % Total   %  Recv % Xferd Average Speed    Time    Time     Time Current
# =>                              Dload  Upload   Total   Spent    Left   Speed
# => 100   168 100   168 0     0   258      0  --:--:-- --:--:-- --:--:--   258
# => 100 1957k 100 1957k 0     0  144k      0   0:00:13  0:00:13 --:--:--  146k

Following make the downloaded file an executable.

chmod +x local_webserver

If you prefer to build the application on your own, please head over to Github and read the README.

Verify the checksums

Before running the executable file, please verify the checksum of the file. All checksums can be found online.

echo "21d791b1fa4de77d5c92f34f7d17aa6b40bee455d11a171d6a23e28aa2e76f7b  local_webserver" | sha256sum -c
# => local_webserver: OK

Run the webserver

With defaults

Without any parameters given, it will asked you for the network interface it should listen on. By default it chooses the interface listening on 127.0.0.1 and uses a random port. It also opens a new tab in your default browser showing the content of the directory it will serve.

./local_webserver
# => Available Interfaces
# => [ 1]               docker0: 172.17.42.1
# => [ 2]*                   lo: 127.0.0.1
# => [ 3]                    lo: ::1
# => Enter Number [1-3] (Timeout: 7s, Default: 2, Press Enter for Default):
# => Open browser with http://127.0.0.1:56521/index.html
# => Server listens on 127.0.0.1:56521
# => 
# => Requests:
# => 127.0.0.1 - - [19/Sep/2015:19:34:48 +0200] "GET /index.html HTTP/1.1" 301 0 "" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36"

Use a fixed port

Using the --port-parameter you can tell local_webserver to use the given port instead of a random one.

./local_webserver --port 12345
# => Available Interfaces
# => [ 1]               docker0: 172.17.42.1
# => [ 2]*                   lo: 127.0.0.1
# => [ 3]                    lo: ::1
# => Enter Number [1-3] (Timeout: 7s, Default: 2, Press Enter for Default):
# => Open browser with http://127.0.0.1:12345/index.html
# => Server listens on 127.0.0.1:12345

Use a fixed network interface

If you prefer to set the network interface local_webserver should listen on by commandline, you can use --interface <ip address> for that.

./local_webserver --interface 127.0.0.2
# => Open browser with http://127.0.0.2:10998/index.html
# => Server listens on 127.0.0.2:10998

Alternatives

Another local web server is darkhttpd. If you’re free to serve a local directory with a solution implemented in Ruby, you might also want to read this article of mine.

Conclusion

Having a single binary makes it easy to serve files in read only-mode only on demand. I hope you find local_webserver useful.

Discussion

If you found a mistake in this article or would like to contribute some content to this article, please file an issue in this Git Repository

Disclaimer

The contents of this article are put together to the best of the authors' knowledge, but it cannot be guaranteed that it's always accurate in any environment. It is up to the reader to make sure, that all information found in this article, does not do any damage to the readers's working environment or whereever this information is applied to. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, arising from, out of or in connection with this article. Please also note the information given on the Licenses' page.

Related Articles