diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..045d3fe --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml diff --git a/Dockerfile b/Dockerfile index d567cf6..aafd7db 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,9 @@ -FROM golang:1.4.2 +FROM golang:1.4.2-onbuild RUN go get github.com/tools/godep - -CMD cd /go/src/app && godep restore && go install && app +RUN cd /go/src/app +RUN godep restore +RUN go build -o bin/application +ENV PORT 3000 +EXPOSE 3000 +CMD ["./run"] diff --git a/Procfile b/Procfile deleted file mode 100644 index b9c2c0e..0000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: puffin_api diff --git a/api.go b/application.go similarity index 97% rename from api.go rename to application.go index d0e6eed..a167d16 100644 --- a/api.go +++ b/application.go @@ -107,11 +107,18 @@ type Impl struct { func (i *Impl) InitDB() { var err error + connection := os.Getenv("DATABASE_URL") + i.DB, err = gorm.Open("postgres", connection) if err != nil { log.Fatalf("Got error when connect database, the error is '%v'", err) } + + if err = i.DB.DB().Ping(); err != nil { + log.Fatalf("Unable to verify connection to database: '%v'", err) + } + i.DB.LogMode(true) } diff --git a/doc/AWS_EB.md b/doc/AWS_EB.md new file mode 100644 index 0000000..5b1cfd5 --- /dev/null +++ b/doc/AWS_EB.md @@ -0,0 +1,45 @@ +# Deploy to Elastic Beanstalk + +## 0. Run a demo application from the web ui. + +> This is the fastest way to get a service role and instance profile +> -- [Service Roles, Instance Profiles, and User Policies][1] + +## 1. Install the EB client + +```sh +pip install --user awsebcli +# add $HOME/.local/bin to your PATH +``` + +## 2. Create EB app and environment + +```sh +NAME=puffin-api +REGION="eu-central-1" +PASSWORD=$(pwgen 30 1) + +eb init \ + --region eu-central-1 \ + --platform "Go 1.4 (Preconfigured - Docker)" \ + $NAME + +eb create $NAME \ + --cname $NAME \ + --region $REGION \ + --database \ + --database.engine postgres \ + --database.user root \ + --database.password $PASSWORD + +echo $PASSWORD +``` + +## 3. Deploy a new version + +```sh +eb use +eb deploy +``` + +[1]:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts-roles.html diff --git a/run b/run new file mode 100755 index 0000000..f19121a --- /dev/null +++ b/run @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +set -x +export DATABASE_URL="postgres://${RDS_USERNAME}:${RDS_PASSWORD}@${RDS_HOSTNAME}/${RDS_DB_NAME}?sslmode=disable" +bin/application