diff --git a/.gitignore b/.gitignore index f1c181e..1521c8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, build with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out +dist diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..1d812c6 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,8 @@ +ports: +- port: 8080 +tasks: +- command: | + cd .. && + curl -OL https://github.com/goreleaser/goreleaser/releases/download/v0.108.0/goreleaser_Linux_x86_64.tar.gz && + tar xfz goreleaser_Linux_x86_64.tar.gz && + rm goreleaser_Linux_x86_64.tar.gz diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..d23269c --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,29 @@ +# This is an example goreleaser.yaml file with some sane defaults. +# Make sure to check the documentation at http://goreleaser.com +before: + hooks: + # you may remove this if you don't use vgo + - go mod download +builds: +- env: + - CGO_ENABLED=0 + ldflags: + - -s -w -X main.build={{.Version}} +archives: +- replacements: + darwin: Darwin + linux: Linux + windows: Windows + 386: i386 + amd64: x86_64 + format: binary +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..aaf91be --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Christian Weichel + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8879d05 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/32leaves/servr + +go 1.12 + +require ( + github.com/sirupsen/logrus v1.4.2 + github.com/spf13/pflag v1.0.3 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4057e7a --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go new file mode 100644 index 0000000..4efacd7 --- /dev/null +++ b/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + log "github.com/sirupsen/logrus" + "github.com/spf13/pflag" + "net/http" + "net/http/httputil" +) + +var build = "UNKNOWN" + +func main() { + log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) + + port := pflag.StringP("port", "p", "8080", "port to serve on") + dir := pflag.StringP("directory", "d", ".", "the directory to serve") + local := pflag.BoolP("localhost", "l", false, "serve on localhost only") + version := pflag.BoolP("version", "v", false, "prints the version") + dontDump := pflag.BoolP("dont-dump", "N", false, "be less verbose and don't dump requests") + dontServe := pflag.BoolP("dont-serve", "D", false, "don't serve any directy (ignores --directory)") + pflag.Parse() + + if *version { + fmt.Println(build) + return + } + + handler := &debugHandler{DumpRequest: !*dontDump} + if !*dontServe { + log.WithField("directory", *dir).Info("serving files") + handler.Delegate = http.FileServer(http.Dir(*dir)) + } + http.Handle("/", handler) + + addr := ":" + *port + if *local { + addr = "127.0.0.1" + addr + } + + log.WithField("addr", addr).WithField("version", build).Info("server running") + err := http.ListenAndServe(addr, nil) + log.WithError(err).Fatal("cannot serve") +} + +type debugHandler struct { + Delegate http.Handler + DumpRequest bool +} + +func (h *debugHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + dump := fmt.Sprintf("%s %s", req.Method, req.URL) + if h.DumpRequest { + out, err := httputil.DumpRequest(req, true) + if err != nil { + log.WithError(err).Error("cannot dump request") + } else { + dump = string(out) + } + } + log.Info(dump) + + if h.Delegate != nil { + h.Delegate.ServeHTTP(resp, req) + } +}