Skip to content

Instantly share code, notes, and snippets.

@paulmach paulmach/serve.go
Last active Nov 19, 2019

Embed
What would you like to do?
Simple Static File Server in Go
/*
Serve is a very simple static file server in go
Usage:
-p="8100": port to serve on
-d=".": the directory of static files to host
Navigating to http://localhost:8100 will display the index.html or directory
listing file.
*/
package main
import (
"flag"
"log"
"net/http"
)
func main() {
port := flag.String("p", "8100", "port to serve on")
directory := flag.String("d", ".", "the directory of static file to host")
flag.Parse()
http.Handle("/", http.FileServer(http.Dir(*directory)))
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
@ghost

This comment has been minimized.

Copy link

ghost commented Aug 16, 2016

thanks

@ctrekker

This comment has been minimized.

Copy link

ctrekker commented Oct 20, 2017

Super helpful!

@gregogalante

This comment has been minimized.

Copy link

gregogalante commented Nov 2, 2017

My code is not working if i try to serve static files from a specific directory and a specific path.

The files structure is:

- serve.go
- images
- - example.jpg

The server handle is:

http.Handle("/images/", http.FileServer(http.Dir("./images")))

With this code the server return always a 404 error when i send a request to: http://localhost:8080/images/example.jpg

What's the problem?

Update

Solved with the usage of http.StripPrefix:

http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("./images"))))
@philippgille

This comment has been minimized.

Copy link

philippgille commented May 6, 2018

Hi @paulmach,

I got inspired by this Gist, added printing a table of local network interfaces and a suggested URL for sharing, added flags, as well as created packages for Scoop (Windows package manager), Homebrew (macOS package manager) and Snap (Linux package manager).

See: https://github.com/philippgille/serve

I didn't add a license yet, because I wanted to ask you first about your consent regarding me creating that full-blown repository based on your Gist.

So: Is it okay for you if I add an Open Source license (haven't decided on one yet - eigher AGPL, MPL or Apache, see https://choosealicense.com/licenses/) to my project and continue working on it?

Kind regards,
Philipp

@paulmach

This comment has been minimized.

Copy link
Owner Author

paulmach commented May 14, 2018

@philippgille Yes it is okay to use this GIST for whatever you want, Open Source or otherwise.

@philippgille

This comment has been minimized.

Copy link

philippgille commented May 15, 2018

@paulmach Thanks a lot!

@95rade

This comment has been minimized.

Copy link

95rade commented Oct 24, 2018

Awesome example of a simplest go web service. thanks for sharing

@bryaakov

This comment has been minimized.

Copy link

bryaakov commented Oct 30, 2018

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.