Skip to content

Commit

Permalink
Merge branch 'feature/webhook' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	Gemfile.lock
#	README.md
  • Loading branch information
x1wins committed May 21, 2021
2 parents 5a02b85 + 49a0105 commit afd5498
Show file tree
Hide file tree
Showing 31 changed files with 470 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ GEM
minitest (5.14.4)
msgpack (1.4.2)
nio4r (2.5.7)
nokogiri (1.11.4)
nokogiri (1.11.5)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
orm_adapter (0.5.0)
Expand Down Expand Up @@ -301,4 +301,4 @@ RUBY VERSION
ruby 2.6.3p62

BUNDLED WITH
2.2.16
2.2.17
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ OVP(online video platform)란<br/>
![cw-ovp-system-structure.png](cw-ovp-system-structure.png)
- If your node for database that got fault or something wrong, You can lost persistent in database. that's why I recommend AWS RDS for postgresql.

## Webhook
[Webhook](/WEBHOOK.md)

## Getting started
### Storage config
* AWS S3 Storage
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/webhooks.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the webhooks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
4 changes: 2 additions & 2 deletions app/controllers/encodes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require './lib/pagination'

class EncodesController < ApplicationController
before_action :set_encode, only: [:show]
before_action :authenticate_user!
before_action :set_encode, only: [:show]

# GET /encodes
# GET /encodes.json
Expand Down Expand Up @@ -46,6 +46,6 @@ def set_encode

# Only allow a list of trusted parameters through.
def encode_params
params.require(:encode).permit(:title, :file).merge(user_id: current_user.id)
params.require(:encode).permit(:title, :callback_id, :file).merge(user_id: current_user.id)
end
end
79 changes: 79 additions & 0 deletions app/controllers/webhooks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require './lib/pagination'

class WebhooksController < ApplicationController
before_action :authenticate_user!
before_action :set_webhook, only: [:show, :edit, :update, :destroy]

# GET /webhooks
# GET /webhooks.json
def index
page = params[:page].presence || 1
per = params[:per].presence || Pagination.per
@webhooks = Webhook.by_date.page(page).per(per)
end

# GET /webhooks/1
# GET /webhooks/1.json
def show
end

# GET /webhooks/new
def new
@webhook = Webhook.new
end

# GET /webhooks/1/edit
def edit
end

# POST /webhooks
# POST /webhooks.json
def create
@webhook = Webhook.new(webhook_params)

respond_to do |format|
if @webhook.save
format.html { redirect_to @webhook, notice: 'Webhook was successfully created.' }
format.json { render :show, status: :created, location: @webhook }
else
format.html { render :new }
format.json { render json: @webhook.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /webhooks/1
# PATCH/PUT /webhooks/1.json
def update
respond_to do |format|
if @webhook.update(webhook_params)
format.html { redirect_to @webhook, notice: 'Webhook was successfully updated.' }
format.json { render :show, status: :ok, location: @webhook }
else
format.html { render :edit }
format.json { render json: @webhook.errors, status: :unprocessable_entity }
end
end
end

# DELETE /webhooks/1
# DELETE /webhooks/1.json
def destroy
@webhook.destroy
respond_to do |format|
format.html { redirect_to webhooks_url, notice: 'Webhook was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_webhook
@webhook = Webhook.find(params[:id])
end

# Only allow a list of trusted parameters through.
def webhook_params
params.require(:webhook).permit(:url, :api_key, :user_id, :method, :is_active).merge(user_id: current_user.id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/webhooks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WebhooksHelper
end
9 changes: 9 additions & 0 deletions app/models/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Webhook < ApplicationRecord
validates :url, presence: true, format: URI::regexp(%w[http https])
validates :api_key, presence: true
METHOD_OPTIONS = %w(post put get)
validates :method, :inclusion => {:in => METHOD_OPTIONS}
scope :by_date, -> { order('id DESC') }
scope :active, -> { where(is_active: true) }
enum methods: { post: "post", put: "put", get: "get"}
end
28 changes: 28 additions & 0 deletions app/services/client/submit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Client
class Submit < ApplicationService
def initialize(webhook, callback_id, callback_value, callback_format)
@webhook = webhook
@callback_id = callback_id
@callback_value = callback_value
@callback_format = callback_format
end

def call
begin
url = @webhook.url
api_key = @webhook.api_key
callback_id = @callback_id
callback_value = @callback_value
callback_format = @callback_format
headers = {}
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
response = http.post(uri.path, "callback_id=#{callback_id}&callback_value=#{callback_value}&callback_format=#{callback_format}&api_key=#{api_key}", headers)
rescue => e
Sidekiq.logger.info e
ensure
Sidekiq.logger.info response
end
end
end
end
18 changes: 18 additions & 0 deletions app/views/encodes/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@
</div>
</div>

<div class="field is-horizontal">
<div class="field-label is-normal">
<%= form.label :callback_id, class: "label" %>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<%= form.text_field :callback_id, class: "input" , placeholder: "Callback ID" %>
</div>
<% encode.errors[:callback_id].each do |message| %>
<p class="help is-danger">
<%= message %>
</p>
<% end %>
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label">
<%= form.label :file, class: "label" %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/encodes/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
</table>
</div>
<%= paginate @encodes %>

<%= link_to 'New Encode', new_encode_path, class: "button is-primary" %>
2 changes: 1 addition & 1 deletion app/views/encodes/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h1 class="title">Encode > New Encode</h1>
<h1 class="title">Encode > New</h1>

<%= render 'form', encode: @encode %>
4 changes: 4 additions & 0 deletions app/views/encodes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<td>Title</td>
<td><%= @encode.title %></td>
</tr>
<tr>
<td>Callback ID</td>
<td><%= @encode.callback_id %></td>
</tr>
<tr>
<td>File name</td>
<td><%= @encode.file.filename if @encode.file.attached? %></td>
Expand Down
4 changes: 4 additions & 0 deletions app/views/layouts/_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
</div>

<div id="navbarExampleTransparentExample" class="navbar-menu">
<div class="navbar-start">
<%= link_to Encode.model_name, encodes_path, class: "navbar-item #{'is-active' if controller.controller_name == EncodesController.controller_name || current_page?(root_path)}" %>
<%= link_to Webhook.model_name, webhooks_path, class: "navbar-item #{'is-active' if controller.controller_name == WebhooksController.controller_name }" %>
</div>
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
Expand Down
16 changes: 0 additions & 16 deletions app/views/layouts/_menu.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<%= render "layouts/header" %>
<div class="section">
<div class="columns">
<%= render "layouts/menu" %>
<main class="column">
<div class="columns is-multiline">
<div class="column is-fullwidth">
Expand Down
100 changes: 100 additions & 0 deletions app/views/webhooks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<%= form_with(model: webhook, local: true) do |form| %>
<% if webhook.errors.any? %>
<div class="notification is-danger">
<strong><%= pluralize(webhook.errors.count, "error") %> prohibited this webhook from being saved:</strong>
</div>
<% end %>

<div class="field is-horizontal">
<div class="field-label is-normal">
<%= form.label :url, class: "label" %>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<%= form.text_field :url, class: webhook.errors[:url].size > 0 ? "input is-danger": "input" , placeholder: "url" %>
</div>
<% webhook.errors[:url].each do |message| %>
<p class="help is-danger">
<%= message %>
</p>
<% end %>
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label is-normal">
<%= form.label :api_key, class: "label" %>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<%= form.text_field :api_key, class: webhook.errors[:api_key].size > 0 ? "input is-danger": "input" , placeholder: "api_key" %>
</div>
<% webhook.errors[:api_key].each do |message| %>
<p class="help is-danger">
<%= message %>
</p>
<% end %>
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label is-normal">
<%= form.label :method, class: "label" %>
</div>
<div class="field-body">
<div class="field">
<div class="control">
<%= form.select :method, Webhook::METHOD_OPTIONS, { :include_blank => '-- Select One --' }, class: webhook.errors[:method].size > 0 ? "input is-danger": "input" %>
</div>
<% webhook.errors[:method].each do |message| %>
<p class="help is-danger">
<%= message %>
</p>
<% end %>
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label is-normal">
</div>
<div class="field-body">
<div class="field">
<div class="control">
<%= form.label :is_active do %>
<%= form.check_box :is_active %>
is_active
<% end %>
</div>
<% webhook.errors[:is_active].each do |message| %>
<p class="help is-danger">
<%= message %>
</p>
<% end %>
</div>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label">
<!-- Left empty for spacing -->
</div>
<div class="field-body">
<%= form.submit class: "button is-primary" %>
</div>
</div>

<div class="field is-horizontal">
<div class="field-label">
<!-- Left empty for spacing -->
</div>
<div class="field-body">
<%= link_to 'Back', webhooks_path, class: "button is-warning" %>
</div>
</div>

<% end %>
2 changes: 2 additions & 0 deletions app/views/webhooks/_webhook.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! webhook, :id, :url, :api_key, :user_id, :method, :is_active, :created_at, :updated_at
json.url webhook_url(webhook, format: :json)
2 changes: 2 additions & 0 deletions app/views/webhooks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1 class="title">Webhook > Edit </h1>
<%= render 'form', webhook: @webhook %>
34 changes: 34 additions & 0 deletions app/views/webhooks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h1 class="title">Webhooks > List</h1>
<div class="table-container">
<table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth" id="encodes">
<thead>
<tr>
<th>Url</th>
<th>Api key</th>
<th>User</th>
<th>Method</th>
<th>Is active</th>
<th></th>
</tr>
</thead>

<tbody>
<% @webhooks.each do |webhook| %>
<tr>
<td><%= webhook.url %></td>
<td><%= webhook.api_key %></td>
<td><%= webhook.user_id %></td>
<td><%= webhook.method %></td>
<td><%= webhook.is_active %></td>
<td>
<%= link_to 'Show', webhook, class: "button is-link" %>
<%= link_to 'Edit', edit_webhook_path(webhook), class: "button is-primary" %>
<%= link_to 'Destroy', webhook, method: :delete, data: { confirm: 'Are you sure?' }, class: "button is-danger" %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>

<%= link_to 'New Webhook', new_webhook_path, class: "button is-primary" %>
1 change: 1 addition & 0 deletions app/views/webhooks/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @webhooks, partial: "webhooks/webhook", as: :webhook
Loading

0 comments on commit afd5498

Please sign in to comment.