Skip to content
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

Rest api #356

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
18 changes: 16 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ UNSPLASH_SECRET=
# Only for production
SECRET_KEY_BASE=

# These two password must be identical
# value for docker-compose: postgres
DATABASE_USER=
# value for docker-compose: postgres
DATABASE_HOST=
# value for docke-compose: pek-next
DATABASE_NAME=
#
DATABASE_PASSWORD=
POSTGRES_PASSWORD=

# available values: development, staging, production
RAILS_ENV=
# enable unless RAILS_ENV is development
RAILS_SERVE_STATIC_FILES=
# provide rollbar access token for production
ROLLBAR_ACCESS_TOKEN=
# enable if used with API
NONAUTH=
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WORKDIR /pek-next
# Install dependencies
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install --deployment --without test development --retry 3
RUN bundle install --deployment --without test --retry 3

# Copy application
COPY . .
Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ gem 'activity_notification', '~> 2.2', '>= 2.2.1'
gem 'pundit', '~> 2.1'
gem "bootsnap", ">= 1.1.0", require: false
gem 'listen'
# use jbuilder to provide data for the REST API
gem 'jbuilder', '~> 2.11', '>= 2.11.2'
# use grape to provide REST API documentation
gem 'grape'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
Expand Down
36 changes: 36 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ GEM
activemodel-serializers-xml (>= 1.0)
activesupport (>= 5.0)
request_store (>= 1.0)
dry-configurable (0.12.1)
concurrent-ruby (~> 1.0)
dry-core (~> 0.5, >= 0.5.0)
dry-container (0.7.2)
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
dry-core (0.6.0)
concurrent-ruby (~> 1.0)
dry-inflector (0.2.0)
dry-logic (1.2.0)
concurrent-ruby (~> 1.0)
dry-core (~> 0.5, >= 0.5)
dry-types (1.5.1)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.5, >= 0.5)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
em-http-request (1.1.5)
addressable (>= 2.3.4)
cookiejar (!= 0.3.1)
Expand Down Expand Up @@ -130,13 +148,22 @@ GEM
ffi (1.15.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
grape (1.5.3)
activesupport
builder
dry-types (>= 1.1)
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
hashie (3.6.0)
http_parser.rb (0.6.0)
httparty (0.17.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
jquery-rails (4.4.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand Down Expand Up @@ -175,6 +202,10 @@ GEM
multi_json (1.12.2)
multi_xml (0.6.0)
multipart-post (2.1.1)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.0.1)
mustermann (>= 1.0.0)
nio4r (2.5.7)
nokogiri (1.11.2)
mini_portile2 (~> 2.5.0)
Expand Down Expand Up @@ -202,6 +233,8 @@ GEM
rspec-rails (>= 3.0.0)
racc (1.5.2)
rack (2.2.3)
rack-accept (0.4.5)
rack (>= 0.4)
rack-protection (2.0.0)
rack
rack-test (1.1.0)
Expand Down Expand Up @@ -266,6 +299,7 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
ruby2_keywords (0.0.4)
rufus-scheduler (3.4.2)
et-orbi (~> 1.0)
sass (3.7.4)
Expand Down Expand Up @@ -340,6 +374,8 @@ DEPENDENCIES
em-http-request
exception_handler (~> 0.8.0.0)
factory_bot_rails
grape
jbuilder (~> 2.11, >= 2.11.2)
jquery-rails
kaminari
listen
Expand Down
5 changes: 5 additions & 0 deletions app/api/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Api < Grape::API
prefix 'api'
format 'json'
mount Userm::Profile
end
8 changes: 8 additions & 0 deletions app/api/userm/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Userm
class Profile < Grape::API
get :profile do
# Your logic
'pro'
end
end
end
6 changes: 5 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ def current_semester
end

def current_user
return impersonate_user if ENV['NONAUTH']
if Rails.env.development? && ENV['NONAUTH']
authorization_header = request.env["HTTP_AUTHORIZATION"]
return User.find(authorization_header.split()[1]) if authorization_header

return impersonate_user
end
@current_user ||= User.includes([{ memberships: [:group] }]).find(session[:user_id])
end
helper_method :current_user
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def show_by_id

def show_self
@user_presenter = current_user.decorate
render :show
@user = current_user
@memberships = Membership.includes(:group).where(user: current_user)
# render :show
render "show_self.json.jbuilder"
end

def edit; end
Expand Down
2 changes: 1 addition & 1 deletion app/views/application/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<i class="uk-icon-bell"></i>
</a>

<% if @notifications.any? %>
<% if @notifications&.any? %>
<div class="uk-dropdown uk-dropdown-navbar uk-overflow-container notification-dropdown">
<ul class="uk-list uk-nav uk-nav-navbar">
<%= render_notifications(@notifications) %>
Expand Down
21 changes: 21 additions & 0 deletions app/views/profiles/show_self.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
json.user do
json.full_name @user.full_name
# json.compact_name @user.compact_name
json.photo_path photo_path(@user_presenter.screen_name)
json.profile_url profile_url(@user_presenter.screen_name)
json.room @user.room
json.cell_phone @user.cell_phone
json.email @user.email
end
json.memberships(@memberships) do |membership|
json.start_date membership.start_date
json.end_date membership.end_date
json.status membership.status

group = membership.group
json.group do
json.id group.id
json.url group_url(group)
json.name group.name
end
end
17 changes: 9 additions & 8 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ default: &default
adapter: postgresql
encoding: unicode
pool: 5
host: localhost
username: pek-next
password: pek-next
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: <%= ENV['DATABASE_HOST'] %>

development:
<<: *default
database: pek-next


# 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: pek-next-test
host: localhost
username: pek-next
password: pek-next

production: &production
<<: *default
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: <%= ENV['DATABASE_HOST'] %>


staging:
<<: *production
4 changes: 1 addition & 3 deletions config/initializers/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
# Without configuration, Rollbar is enabled in all environments.
# To disable in specific environments, set config.enabled=false.

config.access_token = '83caf705fc644065b610040b6c7f8ca2'

# Here we'll disable in 'test':
unless Rails.env.production?
unless ENV['ROLLBAR_ACCESS_TOKEN'].present?
config.enabled = false
end

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
mount Api => '/'

get '/logout', to: 'sessions#destroy', as: :logout
get '/login', to: 'sessions#new', as: :login
Expand Down
10 changes: 2 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ services:
networks:
- pek_network
postgres:
image: postgres:9.6
image: postgres:13.1
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
volumes:
- database_folder:/var/lib/postgresql/data/pgdata
restart: always
Expand All @@ -30,13 +31,6 @@ services:
web:
build: .
command: sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
environment:
- RAILS_SERVE_STATIC_FILES=true
- RAILS_ENV=production
- DATABASE_PASSWORD=
- DATABASE_USER=postgres
- DATABASE_NAME=pek-next
- DATABASE_HOST=postgres
restart: always
env_file:
- .env
Expand Down