Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Weichel committed Jun 7, 2019
1 parent a66ea02 commit 08d57c6
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 12 deletions.
13 changes: 1 addition & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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:'
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
66 changes: 66 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 08d57c6

Please sign in to comment.