Skip to content

Instantly share code, notes, and snippets.

@sosedoff sosedoff/static_server.go
Last active Dec 25, 2015

Embed
What would you like to do?
Static server for html development.
package main
import(
"os"
"fmt"
"net/http"
"github.com/jessevdk/go-flags"
)
type ServerOptions struct {
Port string `short:"b" long:"bind" description:"Server port (5000)"`
Path string `short:"p" long:"path" description:"Assets directory (current)"`
}
func main() {
opts := ServerOptions {}
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
fmt.Println("Error", err)
os.Exit(1)
}
if len(opts.Port) == 0 {
opts.Port = "5000"
}
if len(opts.Path) == 0 {
opts.Path, _ = os.Getwd()
}
bind := fmt.Sprintf(":%s", opts.Port)
http.Handle("/", http.FileServer(http.Dir(opts.Path)))
fmt.Printf("Starting server http://0.0.0.0:%s in %s\n", opts.Port, opts.Path)
err = http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
}
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.