Created
June 22, 2017 16:35
-
-
Save wingyplus/c8e73a4dbdff78ee3516c9acccbd8719 to your computer and use it in GitHub Desktop.
How to upload file with go-json-rest
This file contains hidden or 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 ( | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"github.com/ant0ine/go-json-rest/rest" | |
) | |
func main() { | |
api := rest.NewApi() | |
api.Use(append(rest.DefaultCommonStack, &rest.AccessLogApacheMiddleware{})...) | |
router, _ := rest.MakeRouter( | |
rest.Post("/upload", func(w rest.ResponseWriter, r *rest.Request) { | |
// set maximum file size | |
r.ParseMultipartForm(32 << 20) | |
file, _, err := r.FormFile("file") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// TODO: change it to open file > copy > close it | |
io.Copy(os.Stdout, file) | |
}), | |
) | |
api.SetApp(router) | |
http.ListenAndServe(":9090", api.MakeHandler()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment