Skip to content

Commit

Permalink
docker, improvements, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed May 14, 2017
1 parent 6328ae6 commit 28aa249
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
19 changes: 4 additions & 15 deletions 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
2 changes: 1 addition & 1 deletion 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
Expand Down
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -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`.
7 changes: 6 additions & 1 deletion cache/cache.go
@@ -1,13 +1,19 @@
package cache

import (
"io/ioutil"
"log"
"time"

rediscache "github.com/go-redis/cache"
"github.com/go-redis/redis"
msgpack "gopkg.in/vmihailenco/msgpack.v2"
)

func init() {
log.SetOutput(ioutil.Discard)
}

// Cache interface
type Cache interface {
Close() error
Expand Down Expand Up @@ -37,7 +43,6 @@ func New(url string) Cache {
return msgpack.Unmarshal(b, v)
},
}

return &Redis{
ring: ring,
codec: codec,
Expand Down
13 changes: 13 additions & 0 deletions 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
3 changes: 2 additions & 1 deletion main.go
Expand Up @@ -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()
Expand All @@ -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")
}
Expand Down

0 comments on commit 28aa249

Please sign in to comment.