Skip to content

Instantly share code, notes, and snippets.

@crazyoptimist
Forked from paulmach/serve.go
Created September 14, 2021 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyoptimist/0f503f8a9528bd0c02612ebd9055c5e8 to your computer and use it in GitHub Desktop.
Save crazyoptimist/0f503f8a9528bd0c02612ebd9055c5e8 to your computer and use it in GitHub Desktop.
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))
}
@crazyoptimist
Copy link
Author

crazyoptimist commented Sep 16, 2021

Using Go-Gin:

package main

import (
        "github.com/gin-contrib/static"
        "github.com/gin-gonic/gin"
)

func main() {
        router := gin.Default()

        router.Use(static.Serve("/", static.LocalFile("/mnt/d/Movies", true)))

        router.Run("0.0.0.0:9000")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment