Skip to content

Commit

Permalink
Dockerize application (#8)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
s01ipsist authored Jun 25, 2018
1 parent c113a35 commit 81c5622
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 106 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).


## [v2.0] - 2018-06-24
### Added
- Docker and docker-compose files
### Changed
- Replaced config file with env vars
- Updated redigo upstream

## [v1.0] - 2016-06-03
### Added
- Initial release
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1-alpine

RUN apk add --no-cache git

WORKDIR /go/src/app
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

CMD ["app"]
28 changes: 6 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,17 @@ Concept based on an idea from
http://engineroom.teamwork.com/how-to-securely-provide-a-zip-download-of-a-s3-file-bundle/
then simplified to work as a simple RESTful API.

## Install
https://golang.org/doc/install
```
cd $GOPATH
go get github.com/jobready/thunderzippy
```
## Install and Run

## Configure
```
cp sample_conf.json $GOPATH/thunderzippy_conf.json
docker-compose build
docker-compose up
```

This contains two options:

`RedisServerAndPort` : IP and port of the attached Redis resource

`Port` : the port that the HTTP server will bind to
### Environment Variables

## Run
```
$ $GOPATH/bin/thunderzippy
```
`REDIS_ADDRESS` : Address with port of Redis host e.g. `127.0.0.1:6379`
`PORT` : Local port the HTTP server will bind to

## Use

Expand All @@ -53,11 +42,6 @@ Archive: download.zip
61136 1 file
```

### Server installation
Thunderzippy can be run directly on port 80 or as a proxied service behind nginx using the sample_nginx.conf config.

sample_upstart.conf provides a sample Upstart script.

### License

Thunderzippy is released under the [MIT License](http://www.opensource.org/licenses/MIT).
Expand Down
20 changes: 0 additions & 20 deletions config.go

This file was deleted.

17 changes: 9 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import (
"time"
"errors"
"math/rand"
redigo "github.com/garyburd/redigo/redis"
"os"
redigo "github.com/gomodule/redigo/redis"
)

var redisPool *redigo.Pool

func init() {
log.Printf("Opening Redis: %s", config.RedisServerAndPort)
log.Printf("Opening Redis: %s", os.Getenv("REDIS_ADDRESS"))
redisPool = &redigo.Pool{
MaxIdle: 10,
IdleTimeout: 1 * time.Second,
Dial: func() (redigo.Conn, error) {
return redigo.Dial("tcp", config.RedisServerAndPort)
return redigo.Dial("tcp", os.Getenv("REDIS_ADDRESS"))
},
TestOnBorrow: func(c redigo.Conn, t time.Time) (err error) {
_, err = c.Do("PING")
Expand All @@ -33,7 +34,7 @@ func init() {
func getFileListByZipReferenceId(id string) (files []*ZipEntry, err error) {
redis := redisPool.Get()
defer redis.Close()

// Get the value from Redis
result, err := redis.Do("GET", "zip:" + id)
if err != nil || result == nil {
Expand All @@ -59,10 +60,10 @@ func getFileListByZipReferenceId(id string) (files []*ZipEntry, err error) {
func CreateZipReference(files []*ZipEntry) (ref_id_string string) {
redis := redisPool.Get()
defer redis.Close()

filesJson, err := json.Marshal(files)
HandleError(err)

//get new id redis
ref_id, err := redis.Do("INCR", "zip_reference_id")
HandleError(err)
Expand All @@ -71,7 +72,7 @@ func CreateZipReference(files []*ZipEntry) (ref_id_string string) {
// Save JSON files to Redis
_, err = redis.Do("SET", "zip:" + ref_id_string, filesJson)
HandleError(err)

return
}

Expand All @@ -83,4 +84,4 @@ func RandomString(strlen int) string {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
}
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

services:
redis:
image: redis:3

app:
build: .
depends_on:
- redis
ports:
- 8080:8080
environment:
REDIS_ADDRESS: redis:6379
PORT: 8080
8 changes: 4 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ZipReferenceDownload(w http.ResponseWriter, r *http.Request) {

func ZipReferenceCreate(w http.ResponseWriter, r *http.Request) {
var files []*ZipEntry

body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
http.Error(w, "Data was too limited to allow", 403)
Expand All @@ -67,11 +67,11 @@ func ZipReferenceCreate(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Wrong Data. Please try again", 403)
return
}

if err := json.Unmarshal(body, &files); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422)

if err := json.NewEncoder(w).Encode(err); err != nil {
http.Error(w, "Wrong Data. Please try again", 403)
return
Expand Down Expand Up @@ -115,4 +115,4 @@ func addDownloadToZip(zipWriter *zip.Writer, url string, name string) {
}

io.Copy(f, resp.Body)
}
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package main
import (
"log"
"net/http"
"os"
)

func main() {
log.Printf("Thunderzippy is go on port %s", config.Port)
log.Printf("Thunderzippy is go on port %s", os.Getenv("PORT"))
http.HandleFunc("/zip/", ZipReferenceHandler)
http.ListenAndServe(":" + config.Port, nil)
http.ListenAndServe(":" + os.Getenv("PORT"), nil)
}

func HandleError(err error) {
if err != nil {
panic(err)
}
}
}
7 changes: 1 addition & 6 deletions model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package main

type Configuration struct {
RedisServerAndPort string
Port string
}

type ZipEntry struct {
Filepath, Url string
}
}
4 changes: 0 additions & 4 deletions sample_conf.json

This file was deleted.

20 changes: 0 additions & 20 deletions sample_nginx.conf

This file was deleted.

18 changes: 0 additions & 18 deletions sample_upstart.conf

This file was deleted.

0 comments on commit 81c5622

Please sign in to comment.