Skip to content

Instantly share code, notes, and snippets.

@regmicmahesh
Last active January 23, 2021 05:33
Show Gist options
  • Save regmicmahesh/f2953e20abff11febb7f815fc16c922e to your computer and use it in GitHub Desktop.
Save regmicmahesh/f2953e20abff11febb7f815fc16c922e to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"log"
"net/http"
"os"
)
var folderToServe string
func main() {
cwd, err := os.Getwd()
if err != nil {
log.Fatalln("Failed to get your current working directory")
}
flag.StringVar(&folderToServe, "f", cwd, "The directory to serve with server.")
flag.Parse()
dir := http.Dir(folderToServe)
log.Println("Serving File from", folderToServe)
fileServerHandler := http.FileServer(dir)
log.Println("Server running at Port 8080")
must(http.ListenAndServe(":8080", fileServerHandler), "Failed to Bind Server")
}
func must(err error, msg string) {
if err != nil {
log.Fatalln(msg)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment