Skip to content

Commit

Permalink
Dev env with Docker (#2)
Browse files Browse the repository at this point in the history
* use go modules

* do not use dep for travis

* Use github.com/putdotio/pas as module name

* run inside docker containers

* move go-mod files above to prevent download modules for each run
  • Loading branch information
muraty authored Oct 15, 2020
1 parent bfdabe4 commit 49755a7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Dockerfile-mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM ubuntu:xenial

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get -y install mysql-server-5.7

ARG MYSQL_USER=pas
ARG MYSQL_PASSWORD=123
ARG MYSQL_DATABASE=pas

ADD docker-mysql-init.sh /tmp/

RUN ["bash", "/tmp/docker-mysql-init.sh"]

ENTRYPOINT ["mysqld"]

EXPOSE 3306
12 changes: 12 additions & 0 deletions Dockerfile-pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.15

WORKDIR /src
COPY go.* ./
RUN go mod download

COPY . ./
RUN go install

ADD config.toml /etc/pas.toml

ENTRYPOINT ["pas", "-config", "/etc/pas.toml"]
12 changes: 12 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ListenAddress = "0.0.0.0:8080"
ShutdownTimeout = 5000
MySQLDSN = "pas:123@(mysql:3306)/pas"

[User]
name = "string"
email = "string"

[Events]

[Events.page_viewed]
path = "string"
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.2'
services:
mysql:
image: pas-mysql
container_name: pas-mysql
build:
dockerfile: Dockerfile-mysql
context: .
ports:
- "3306:3306"
pas:
image: pas
container_name: pas
build:
dockerfile: Dockerfile-pas
context: .
depends_on:
- mysql
ports:
- "8080:8080"
tty: true
privileged: true
36 changes: 36 additions & 0 deletions docker-mysql-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -e

mkdir /var/run/mysqld
chown mysql /var/run/mysqld

mysqld &
pid="$!"

for i in {30..0}; do
if echo 'SELECT 1' | mysql &> /dev/null; then
break
fi
echo 'MySQL init process in progress...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed.'
exit 1
fi

mysql -e "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;"

mysql -e "CREATE USER '$MYSQL_USER'@'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD' ;"
mysql -e "GRANT ALL ON \`$MYSQL_DATABASE\`.* TO '$MYSQL_USER'@'%' ;"
mysql -e 'FLUSH PRIVILEGES ;'

if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'MySQL init process failed.'
exit 1
fi

# listen all ips
sed -i 's/^ *bind-address\s*=.*$/bind-address=0.0.0.0/' /etc/mysql/mysql.conf.d/mysqld.cnf

echo MySQL init process done. Ready for start up.
1 change: 1 addition & 0 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s *Handler) handleUsers(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// todo: if len(users) == 0 -> return
_, err = s.analytics.UpdateUsers(users.Users)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down

0 comments on commit 49755a7

Please sign in to comment.