Skip to content

Replace Debian MySQL with Alpine Postgres #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM golang:1.9.2 as builder
RUN go get -d -v github.com/gorilla/mux github.com/go-sql-driver/mysql
RUN go get -d -v github.com/gorilla/mux github.com/lib/pq
ADD . /go/src/github.com/teslagov/clarakm-projects-go/
WORKDIR /go/src/github.com/teslagov/clarakm-projects-go/
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
Expand Down
18 changes: 3 additions & 15 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// app.go

package main

import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"strconv"
)

Expand All @@ -22,10 +19,10 @@ type App struct {

// Initialize DB connections and router
func (a *App) Initialize(user, password, host, dbname string) {
connectionString := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", user, password, host, dbname)
connectionString := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable", user, password, host, dbname)

var err error
a.DB, err = sql.Open("mysql", connectionString)
a.DB, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
Expand All @@ -40,22 +37,13 @@ func (a *App) Run(addr string) {
}

func (a *App) initializeRoutes() {
a.Router.HandleFunc("/hello", a.hello).Methods("GET")
a.Router.HandleFunc("/users", a.getUsers).Methods("GET")
a.Router.HandleFunc("/user", a.createUser).Methods("POST")
a.Router.HandleFunc("/user/{id:[0-9]+}", a.getUser).Methods("GET")
a.Router.HandleFunc("/user/{id:[0-9]+}", a.updateUser).Methods("PUT")
a.Router.HandleFunc("/user/{id:[0-9]+}", a.deleteUser).Methods("DELETE")
}

func (a *App) hello(w http.ResponseWriter, r *http.Request) {
user := os.Getenv("MYSQL_USER")
pass := os.Getenv("MYSQL_PASSWORD")
host := os.Getenv("MYSQL_HOST")
db := os.Getenv("MYSQL_DATABASE")
respondWithJSON(w, http.StatusOK, fmt.Sprintf("Hello, world! You're DB URL is: %s:%s@tcp(%s:3306)/%s", user, pass, host, db))
}

func (a *App) getUsers(w http.ResponseWriter, r *http.Request) {
count, _ := strconv.Atoi(r.FormValue("count"))
start, _ := strconv.Atoi(r.FormValue("start"))
Expand Down
25 changes: 8 additions & 17 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,17 @@ services:
ports:
- "8080:8080"
environment:
- MYSQL_HOST=db
- MYSQL_DATABASE=clarakm-projects
- MYSQL_USER=clara
- MYSQL_PASSWORD=password
- DB_HOST=db
- DB_DATABASE=clarakm-projects
- DB_USER=clara
- DB_PASSWORD=password
depends_on:
- db

db:
container_name: clarakm-projects-db
image: mysql:5.7.20
image: postgres:10.1-alpine
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=clarakm-projects
- MYSQL_USER=clara
- MYSQL_PASSWORD=password
ports:
- "13306:3306"
volumes:
- projects-data-volume:/var/lib/mysql

volumes:
projects-data-volume:
driver: local
- POSTGRES_DB=clarakm-projects
- POSTGRES_USER=clara
- POSTGRES_PASSWORD=password
10 changes: 4 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// main.go

package main

import (
Expand All @@ -9,10 +7,10 @@ import (
func main() {
a := App{}

user := os.Getenv("MYSQL_USER")
pass := os.Getenv("MYSQL_PASSWORD")
host := os.Getenv("MYSQL_HOST")
db := os.Getenv("MYSQL_DATABASE")
user := os.Getenv("DB_USER")
pass := os.Getenv("DB_PASSWORD")
host := os.Getenv("DB_HOST")
db := os.Getenv("DB_DATABASE")

a.Initialize(user, pass, host, db)

Expand Down
11 changes: 2 additions & 9 deletions model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// model.go

package main

import (
Expand Down Expand Up @@ -31,14 +29,9 @@ func (u *user) deleteUser(db *sql.DB) error {
}

func (u *user) createUser(db *sql.DB) error {
statement := fmt.Sprintf("INSERT INTO users(name, age) VALUES('%s', %d)", u.Name, u.Age)
_, err := db.Exec(statement)

if err != nil {
return err
}
statement := fmt.Sprintf("INSERT INTO users(name, age) VALUES('%s', %d) returning id", u.Name, u.Age)

err = db.QueryRow("SELECT LAST_INSERT_ID()").Scan(&u.ID)
err := db.QueryRow(statement).Scan(&u.ID)

if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion seed-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ main() {
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
local db_container_id=$(docker ps -aqf "name=clarakm-projects-db")
local path_to_sql_ddl_file=${SCRIPT_DIR}/seed-data.sql
cat ${path_to_sql_ddl_file} | docker exec -i ${db_container_id} /usr/bin/mysql -uclara -ppassword clarakm-projects
cat ${path_to_sql_ddl_file} | docker exec -i ${db_container_id} /usr/local/bin/psql --username=clara --password clarakm-projects
}

main "$@"
10 changes: 5 additions & 5 deletions seed-data.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id int(8) NOT NULL AUTO_INCREMENT,
name varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
age int(8) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name character varying(255) NOT NULL,
age integer NOT NULL,
Created date
) WITH (OIDS=FALSE);