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.

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 lw. Please make sure, that you instruct your downloader to follow redirections – this is the -L-flag for curl.

curl -s https://api.github.com/repos/feduxorg/local-webserver/releases/latest \
| grep -e "browser_download_url.*linux.*" \
| cut -d : -f 2,3 \
| tr -d \" \
| xargs -I {} sh -c 'curl -L {} | tar -xzf -'

Following make the downloaded file an executable.

chmod +x lw

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

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.

./lw
# => 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 lw to use the given port instead of a random one.

./lw --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 lw should listen on by commandline, you can use --interface <ip address> for that.

./lw --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

A very popular alternative is “Caddy”. They released version “2” a couple of months agao. 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 it, 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 reader's working environment or wherever 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 Imprint' page.