Skip to content

Commit

Permalink
make service runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
smaant committed Mar 13, 2017
1 parent 3ab6ad7 commit a822b41
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Makefile
Expand Up @@ -11,6 +11,9 @@ workdir:

build: workdir/service

build-native: $(GOFILES)
go build -o workdir/native-service .

workdir/service: $(GOFILES)
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o workdir/service .

Expand Down
4 changes: 2 additions & 2 deletions db/migrations/001_contacts.up.sql
@@ -1,5 +1,5 @@
CREATE TABLE contacts (
CREATE TABLE IF NOT EXISTS contacts (
id SERIAL PRIMARY KEY,
email varchar(255) UNIQUE NOT NULL,
name varchar(255) NOT NULL
);
);
38 changes: 38 additions & 0 deletions main.go
@@ -0,0 +1,38 @@
package main

import (
"database/sql"
"fmt"
"net/http"
"os"

"github.com/circleci/cci-demo-docker/service"
_ "github.com/mattes/migrate/driver/postgres"
"github.com/mattes/migrate/migrate"
)

func main() {
db := SetupDB()
server := service.NewServer(db)
http.HandleFunc("/", server.ServeHTTP)
http.ListenAndServe(":8080", nil)
}

func SetupDB() *service.Database {
databaseUrl := os.Getenv("DATABASE_URL")
if databaseUrl == "" {
panic("DATABASE_URL must be set!")
}

allErrors, ok := migrate.ResetSync(databaseUrl, "./db/migrations")
if !ok {
panic(fmt.Sprintf("%+v", allErrors))
}

db, err := sql.Open("postgres", databaseUrl)
if err != nil {
panic(fmt.Sprintf("Unable to open DB connection: %+v", err))
}

return &service.Database{db}
}

0 comments on commit a822b41

Please sign in to comment.