From 282e902b0aae88f237f2a4de5539120bf904ba1a Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Thu, 3 Oct 2019 12:30:04 -0700 Subject: [PATCH] initial --- .gitignore | 1 + Makefile | 27 +++++++++++++++++++++++++++ main.go | 36 ++++++++++++++++++++++++++++++++++++ server.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.go create mode 100644 server.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fbb154 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +pooteeweet diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..eae3316 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +VERSION := $(shell git rev-parse --short HEAD) +BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') +BUILDUSER := $(shell whoami) +BUILDHOST := $(shell hostname -s) +BUILDARCH := $(shell uname -m) + +GOLDFLAGS += -X main.Version=$(VERSION) +GOLDFLAGS += -X main.Buildtime=$(BUILDTIME) +GOLDFLAGS += -X main.Builduser=$(BUILDUSER)@$(BUILDHOST) +GOLDFLAGS += -X main.Buildarch=$(BUILDARCH) +GOFLAGS = -ldflags "$(GOLDFLAGS)" + +default: run + +run: build + ./pooteeweet + +build: ./pooteeweet + +./pooteeweet: *.go + go build -o $@ $(GOFLAGS) . + +fmt: + go fmt *.go + +test: + docker build -t sneak/pooteeweet . diff --git a/main.go b/main.go new file mode 100644 index 0000000..52fff1e --- /dev/null +++ b/main.go @@ -0,0 +1,36 @@ +//3456789112345676892123456789312345678941234567895123456789612345678971234567898 +package main + +import "github.com/sirupsen/logrus" + +var Version string +var Buildtime string +var Builduser string +var Buildarch string +var log *logrus.Logger + +func main() { + initLogging() + serve() +} + +func initLogging() { + log = logrus.New() + log.SetReportCaller(true) + log.SetLevel(logrus.DebugLevel) + log.SetFormatter(UTCFormatter{&logrus.JSONFormatter{}}) +} + +func serve() { + server := setupHttpServer() + log.Fatal(server.ListenAndServe()) +} + +type UTCFormatter struct { + logrus.Formatter +} + +func (u UTCFormatter) Format(e *logrus.Entry) ([]byte, error) { + e.Time = e.Time.UTC() + return u.Formatter.Format(e) +} diff --git a/server.go b/server.go new file mode 100644 index 0000000..fd5d4cc --- /dev/null +++ b/server.go @@ -0,0 +1,51 @@ +//3456789112345676892123456789312345678941234567895123456789612345678971234567898 +package main + +import "flag" +import "net/http" +import "time" + +import "github.com/gorilla/mux" +import "github.com/justinas/alice" +import "github.com/sirupsen/logrus" + +func LoggerMiddleware(h http.Handler) http.Handler { + return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + startTime := time.Now().UTC() + h.ServeHTTP(writer, request) + log.WithFields(logrus.Fields{ + "method": request.Method, + "path": request.URL.Path, + "duration_ms": time.Since(startTime).Milliseconds(), + }).Info("pageload") + }) +} + +func setupHttpRouter(staticdir string) http.Handler { + r := mux.NewRouter() + + // This will serve files under + // http://localhost:8000/static/ + r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticdir)))) + + return r +} + +func setupHttpServer() *http.Server { + var dir string + + flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir") + flag.Parse() + + router := setupHttpRouter(dir) + + server := alice.New(LoggerMiddleware).Then(router) + + listener := &http.Server{ + Handler: server, + Addr: ":8000", + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + return listener +}