From 9fba7fb74b5080bb0b25895990f390c53718605b Mon Sep 17 00:00:00 2001
From: rt00492 <rt00492@surrey.ac.uk>
Date: Thu, 21 Apr 2022 20:40:11 +0100
Subject: [PATCH] Adding Auth Microservice

---
 config/database.yml | 44 ++++++++++++++++++++++++++++++--------------
 db/auth_schema.rb   | 29 +++++++++++++++++++++++++++++
 docker-compose.yaml | 35 +++++++++++++++++++++++++++++------
 entrypoint.sh       |  7 +++++--
 4 files changed, 93 insertions(+), 22 deletions(-)
 create mode 100644 db/auth_schema.rb

diff --git a/config/database.yml b/config/database.yml
index 301ecac..69da1a5 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -1,17 +1,33 @@
-default: &default
-  adapter: mysql2
-  encoding: utf8
-  host: db
-  username: root
-  password: group24
-  # username: admin
-  # password: admin
-
 development:
-  <<: *default
-  database: docker_dev
+  primary:
+    adapter: mysql2
+    encoding: utf8
+    host: db_primary
+    username: root
+    password: group24
+    database: docker_dev
+  auth:
+    adapter: mysql2
+    encoding: utf8
+    host: db_auth
+    username: root
+    password: group24
+    database: auth
+    port: 3307
 
 test:
-  <<: *default
-  database: docker_test
-
+  primary:
+    adapter: mysql2
+    encoding: utf8
+    host: db_primary
+    username: root
+    password: group24
+    database: docker_dev
+  auth:
+    adapter: mysql2
+    encoding: utf8
+    host: db_auth
+    username: root
+    password: group24
+    database: auth
+    port: 3307
diff --git a/db/auth_schema.rb b/db/auth_schema.rb
new file mode 100644
index 0000000..9036c39
--- /dev/null
+++ b/db/auth_schema.rb
@@ -0,0 +1,29 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# This file is the source Rails uses to define your schema when running `bin/rails
+# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
+# be faster and is potentially less error prone than running all of your
+# migrations from scratch. Old migrations may fail to apply correctly if those
+# migrations use external dependencies or application code.
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema[7.0].define(version: 2022_04_03_154638) do
+  create_table "users", charset: "utf8", force: :cascade do |t|
+    t.string "username"
+    t.string "firstname"
+    t.string "lastname"
+    t.datetime "created_at", null: false
+    t.datetime "updated_at", null: false
+    t.string "email", default: "", null: false
+    t.string "encrypted_password", default: "", null: false
+    t.string "reset_password_token"
+    t.datetime "reset_password_sent_at"
+    t.datetime "remember_created_at"
+    t.index ["email"], name: "index_users_on_email", unique: true
+    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
+  end
+
+end
diff --git a/docker-compose.yaml b/docker-compose.yaml
index 9ce656e..c8a873a 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -1,25 +1,48 @@
 version: "3.7"
 
 services:
-    db:
+    db_primary:
         image: mysql:5.7
         # volumes: 
         #     - db_data:/var/lib/mysql
         restart: always
         environment:
-            # MYSQL_DATABASE: docker
+            MYSQL_DATABASE: docker_dev
             MYSQL_USER: admin
             MYSQL_PASSWORD: admin
             MYSQL_ROOT_PASSWORD: group24
+        ports:
+            - "3306:3306"
+    db_auth:
+        image: mysql:5.7
+        restart: always
+        environment:
+            MYSQL_DATABASE: auth
+            MYSQL_USER: admin
+            MYSQL_PASSWORD: admin
+            MYSQL_ROOT_PASSWORD: group24
+            MYSQL_TCP_PORT: 3307
+
+        ports:
+            - "3307:3307"
     
-    web:
+    
+    primary:
         build: .
         image: webcalendar
         ports:
-            - 3000:3000
+            - "3000:3000"
         volumes:
             - .:/app
         depends_on:
-            - db
+            - db_primary
+        links:
+            - db_primary
+    auth:
+        image: driftydirt/group24auth:0.0.4
+        ports: 
+            - "3001:3000"
+        depends_on:
+            - db_auth
         links:
-            - db
+            - db_auth
diff --git a/entrypoint.sh b/entrypoint.sh
index 93053ac..dab0569 100644
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -10,14 +10,14 @@ rm -f /tmp/pids/server.pid
 # Check and wait for mysql container to be setup and connected
 echo "Waiting for MySQL DB connection ..."
 
-status=$(nc -z db 3306; echo $?)
+status=$(nc -z db_primary 3306; echo $?)
 echo $status
 
 while [ $status != 0 ]
 do
   echo "Waiting 3s ..."
   sleep 3s
-  status=$(nc -z db 3306; echo $?)
+  status=$(nc -z db_primary 3306; echo $?)
   echo $status
 done
 
@@ -43,6 +43,9 @@ echo "DB is up ..."
 # printf "[rake db:seed]\n"
 # rake db:seed
 
+RAILS_ENV=development rails db:environment:set
+
+
 # Reseting database (dropping previous, creating and migrating fresh db)
 printf "\nResetting database ...\n"
 printf "[db:migrate:reset] => [db:drop]-[db:create]-[db:migrate]\n"
-- 
GitLab