From 91e01403f8680102d49b361dcf1028cb1fe2d68c Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Tue, 29 Oct 2019 10:05:18 -0700 Subject: [PATCH] move into module --- .gitignore | 2 +- Dockerfile | 4 +-- Makefile | 8 +++--- main.go | 44 ++++------------------------- merp/main.go | 50 +++++++++++++++++++++++++++++++++ merp.go => merp/merp.go | 4 +-- {models => merp/models}/db.go | 0 {models => merp/models}/merp.go | 0 server.go => merp/server.go | 2 +- 9 files changed, 66 insertions(+), 48 deletions(-) create mode 100644 merp/main.go rename merp.go => merp/merp.go (98%) rename {models => merp/models}/db.go (100%) rename {models => merp/models}/merp.go (100%) rename server.go => merp/server.go (98%) diff --git a/.gitignore b/.gitignore index 9d7d247..ef1abef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -merp +merpd prod-secrets.yaml diff --git a/Dockerfile b/Dockerfile index 5407dc8..c835d3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,12 +10,12 @@ RUN tar cvfz go-src.tgz src && du -sh * FROM alpine -COPY --from=builder /go/src/github.com/sneak/merp/merp /bin/merp +COPY --from=builder /go/src/github.com/sneak/merp/merpd /bin/merpd # put the source in there too for safekeeping COPY --from=builder /go/go-src.tgz /usr/local/src/go-src.tgz #COPY --from=builder /go/src /usr/local/src/go -CMD /bin/merp +CMD /bin/merpd # FIXME add testing diff --git a/Makefile b/Makefile index c4ce2cd..7624b4c 100644 --- a/Makefile +++ b/Makefile @@ -29,15 +29,15 @@ endif default: run run: build - DEBUG=1 PORT=1111 ./$(APPNAME) + DEBUG=1 PORT=1111 ./$(APPNAME)d -build: ./$(APPNAME) +build: ./$(APPNAME)d -./$(APPNAME): *.go models/*.go +./$(APPNAME)d: *.go merp/*.go merp/models/*.go go build -o $@ $(GOFLAGS) . clean: - rm $(APPNAME) + rm $(APPNAME)d fmt: go fmt *.go diff --git a/main.go b/main.go index 81189a1..762f9e6 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,7 @@ //3456789112345676892123456789312345678941234567895123456789612345678971234567898 package main -import "os" -import "time" -import "github.com/rs/zerolog" -import "github.com/rs/zerolog/log" -import "golang.org/x/crypto/ssh/terminal" +import "github.com/sneak/merp/merp" var Version string var Buildtime string @@ -14,37 +10,9 @@ var Buildarch string var Appname string func main() { - initLogging() - identify() - serve() -} - -func identify() { - log.Info(). - Str("app", Appname). - Str("version", Version). - Str("buildarch", Buildarch). - Str("buildtime", Buildtime). - Str("builduser", Builduser). - Msg("starting") -} - -func initLogging() { - - // always log in UTC - zerolog.TimestampFunc = func() time.Time { - return time.Now().UTC() - } - - log.Logger = log.With().Caller().Stack().Logger() - - if terminal.IsTerminal(int(os.Stdout.Fd())) { - output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339Nano} - log.Logger = zerolog.New(output).With().Caller().Stack().Logger().With().Timestamp().Logger() - } - - zerolog.SetGlobalLevel(zerolog.InfoLevel) - if os.Getenv("DEBUG") != "" { - zerolog.SetGlobalLevel(zerolog.DebugLevel) - } + merp.Version = Version + merp.Buildtime = Buildtime + merp.Builduser = Builduser + merp.Buildarch = Buildarch + merp.Start() } diff --git a/merp/main.go b/merp/main.go new file mode 100644 index 0000000..c916144 --- /dev/null +++ b/merp/main.go @@ -0,0 +1,50 @@ +//3456789112345676892123456789312345678941234567895123456789612345678971234567898 +package merp + +import "os" +import "time" +import "github.com/rs/zerolog" +import "github.com/rs/zerolog/log" +import "golang.org/x/crypto/ssh/terminal" + +var Version string +var Buildtime string +var Builduser string +var Buildarch string +var Appname string + +func Start() { + initLogging() + identify() + serve() +} + +func identify() { + log.Info(). + Str("app", Appname). + Str("version", Version). + Str("buildarch", Buildarch). + Str("buildtime", Buildtime). + Str("builduser", Builduser). + Msg("starting") +} + +func initLogging() { + + // always log in UTC + zerolog.TimestampFunc = func() time.Time { + return time.Now().UTC() + } + + log.Logger = log.With().Caller().Stack().Logger() + + if terminal.IsTerminal(int(os.Stdout.Fd())) { + output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339Nano} + log.Logger = zerolog.New(output).With().Caller().Stack().Logger().With().Timestamp().Logger() + } + + zerolog.SetGlobalLevel(zerolog.InfoLevel) + if os.Getenv("DEBUG") != "" { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } +} diff --git a/merp.go b/merp/merp.go similarity index 98% rename from merp.go rename to merp/merp.go index 4cecd3f..2828e41 100644 --- a/merp.go +++ b/merp/merp.go @@ -1,4 +1,4 @@ -package main +package merp import "encoding/json" import "net/http" @@ -8,7 +8,7 @@ import "time" import "github.com/gin-gonic/gin" import "github.com/google/uuid" import "github.com/rs/zerolog/log" -import "github.com/sneak/merp/models" +import "github.com/sneak/merp/merp/models" func thingRegex() *regexp.Regexp { THING_REGEX, e := regexp.Compile(`^[a-zA-Z0-9\_\-]+$`) diff --git a/models/db.go b/merp/models/db.go similarity index 100% rename from models/db.go rename to merp/models/db.go diff --git a/models/merp.go b/merp/models/merp.go similarity index 100% rename from models/merp.go rename to merp/models/merp.go diff --git a/server.go b/merp/server.go similarity index 98% rename from server.go rename to merp/server.go index 590f13a..16b7aaa 100644 --- a/server.go +++ b/merp/server.go @@ -1,5 +1,5 @@ //3456789112345676892123456789312345678941234567895123456789612345678971234567898 -package main +package merp import "fmt" import "net/http"