Skip to content

Commit 81c5622

Browse files
authored
Dockerize application (#8)
* Dockerize application * Replace config with env vars * replace deprecated onbuild image with latest go build * use alpine base for smaller final image * specify go version for base image
1 parent c113a35 commit 81c5622

File tree

13 files changed

+67
-106
lines changed

13 files changed

+67
-106
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
8+
## [v2.0] - 2018-06-24
9+
### Added
10+
- Docker and docker-compose files
11+
### Changed
12+
- Replaced config file with env vars
13+
- Updated redigo upstream
14+
15+
## [v1.0] - 2016-06-03
16+
### Added
17+
- Initial release

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1-alpine
2+
3+
RUN apk add --no-cache git
4+
5+
WORKDIR /go/src/app
6+
COPY . .
7+
8+
RUN go get -d -v ./...
9+
RUN go install -v ./...
10+
11+
CMD ["app"]

README.md

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,17 @@ Concept based on an idea from
99
http://engineroom.teamwork.com/how-to-securely-provide-a-zip-download-of-a-s3-file-bundle/
1010
then simplified to work as a simple RESTful API.
1111

12-
## Install
13-
https://golang.org/doc/install
14-
```
15-
cd $GOPATH
16-
go get github.com/jobready/thunderzippy
17-
```
12+
## Install and Run
1813

19-
## Configure
2014
```
21-
cp sample_conf.json $GOPATH/thunderzippy_conf.json
15+
docker-compose build
16+
docker-compose up
2217
```
2318

24-
This contains two options:
25-
26-
`RedisServerAndPort` : IP and port of the attached Redis resource
27-
28-
`Port` : the port that the HTTP server will bind to
19+
### Environment Variables
2920

30-
## Run
31-
```
32-
$ $GOPATH/bin/thunderzippy
33-
```
21+
`REDIS_ADDRESS` : Address with port of Redis host e.g. `127.0.0.1:6379`
22+
`PORT` : Local port the HTTP server will bind to
3423

3524
## Use
3625

@@ -53,11 +42,6 @@ Archive: download.zip
5342
61136 1 file
5443
```
5544

56-
### Server installation
57-
Thunderzippy can be run directly on port 80 or as a proxied service behind nginx using the sample_nginx.conf config.
58-
59-
sample_upstart.conf provides a sample Upstart script.
60-
6145
### License
6246

6347
Thunderzippy is released under the [MIT License](http://www.opensource.org/licenses/MIT).

config.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

db.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ import (
77
"time"
88
"errors"
99
"math/rand"
10-
redigo "github.com/garyburd/redigo/redis"
10+
"os"
11+
redigo "github.com/gomodule/redigo/redis"
1112
)
1213

1314
var redisPool *redigo.Pool
1415

1516
func init() {
16-
log.Printf("Opening Redis: %s", config.RedisServerAndPort)
17+
log.Printf("Opening Redis: %s", os.Getenv("REDIS_ADDRESS"))
1718
redisPool = &redigo.Pool{
1819
MaxIdle: 10,
1920
IdleTimeout: 1 * time.Second,
2021
Dial: func() (redigo.Conn, error) {
21-
return redigo.Dial("tcp", config.RedisServerAndPort)
22+
return redigo.Dial("tcp", os.Getenv("REDIS_ADDRESS"))
2223
},
2324
TestOnBorrow: func(c redigo.Conn, t time.Time) (err error) {
2425
_, err = c.Do("PING")
@@ -33,7 +34,7 @@ func init() {
3334
func getFileListByZipReferenceId(id string) (files []*ZipEntry, err error) {
3435
redis := redisPool.Get()
3536
defer redis.Close()
36-
37+
3738
// Get the value from Redis
3839
result, err := redis.Do("GET", "zip:" + id)
3940
if err != nil || result == nil {
@@ -59,10 +60,10 @@ func getFileListByZipReferenceId(id string) (files []*ZipEntry, err error) {
5960
func CreateZipReference(files []*ZipEntry) (ref_id_string string) {
6061
redis := redisPool.Get()
6162
defer redis.Close()
62-
63+
6364
filesJson, err := json.Marshal(files)
6465
HandleError(err)
65-
66+
6667
//get new id redis
6768
ref_id, err := redis.Do("INCR", "zip_reference_id")
6869
HandleError(err)
@@ -71,7 +72,7 @@ func CreateZipReference(files []*ZipEntry) (ref_id_string string) {
7172
// Save JSON files to Redis
7273
_, err = redis.Do("SET", "zip:" + ref_id_string, filesJson)
7374
HandleError(err)
74-
75+
7576
return
7677
}
7778

@@ -83,4 +84,4 @@ func RandomString(strlen int) string {
8384
result[i] = chars[rand.Intn(len(chars))]
8485
}
8586
return string(result)
86-
}
87+
}

docker-compose.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: '3'
2+
3+
services:
4+
redis:
5+
image: redis:3
6+
7+
app:
8+
build: .
9+
depends_on:
10+
- redis
11+
ports:
12+
- 8080:8080
13+
environment:
14+
REDIS_ADDRESS: redis:6379
15+
PORT: 8080

handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func ZipReferenceDownload(w http.ResponseWriter, r *http.Request) {
5656

5757
func ZipReferenceCreate(w http.ResponseWriter, r *http.Request) {
5858
var files []*ZipEntry
59-
59+
6060
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
6161
if err != nil {
6262
http.Error(w, "Data was too limited to allow", 403)
@@ -67,11 +67,11 @@ func ZipReferenceCreate(w http.ResponseWriter, r *http.Request) {
6767
http.Error(w, "Wrong Data. Please try again", 403)
6868
return
6969
}
70-
70+
7171
if err := json.Unmarshal(body, &files); err != nil {
7272
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
7373
w.WriteHeader(422)
74-
74+
7575
if err := json.NewEncoder(w).Encode(err); err != nil {
7676
http.Error(w, "Wrong Data. Please try again", 403)
7777
return
@@ -115,4 +115,4 @@ func addDownloadToZip(zipWriter *zip.Writer, url string, name string) {
115115
}
116116

117117
io.Copy(f, resp.Body)
118-
}
118+
}

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package main
33
import (
44
"log"
55
"net/http"
6+
"os"
67
)
78

89
func main() {
9-
log.Printf("Thunderzippy is go on port %s", config.Port)
10+
log.Printf("Thunderzippy is go on port %s", os.Getenv("PORT"))
1011
http.HandleFunc("/zip/", ZipReferenceHandler)
11-
http.ListenAndServe(":" + config.Port, nil)
12+
http.ListenAndServe(":" + os.Getenv("PORT"), nil)
1213
}
1314

1415
func HandleError(err error) {
1516
if err != nil {
1617
panic(err)
1718
}
18-
}
19+
}

model.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package main
22

3-
type Configuration struct {
4-
RedisServerAndPort string
5-
Port string
6-
}
7-
83
type ZipEntry struct {
94
Filepath, Url string
10-
}
5+
}

0 commit comments

Comments
 (0)