package main import ( "archive/zip" "fmt" "github.com/pkg/browser" "io" "net/http" "os" "path/filepath" "regexp" ) func rootHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, index) } func uploadHandler(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(32 << 20) file, handler, err := r.FormFile("artifact") if err != nil { fmt.Println(err) return } defer file.Close() os.RemoveAll("tmp") os.Mkdir("tmp", 0755) filename := filepath.Join("tmp", handler.Filename) f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { fmt.Println(err) return } defer f.Close() io.Copy(f, file) err = Unzip(filename, filepath.Join("tmp", "serve")) if err != nil { panic(err) } var vizpath string filepath.Walk("tmp", func(path string, f os.FileInfo, _ error) error { if !f.IsDir() { r, err := regexp.MatchString("index.", f.Name()) if err == nil && r { vizpath = path } } return nil }) http.Redirect(w, r, vizpath, 301) } func main() { http.HandleFunc("/", rootHandler) http.HandleFunc("/upload", uploadHandler) http.Handle("/tmp/", http.StripPrefix("/tmp", http.FileServer(http.Dir("tmp")))) err := browser.OpenURL("http://localhost:8282") if err != nil { panic(err) } http.ListenAndServe(":8282", nil) } // Modified from http://stackoverflow.com/a/24792688 func Unzip(src, dest string) error { r, err := zip.OpenReader(src) if err != nil { return err } defer func() { if err := r.Close(); err != nil { panic(err) } }() os.MkdirAll(dest, 0755) // Closure to address file descriptors issue with all the deferred .Close() methods extractAndWriteFile := func(f *zip.File) error { rc, err := f.Open() if err != nil { return err } defer func() { if err := rc.Close(); err != nil { panic(err) } }() path := filepath.Join(dest, f.Name) if f.FileInfo().IsDir() { os.MkdirAll(path, 0755) } else { os.MkdirAll(filepath.Dir(path), 0755) f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return err } defer func() { if err := f.Close(); err != nil { panic(err) } }() _, err = io.Copy(f, rc) if err != nil { return err } } return nil } for _, f := range r.File { err := extractAndWriteFile(f) if err != nil { return err } } return nil } var index string = `