diff --git a/Dockerfile b/Dockerfile index bda6e0e..c491fa6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,5 @@ -FROM alpine:3.2 - -ENV GOPATH=/gopath \ - SRC=/gopath/src/github.com/caarlos0/cepinator - -WORKDIR $SRC -ADD . $SRC +FROM alpine +RUN apk add --update ca-certificates && rm -rf /var/cache/apk/* /tmp/* EXPOSE 3000 - -RUN apk add -U git go && \ - go get -v -d ./... && \ - go install -v ./... && \ - apk del git go && \ - rm -rf /gopath/src /gopath/pkg /var/cache/apk/* - -CMD /gopath/bin/cepinator +COPY cepinator / +ENTRYPOINT ./cepinator diff --git a/LICENSE b/LICENSE index de6369b..3efaaf0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Carlos Alexandro Becker +Copyright (c) 2015-2017 Carlos Alexandro Becker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 87105d3..0af2726 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,21 @@ Brazilian zip codes (CEP) microservice. -Simple microservice written in Go to manage brazilian zip code addresses. +It gets CEP's from [viacep] and cache them in a redis store. +It's simple and really fast. + +You can run it within Docker: + +```console +docker -d run -p 3000:3000 caarlos0/cepinator +``` + +Or download a binary from [releases](https://github.com/caarlos0/cepinator/releases) +and execute it. + +## Configuration + +Configuration is done via environment variables: + +- `PORT`: port to bind to, defaults to 3000; +- `REDIS_URL`: redis URL to use, defaults to `:6379`. diff --git a/cache/cache.go b/cache/cache.go index 865609c..c3550a6 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,6 +1,8 @@ package cache import ( + "io/ioutil" + "log" "time" rediscache "github.com/go-redis/cache" @@ -8,6 +10,10 @@ import ( msgpack "gopkg.in/vmihailenco/msgpack.v2" ) +func init() { + log.SetOutput(ioutil.Discard) +} + // Cache interface type Cache interface { Close() error @@ -37,7 +43,6 @@ func New(url string) Cache { return msgpack.Unmarshal(b, v) }, } - return &Redis{ ring: ring, codec: codec, diff --git a/docker-build b/docker-build new file mode 100755 index 0000000..4a41e40 --- /dev/null +++ b/docker-build @@ -0,0 +1,13 @@ +#!/bin/bash +docker run \ + -v "$PWD":/go/src/github.com/caarlos0/cepinator \ + -w /go/src/github.com/caarlos0/cepinator \ + -e GOOS=linux \ + -e GOARCH=amd64 \ + -e CGO_ENABLED=0 \ + golang:1.8 \ + bash -c 'go build -tags netgo --ldflags "-extldflags "-static"" -o cepinator .' + +docker build -t caarlos0/cepinator . +docker push caarlos0/cepinator +docker rmi caarlos0/cepinator diff --git a/main.go b/main.go index 5b2e70c..347cfda 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ func main() { if err := env.Parse(&config); err != nil { log.WithError(err).Fatal("failed to load config") } + var log = log.WithField("port", config.Port).WithField("redis", config.RedisURL) var cache = cache.New(config.RedisURL) defer cache.Close() @@ -45,7 +46,7 @@ func main() { WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } - log.WithField("port", config.Port).Info("starting up...") + log.Info("starting up...") if err := srv.ListenAndServe(); err != nil { log.WithError(err).Error("failed to start up server") }