Last active
October 16, 2017 10:32
-
-
Save namsral/dd92a011b9195d25ccf8 to your computer and use it in GitHub Desktop.
Deploy and serve assets from a zip archive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"archive/zip" | |
"flag" | |
"log" | |
"net/http" | |
"golang.org/x/tools/godoc/vfs/httpfs" | |
"golang.org/x/tools/godoc/vfs/zipfs" | |
) | |
func main() { | |
zipPath := flag.String("zip", "assets.zip", "zip file containing assets") | |
httpAddr := flag.String("http", "localhost:6011", "http address") | |
flag.Parse() | |
r, err := zip.OpenReader(*zipPath) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fs := zipfs.New(r, *zipPath) | |
m := http.NewServeMux() | |
m.Handle("/assets/", http.FileServer(httpfs.New(fs))) | |
log.Print("Listening on ", *httpAddr) | |
if err := http.ListenAndServe(*httpAddr, m); err != nil { | |
log.Fatal("ListenAndServe:", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment