From ffa14550fdef2fe3f94c3922371a1b5b84d84a60 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Tue, 16 Jul 2019 07:30:33 -0600 Subject: [PATCH] neato --- .gitignore | 2 ++ build.sh | 16 ++++------------ main.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 3d3c2ed..94e2693 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ *.tar simpleserve +*.tar *.sw* **.sw* +server diff --git a/build.sh b/build.sh index 8ab411b..01573f9 100644 --- a/build.sh +++ b/build.sh @@ -1,7 +1,5 @@ #! /bin/bash -ss=simpleserve - function main() { init server @@ -15,15 +13,9 @@ function init() { } function server() { - pushd "$GOPATH/src/local/$ss" - local f="$(gobuild)" - popd - mv "$f" "./$ss" -} - -function gobuild() { - GOOS=linux CGO_ENABLED=0 go build -o /tmp/${PWD##*/} -a -installsuffix cgo >&2 - echo "/tmp/${PWD##*/}" + local f="./server" + GOOS=linux CGO_ENABLED=0 go build -o "$f" -a -installsuffix cgo >&2 + echo "$f" } function gitname() { @@ -35,7 +27,7 @@ function gitname() { } function pack() { - tar -czf "$1.tar" "$ss" "public" + tar -czf "$1.tar" "./server" "./public" echo "$1.tar" } diff --git a/main.go b/main.go new file mode 100644 index 0000000..5323f01 --- /dev/null +++ b/main.go @@ -0,0 +1,34 @@ +/* +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 ( + "local/args" + "log" + "net/http" + "strings" +) + +func main() { + fs := args.NewArgSet() + fs.Append(args.STRING, "p", "port to serve", "8100") + fs.Append(args.STRING, "d", "static path to serve", "./public") + if err := fs.Parse(); err != nil { + panic(err) + } + + d := fs.Get("d").GetString() + p := strings.TrimPrefix(fs.Get("p").GetString(), ":") + + http.Handle("/", http.FileServer(http.Dir(d))) + + log.Printf("Serving %s on HTTP port: %s\n", d, p) + + log.Fatal(http.ListenAndServe(":"+p, nil)) +}