github.com/go-swagger/go-swagger@v0.31.0/examples/file-server/restapi/configure_file_upload.go (about) 1 // This file is safe to edit. Once it exists it will not be overwritten 2 3 package restapi 4 5 import ( 6 "crypto/tls" 7 "fmt" 8 "io" 9 "log" 10 "net/http" 11 "os" 12 "path" 13 14 "github.com/go-openapi/errors" 15 "github.com/go-openapi/runtime" 16 "github.com/go-openapi/runtime/middleware" 17 18 "github.com/go-swagger/go-swagger/examples/file-server/restapi/operations" 19 "github.com/go-swagger/go-swagger/examples/file-server/restapi/operations/uploads" 20 ) 21 22 //go:generate swagger generate server --target ../../file-server --name FileUpload --spec ../swagger.yml --principal interface{} 23 24 func configureFlags(api *operations.FileUploadAPI) { 25 // api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... } 26 } 27 28 func configureAPI(api *operations.FileUploadAPI) http.Handler { 29 // configure the api here 30 api.ServeError = errors.ServeError 31 32 // Set your custom logger if needed. Default one is log.Printf 33 // Expected interface func(string, ...interface{}) 34 // 35 // Example: 36 // api.Logger = log.Printf 37 38 api.UseSwaggerUI() 39 // To continue using redoc as your UI, uncomment the following line 40 // api.UseRedoc() 41 42 api.JSONConsumer = runtime.JSONConsumer() 43 api.MultipartformConsumer = runtime.DiscardConsumer 44 45 api.JSONProducer = runtime.JSONProducer() 46 47 // You may change here the memory limit for this multipart form parser. Below is the default (32 MB). 48 // uploads.UploadFileMaxParseMemory = 32 << 20 49 50 uploadFolder, err := os.MkdirTemp(".", "upload") 51 if err != nil { 52 panic("could not create upload folder") 53 } 54 uploadCounter := 0 55 56 api.UploadsUploadFileHandler = uploads.UploadFileHandlerFunc(func(params uploads.UploadFileParams) middleware.Responder { 57 58 if params.File == nil { 59 return middleware.Error(404, fmt.Errorf("no file provided")) 60 } 61 defer func() { 62 _ = params.File.Close() 63 }() 64 65 if namedFile, ok := params.File.(*runtime.File); ok { 66 log.Printf("received file name: %s", namedFile.Header.Filename) 67 log.Printf("received file size: %d", namedFile.Header.Size) 68 } 69 70 // uploads file and save it locally 71 filename := path.Join(uploadFolder, fmt.Sprintf("uploaded_file_%d.dat", uploadCounter)) 72 uploadCounter++ 73 f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) 74 if err != nil { 75 return middleware.Error(500, fmt.Errorf("could not create file on server")) 76 } 77 78 n, err := io.Copy(f, params.File) 79 if err != nil { 80 return middleware.Error(500, fmt.Errorf("could not upload file on server")) 81 } 82 83 log.Printf("copied bytes %d", n) 84 85 log.Printf("file uploaded copied as %s", filename) 86 87 return uploads.NewUploadFileOK() 88 }) 89 90 api.PreServerShutdown = func() {} 91 92 api.ServerShutdown = func() {} 93 94 return setupGlobalMiddleware(api.Serve(setupMiddlewares)) 95 } 96 97 // The TLS configuration before HTTPS server starts. 98 func configureTLS(tlsConfig *tls.Config) { 99 // Make all necessary changes to the TLS configuration here. 100 } 101 102 // As soon as server is initialized but not run yet, this function will be called. 103 // If you need to modify a config, store server instance to stop it individually later, this is the place. 104 // This function can be called multiple times, depending on the number of serving schemes. 105 // scheme value will be set accordingly: "http", "https" or "unix". 106 func configureServer(s *http.Server, scheme, addr string) { 107 } 108 109 // The middleware configuration is for the handler executors. These do not apply to the swagger.json document. 110 // The middleware executes after routing but before authentication, binding and validation. 111 func setupMiddlewares(handler http.Handler) http.Handler { 112 return handler 113 } 114 115 // The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document. 116 // So this is a good place to plug in a panic handling middleware, logging and metrics. 117 func setupGlobalMiddleware(handler http.Handler) http.Handler { 118 return handler 119 }