diff --git a/Dockerfile b/Dockerfile index 4d607d159e45f4b3d0a8a5f2587ebf54077b1c89..842969ecf47d94d13efce888d4ed8ee0dbeeac73 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,24 @@ -# syntax=docker/dockerfile:1 FROM ruby:2.7.0 -RUN apt-get update -qq && apt-get install -y nodejs default-mysql-client -WORKDIR /event-service -COPY Gemfile /event-service/Gemfile -COPY Gemfile.lock /event-service/Gemfile.lock + +RUN apt-get update +RUN apt-get install -y nodejs +RUN gem install bundler -v 2.1.4 +RUN apt-get install -y default-mysql-client +RUN apt-get install -y netcat +RUN apt-get install -y dos2unix + +RUN echo "export PATH=\"$PATH:/usr/local/bundle/bin\"" >> ~/.bashrc + +WORKDIR /app +COPY Gemfile* /app/ RUN bundle install +COPY . /app -# Add a script to be executed every time the container starts. COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh -ENTRYPOINT ["entrypoint.sh"] -EXPOSE 3000 +RUN dos2unix /usr/bin/entrypoint.sh +ENTRYPOINT [ "entrypoint.sh" ] -# Configure the main process to run when running the image +EXPOSE 3000 +# WORKDIR /app/app CMD ["rails", "server", "-b", "0.0.0.0"] diff --git a/Gemfile b/Gemfile index 3d8cf7a89611c716ea2eaea65baeb45d0292a806..d76031885f2009f5e3912ec80b038481c93aa8f3 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ ruby '2.7.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.5' # Use mysql as the database for Active Record -gem 'mysql2', '~> 0.5' +gem 'pg' # Use Puma as the app server gem 'puma', '~> 5.0' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder @@ -33,8 +33,6 @@ end group :development do gem 'listen', '~> 3.3' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring - gem 'spring' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] diff --git a/Gemfile.lock b/Gemfile.lock index f9b54f22e8b81bfffbdfb1688419474c9d1ea8e3..3904c22d04bc124691c7cd3782330d52674e6336 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,60 +1,60 @@ GEM remote: https://rubygems.org/ specs: - actioncable (6.1.5) - actionpack (= 6.1.5) - activesupport (= 6.1.5) + actioncable (6.1.5.1) + actionpack (= 6.1.5.1) + activesupport (= 6.1.5.1) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailbox (6.1.5) - actionpack (= 6.1.5) - activejob (= 6.1.5) - activerecord (= 6.1.5) - activestorage (= 6.1.5) - activesupport (= 6.1.5) + actionmailbox (6.1.5.1) + actionpack (= 6.1.5.1) + activejob (= 6.1.5.1) + activerecord (= 6.1.5.1) + activestorage (= 6.1.5.1) + activesupport (= 6.1.5.1) mail (>= 2.7.1) - actionmailer (6.1.5) - actionpack (= 6.1.5) - actionview (= 6.1.5) - activejob (= 6.1.5) - activesupport (= 6.1.5) + actionmailer (6.1.5.1) + actionpack (= 6.1.5.1) + actionview (= 6.1.5.1) + activejob (= 6.1.5.1) + activesupport (= 6.1.5.1) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (6.1.5) - actionview (= 6.1.5) - activesupport (= 6.1.5) + actionpack (6.1.5.1) + actionview (= 6.1.5.1) + activesupport (= 6.1.5.1) rack (~> 2.0, >= 2.0.9) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (6.1.5) - actionpack (= 6.1.5) - activerecord (= 6.1.5) - activestorage (= 6.1.5) - activesupport (= 6.1.5) + actiontext (6.1.5.1) + actionpack (= 6.1.5.1) + activerecord (= 6.1.5.1) + activestorage (= 6.1.5.1) + activesupport (= 6.1.5.1) nokogiri (>= 1.8.5) - actionview (6.1.5) - activesupport (= 6.1.5) + actionview (6.1.5.1) + activesupport (= 6.1.5.1) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (6.1.5) - activesupport (= 6.1.5) + activejob (6.1.5.1) + activesupport (= 6.1.5.1) globalid (>= 0.3.6) - activemodel (6.1.5) - activesupport (= 6.1.5) - activerecord (6.1.5) - activemodel (= 6.1.5) - activesupport (= 6.1.5) - activestorage (6.1.5) - actionpack (= 6.1.5) - activejob (= 6.1.5) - activerecord (= 6.1.5) - activesupport (= 6.1.5) + activemodel (6.1.5.1) + activesupport (= 6.1.5.1) + activerecord (6.1.5.1) + activemodel (= 6.1.5.1) + activesupport (= 6.1.5.1) + activestorage (6.1.5.1) + actionpack (= 6.1.5.1) + activejob (= 6.1.5.1) + activerecord (= 6.1.5.1) + activesupport (= 6.1.5.1) marcel (~> 1.0) mini_mime (>= 1.1.0) - activesupport (6.1.5) + activesupport (6.1.5.1) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) @@ -75,7 +75,7 @@ GEM listen (3.7.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - loofah (2.16.0) + loofah (2.17.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -83,43 +83,41 @@ GEM marcel (1.0.2) method_source (1.0.0) mini_mime (1.1.2) - mini_portile2 (2.8.0) minitest (5.15.0) msgpack (1.5.1) - mysql2 (0.5.3) nio4r (2.5.8) - nokogiri (1.13.4) - mini_portile2 (~> 2.8.0) + nokogiri (1.13.4-x86_64-linux) racc (~> 1.4) + pg (1.3.5) puma (5.6.4) nio4r (~> 2.0) racc (1.6.0) rack (2.2.3) rack-test (1.1.0) rack (>= 1.0, < 3) - rails (6.1.5) - actioncable (= 6.1.5) - actionmailbox (= 6.1.5) - actionmailer (= 6.1.5) - actionpack (= 6.1.5) - actiontext (= 6.1.5) - actionview (= 6.1.5) - activejob (= 6.1.5) - activemodel (= 6.1.5) - activerecord (= 6.1.5) - activestorage (= 6.1.5) - activesupport (= 6.1.5) + rails (6.1.5.1) + actioncable (= 6.1.5.1) + actionmailbox (= 6.1.5.1) + actionmailer (= 6.1.5.1) + actionpack (= 6.1.5.1) + actiontext (= 6.1.5.1) + actionview (= 6.1.5.1) + activejob (= 6.1.5.1) + activemodel (= 6.1.5.1) + activerecord (= 6.1.5.1) + activestorage (= 6.1.5.1) + activesupport (= 6.1.5.1) bundler (>= 1.15.0) - railties (= 6.1.5) + railties (= 6.1.5.1) sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.4.2) loofah (~> 2.3) - railties (6.1.5) - actionpack (= 6.1.5) - activesupport (= 6.1.5) + railties (6.1.5.1) + actionpack (= 6.1.5.1) + activesupport (= 6.1.5.1) method_source rake (>= 12.2) thor (~> 1.0) @@ -127,7 +125,6 @@ GEM rb-fsevent (0.11.1) rb-inotify (0.10.1) ffi (~> 1.0) - spring (4.0.0) sprockets (4.0.3) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -150,14 +147,12 @@ DEPENDENCIES bootsnap (>= 1.4.4) byebug listen (~> 3.3) - mysql2 (~> 0.5) + pg puma (~> 5.0) rails (~> 6.1.5) - spring - tzinfo-data RUBY VERSION ruby 2.7.0p0 BUNDLED WITH - 2.1.2 + 2.1.4 diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 8ada10f40a227f9affd73d391548ee2dff5a7933..4b1642cf168b55bcb302b38ede52a79c33d47bd4 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -7,7 +7,6 @@ class EventsController < ApplicationController render json: @events end - # GET /events/1 def show render json: @event @@ -46,6 +45,8 @@ class EventsController < ApplicationController # Only allow a list of trusted parameters through. def event_params - params.require(:event).permit(:title, :description, :active, :createdAt, :scheduledAt) + puts "lolol" + puts params["event"] + params.require(:event).permit(:title, :description, :active, :createdAt, :scheduledAt, :creator, :invitees => []) end end diff --git a/config/database.yml b/config/database.yml index 0b9c85cf79d6ad176e943f5eafd2f97b2b1c47d4..356745fd05eb0535ebec9ac9579b489a47164649 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,54 +1,19 @@ -# MySQL. Versions 5.5.8 and up are supported. -# -# Install the MySQL driver -# gem install mysql2 -# -# Ensure the MySQL gem is defined in your Gemfile -# gem 'mysql2' -# -# And be sure to use new-style password hashing: -# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html -# -default: &default - adapter: mysql2 - encoding: utf8 - host: db - username: root - password: group24 - development: - <<: *default - database: docker_dev + primary: + adapter: postgresql + encoding: unicode + host: db_events + username: admin + password: group24 + database: event_microservice + port: 5432 -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. test: - <<: *default - database: docker_test - -# As with config/credentials.yml, you never want to store sensitive information, -# like your database password, in your source code. If your source code is -# ever seen by anyone, they now have access to your database. -# -# Instead, provide the password or a full connection URL as an environment -# variable when you boot the app. For example: -# -# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" -# -# If the connection URL is provided in the special DATABASE_URL environment -# variable, Rails will automatically merge its configuration values on top of -# the values provided in this file. Alternatively, you can specify a connection -# URL environment variable explicitly: -# -# production: -# url: <%= ENV['MY_APP_DATABASE_URL'] %> -# -# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database -# for a full overview on how database connection configuration can be specified. -# -production: - <<: *default - database: event_service_production - username: event_service - password: <%= ENV['EVENT_SERVICE_DATABASE_PASSWORD'] %> + primary: + adapter: postgresql + encoding: unicode + host: db_events + username: admin + password: group24 + database: event_microservice + port: 5432 diff --git a/config/routes.rb b/config/routes.rb index 34fb1aff8c6697352f48cc7118e59a0eef87aca5..dda82ccda6efed645c8c5b171d9eb49533644657 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,12 @@ Rails.application.routes.draw do resources :events # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html + + get "events", to: "events#index" + get "event", to: "events#show" + post "events", to: "events#create" + put "events", to: "events#update" + delete "events", to: "events#destroy" + + end diff --git a/db/migrate/20220415153430_create_events.rb b/db/migrate/20220415153430_create_events.rb index a27af9899a054aab1d05758e9ac29212930ceec1..003e70bd0a3e483c0b6dda60114444c9b6c6b016 100644 --- a/db/migrate/20220415153430_create_events.rb +++ b/db/migrate/20220415153430_create_events.rb @@ -6,7 +6,6 @@ class CreateEvents < ActiveRecord::Migration[6.1] t.boolean :active t.datetime :createdAt t.datetime :scheduledAt - t.timestamps end end diff --git a/db/migrate/20220501134156_add_creator_to_event.rb b/db/migrate/20220501134156_add_creator_to_event.rb new file mode 100644 index 0000000000000000000000000000000000000000..4b5b9893dbeb03f4e9adea27b60ebbb29b001d15 --- /dev/null +++ b/db/migrate/20220501134156_add_creator_to_event.rb @@ -0,0 +1,5 @@ +class AddCreatorToEvent < ActiveRecord::Migration[6.1] + def change + add_column :events, :creator, :string + end +end diff --git a/db/migrate/20220501142617_add_invitees_to_event.rb b/db/migrate/20220501142617_add_invitees_to_event.rb new file mode 100644 index 0000000000000000000000000000000000000000..0ba7de08a844b4138cf0cc65c9345a95de58590b --- /dev/null +++ b/db/migrate/20220501142617_add_invitees_to_event.rb @@ -0,0 +1,5 @@ +class AddInviteesToEvent < ActiveRecord::Migration[6.1] + def change + add_column :events, :invitees, :text, array: true, default: [] + end +end diff --git a/db/schema.rb b/db/schema.rb index 016fbb71a2a2554590c45114952f54d53ec14e8b..8177cc38f9df3934d1deb2b9265529e3ed176fff 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_15_153430) do +ActiveRecord::Schema.define(version: 2022_05_01_142617) do - create_table "events", charset: "utf8", force: :cascade do |t| + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "events", force: :cascade do |t| t.string "title" t.text "description" t.boolean "active" @@ -20,6 +23,8 @@ ActiveRecord::Schema.define(version: 2022_04_15_153430) do t.datetime "scheduledAt" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.string "creator" + t.text "invitees", default: [], array: true end end diff --git a/docker-compose.yml b/docker-compose.yml index 882810d2ba9000ceed60e79768ac81f44dcc4f3b..679af727105b7edd8a4414ce2fbcb25b185d95b1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,22 +1,26 @@ version: "3.7" services: - db: - image: mysql:5.7 + db_events: + image: postgres:latest # volumes: # - db_data:/var/lib/mysql restart: always environment: - # MYSQL_DATABASE: docker - MYSQL_USER: admin - MYSQL_PASSWORD: admin - MYSQL_ROOT_PASSWORD: group24 + POSTGRES_DB: event_microservice + POSTGRES_USER: admin + POSTGRES_PASSWORD: group24 + + ports: + - 3308:3308 event: build: . - command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" + image: event-service ports: - - 3000:3000 + - 3003:3000 volumes: - - .:/event-service + - .:/app depends_on: - - db \ No newline at end of file + - db_events + links: + - db_events \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh index 686e023ae2cc47bfec9d7f57d98634fa1fc356fc..f4545fb914dcac34e6d41440e0ce9589786de74f 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,8 +1,64 @@ #!/bin/bash set -e -# Remove a potentially pre-existing server.pid for Rails. -rm -f /event-service/tmp/pids/server.pid +#Remove a potentally pre-existing server.pid for Rails. +rm -f /tmp/pids/server.pid -# Then exec the container's main process (what's set as CMD in the Dockerfile). -exec "$@" \ No newline at end of file +# Sleep for 14 seconds to allow mysql db to setup and connect +# sleep 14 + +# Check and wait for mysql container to be setup and connected +echo "Waiting for MySQL DB connection ..." + +status=$(nc -z db_events 5432; echo $?) +echo $status + +while [ $status != 0 ] +do + echo "Waiting 3s ..." + sleep 3s + status=$(nc -z db_events 5432; echo $?) + echo $status +done + +echo "DB is up ..." + +# # Dropping existing db +# printf "\nDropping exising db ...\n" +# rake db:drop + +# # Create all databases +# printf "\nCreating new databases ...\n" +# rake db:create + +# # Migrate all pending db migrations +# printf "\nMigrating new databases ...\n" +# printf "[db:migrate]\n" +# rails db:migrate +# printf "[db:migrate RAILS_ENV=test]\n" +# rails db:migrate RAILS_ENV=test + +# # Push seeds into database +# printf "\nPushing seeds to database ...\n" +# 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" +rails db:migrate:reset + +# Pushing seeds into database +printf "\nPushing seeds to database ...\n" +printf "[db:setup] => [db:schema:load]-[db:seed]\n" +rails db:setup + +printf "\n------------------------\n" +printf "Completed database setup\nStarting server ...\n" +printf "hosted on: localhost:3000\n\n" + +# Then exec the containers main process (what's set as CMD in the Dockerfile). +exec "$@"