From 8ad8b6e6efc9dcdb2ac541741531abfdf252f329 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Mon, 15 Feb 2021 20:56:48 +0100 Subject: [PATCH 1/2] adding file upload support --- simplehttpserver.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/simplehttpserver.go b/simplehttpserver.go index 2e7ad19..1bbb135 100644 --- a/simplehttpserver.go +++ b/simplehttpserver.go @@ -4,15 +4,18 @@ import ( "bytes" "flag" "fmt" + "io/ioutil" "log" "net/http" "net/http/httputil" + "path" ) type options struct { ListenAddress string Folder string Verbose bool + Upload bool } var opts options @@ -20,7 +23,9 @@ var opts options func main() { flag.StringVar(&opts.ListenAddress, "listen", "0.0.0.0:8000", "Address:Port") flag.StringVar(&opts.Folder, "path", ".", "Folder") + flag.BoolVar(&opts.Upload, "upload", false, "Enable upload via PUT") flag.BoolVar(&opts.Verbose, "v", false, "Verbose") + flag.Parse() if flag.NArg() > 0 && opts.Folder == "." { @@ -28,6 +33,9 @@ func main() { } log.Printf("Serving %s on http://%s/...", opts.Folder, opts.ListenAddress) + if opts.Upload { + log.Println("Upload enabled") + } fmt.Println(http.ListenAndServe(opts.ListenAddress, loglayer(http.FileServer(http.Dir(opts.Folder))))) } @@ -37,6 +45,18 @@ func loglayer(handler http.Handler) http.Handler { lrw := newLoggingResponseWriter(w) handler.ServeHTTP(lrw, r) + // Handles file write if enabled + if opts.Upload && r.Method == http.MethodPut { + data, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Println(err) + } + err = handleUpload(path.Base(r.URL.Path), data) + if err != nil { + log.Println(err) + } + } + if opts.Verbose { headers := new(bytes.Buffer) lrw.Header().Write(headers) //nolint @@ -70,3 +90,7 @@ func (lrw *loggingResponseWriter) WriteHeader(code int) { lrw.statusCode = code lrw.ResponseWriter.WriteHeader(code) } + +func handleUpload(file string, data []byte) error { + return ioutil.WriteFile(file, data, 0655) +} From 8c1cc031b82f6459765779a17bbe6a47ac3393fe Mon Sep 17 00:00:00 2001 From: sandeep <8293321+bauthard@users.noreply.github.com> Date: Tue, 16 Feb 2021 01:53:09 +0530 Subject: [PATCH 2/2] Update build.yaml --- .github/workflows/build.yaml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a157f84..008bca6 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -6,27 +6,18 @@ on: pull_request: jobs: - lint: - name: golangci-lint + golangci-lint: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Run golangci-lint - uses: golangci/golangci-lint-action@v2.2.0 + uses: golangci/golangci-lint-action@v2.4.0 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. version: v1.31 args: --timeout 5m - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - # args: --issues-exit-code=0 - - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true build: name: Build runs-on: ubuntu-latest