Post

File Server in GO-Language

File Server is a computer attached to a network that provides a location for shared disk access, i.e storage of computer files such as text, image, sound, video that can be accessed by the workstations that are able to reach the computer that shares the access through a computer network.

Prerequisites

Before we begin , please set up GO environment on your machine. Head over to their official page to install if you have not done so.

Getting Started

Initialise a directory for this file-server project and create a go file named main.go or server.go and start with importing the pacakages from the main as

1
2
3
4
import (
  "fmt"
  "net/http"
)

The packages used here

  • fmt - Package fmt implements formatted I/O with functions analogous to C’s printf and scanf. The format ‘verbs’ are derived from C’s but are simpler.
  • http - Package http provides HTTP client and server implementations.

Create the main function and use the function func http.ListenAndServe(addr string, handler http.Handler) error .

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

1
http.ListenAndServe(":5000", nil)

Check your server is running or not with following commands

1
2
go mod init `module_name`
go run `go_file`

Now open up the web browser and check localhost:5000 is running and showing nothing. 404pagenotfound-image

Now create a directory inside the go project directory like I have named it as public which will contain the files which we will serve over server.

1
2
3
4
5
6
7
8
9
10
11
├── go.mod
├── public
│   ├── about.js
│   ├── cv.pdf
│   ├── diagram.fig
│   ├── form.html
│   ├── gatsby-browser.js
│   ├── gatsby-ssr.js
│   ├── Go in 100 Seconds [446E-r0rXHI].mkv
│   └── iste-orange.png
└── server.go

Declare the dir variable like this

1
dir := http.Dir("./public/")

which will use the http.Dir() that implements FileSystem using the native file system restricted to a specific directory tree.

Also An empty Dir is treated as “.” and we have used “./public”.

Create a variable named dirHandler which will use the FileServer function and takes the above declared dir as input.

1
dirHandler:= http.FileServer(dir)

Lastly call the function http.Handle() which will register the handler for the given pattern in the DefaultServeMux.

1
http.Handle("/", dirHandler)

So this was it, now you can add more lines to your code for making it easy to understand

1
fmt.Printf("FileServer is up and running at port 5000....\n")

Run the server with go run . and open up the web browser, you will see the fileserver is running fileserver-running-image

Tadda!, we have succesfully made the File Server and test if the files are opening up or not. gif

How to send files locally

To set up a sort of quick NAS (Network Attached Storage) system:

  1. Make sure both devices are connected through same network via LAN or WiFi.
  2. Go to the your fileserver directory using cd on Linux or MacOS systems or CD for Windows.
  3. Start your File server with go run .
  4. Open new terminal and type ifconfig on nix or MacOS or ipconfig on Windows and ip address on Linux to find your IP address.

Now on the second device:

Open browser and type in the IP address of the first machine, along with port 5000

http://[ip address]:5000

A page will open showing all the files in the directory being shared from the first computer.

If facing any issue, can refer to the example fileserver at example.

Happy Hacking :”)

Visitor Count

visitor count
This post is licensed under CC BY 4.0 by the author.

Trending Tags