Skip to content

Commit

Permalink
Merge pull request #132 from avantifellows/main
Browse files Browse the repository at this point in the history
Production Release: 03/10/2023
  • Loading branch information
Bahugunajii authored Oct 3, 2023
2 parents 2d289da + 11ed738 commit 840277d
Show file tree
Hide file tree
Showing 17 changed files with 809 additions and 665 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/production_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ jobs:
run: |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
#Setting environment variables .
cd /var/www/html/db-service/config
sed -i 's|DATABASE_URL=.*|DATABASE_URL=${{ secrets.PRODUCTION_DATABASE_URL }}|' .env
sed -i 's|SECRET_KEY_BASE=.*|SECRET_KEY_BASE=${{ secrets.PRODUCTION_SECRET_KEY_BASE }}|' .env
sed -i 's|PHX_HOST=.*|PHX_HOST=${{ secrets.PRODUCTION_PHX_HOST }}|' .env
sed -i 's|WHITELISTED_DOMAINS=.*|WHITELISTED_DOMAINS=${{ secrets.PRODUCTION_WHITELISTED_DOMAINS }}|' .env
sed -i 's|POOL_SIZE=.*|POOL_SIZE=${{ secrets.PRODUCTION_POOL_SIZE }}|' .env
sed -i 's|PORT=.*|PORT=${{ secrets.PRODUCTION_PORT }}|' .env
sed -i 's|BEARER_TOKEN=.*|BEARER_TOKEN=${{ secrets.PRODUCTION_BEARER_TOKEN }}|' .env
#Now we have got the access of EC2 and we will start the deploy .
cd /var/www/html
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/staging_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ jobs:
branch_name="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} "
cd /var/www/html/db-service/config
sed -i 's|DATABASE_URL=.*|DATABASE_URL=${{ secrets.STAGING_DATABASE_URL }}|' .env
sed -i 's|SECRET_KEY_BASE=.*|SECRET_KEY_BASE=${{ secrets.STAGING_SECRET_KEY_BASE }}|' .env
sed -i 's|PHX_HOST=.*|PHX_HOST=${{ secrets.STAGING_PHX_HOST }}|' .env
sed -i 's|WHITELISTED_DOMAINS=.*|WHITELISTED_DOMAINS=${{ secrets.STAGING_WHITELISTED_DOMAINS }}|' .env
sed -i 's|POOL_SIZE=.*|POOL_SIZE=${{ secrets.STAGING_POOL_SIZE }}|' .env
sed -i 's|PORT=.*|PORT=${{ secrets.STAGING_PORT }}|' .env
sed -i 's|BEARER_TOKEN=.*|BEARER_TOKEN=${{ secrets.STAGING_BEARER_TOKEN }}|' .env
cd /var/www/html
sudo ./kill_process_on_port_4000.sh
cd /var/www/html/db-service &&
Expand All @@ -41,4 +49,4 @@ jobs:
sudo MIX_ENV=prod mix ecto.migrate &&
sudo MIX_ENV=prod mix phx.swagger.generate &&
sudo MIX_ENV=prod elixir --erl "-detached" -S mix phx.server
"
"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ erl_crash.dump
# Ignore package tarball (built via "mix hex.build").
dbservice-*.tar

# Ignore .env
/config/.env

# Ignore log file
/logs/info.log
8 changes: 7 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ config :dbservice, Dbservice.Mailer, adapter: Swoosh.Adapters.Local
config :swoosh, :api_client, false

# Configures Elixir's Logger
config :logger, :console,
config :logger,
backends: [:console, {LoggerFileBackend, :request_log}],
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

config :logger, :request_log,
path: "logs/info.log",
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

Expand Down
2 changes: 1 addition & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Config
# config :dbservice, DbserviceWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"

# Do not print debug messages in production
config :logger, level: :info
config :logger, :console, format: "[$level] $message\n"

# ## SSL Support
#
Expand Down
3 changes: 2 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ if config_env() == :prod do
ip: {0, 0, 0, 0, 0, 0, 0, 0},
port: port
],
secret_key_base: secret_key_base
secret_key_base: secret_key_base,
debug_errors: true
end
4 changes: 3 additions & 1 deletion lib/dbservice/form_schemas/form_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Dbservice.FormSchemas.FormSchema do

schema "form_schema" do
field(:name, :string)
field(:meta_data, :map)
field(:attributes, :map)

timestamps()
Expand All @@ -20,7 +21,8 @@ defmodule Dbservice.FormSchemas.FormSchema do
form_schema
|> cast(attrs, [
:name,
:attributes
:attributes,
:meta_data
])
|> validate_required([:name])
end
Expand Down
10 changes: 5 additions & 5 deletions lib/dbservice/sessions/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ defmodule Dbservice.Sessions.Session do
field(:platform_id, :string)
field(:type, :string)
field(:auth_type, :string)
field(:activate_signup, :string)
field(:id_generation, :string)
field(:redirection, :string)
field(:pop_up_form, :string)
field(:number_of_fields_in_pop_form, :string)
field(:activate_signup, :boolean)
field(:id_generation, :boolean)
field(:redirection, :boolean)
field(:pop_up_form, :boolean)
field(:number_of_fields_in_pop_form, :integer)

timestamps()

Expand Down
3 changes: 2 additions & 1 deletion lib/dbservice_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ defmodule DbserviceWeb.Endpoint do
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug DbserviceWeb.DomainWhitelistPlug
plug CORSPlug
plug DbserviceWeb.AuthenticationMiddleware

plug DbserviceWeb.Router
end
41 changes: 41 additions & 0 deletions lib/dbservice_web/plugs/authentication_middleware_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule DbserviceWeb.AuthenticationMiddleware do
@moduledoc """
This module provides a Plug middleware for handling API key-based authentication.
The middleware checks the 'Authorization' header of incoming HTTP requests to ensure that
it matches a predefined API key stored in the 'BEARER_TOKEN' environment variable.
If the API key is valid, the request is allowed to proceed; otherwise, a '401 Unauthorized'
response is sent, and further processing is halted.
"""
import Plug.Conn
import Dotenvy

def init(_opts), do: %{}

def call(conn, _opts) do
source(["config/.env", "config/.env"])

referer =
get_req_header(conn, "referer")
|> List.first()
|> to_string()

request_path = conn.request_path

api_key = get_req_header(conn, "authorization")

if api_key == ["Bearer " <> env!("BEARER_TOKEN", :string!)] ||
is_swagger_request?(referer, request_path) do
conn
else
conn
|> send_resp(401, "Not Authorized")
|> halt()
end
end

defp is_swagger_request?(referer, request_path) do
String.contains?(referer, "swagger") || String.contains?(request_path, "swagger")
end
end
10 changes: 5 additions & 5 deletions lib/dbservice_web/swagger_schemas/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ defmodule DbserviceWeb.SwaggerSchema.Session do
form_schema_id(:string, "Id for the form schema")
type(:string, "Type of session")
auth_type(:string, "Authentication methods used for session")
activate_signup(:string, "Is sign up allowed for this session")
id_generation(:string, "Is ID being generated for this session")
redirection(:string, "Is the session redirecting to some other platform")
pop_up_form(:string, "Is the session showing a pop up form")
number_of_fields_in_pop_form(:string, "Number of fields in the pop form")
activate_signup(:boolean, "Is sign up allowed for this session")
id_generation(:boolean, "Is ID being generated for this session")
redirection(:boolean, "Is the session redirecting to some other platform")
pop_up_form(:boolean, "Is the session showing a pop up form")
number_of_fields_in_pop_form(:integer, "Number of fields in the pop form")
end

example(%{
Expand Down
4 changes: 3 additions & 1 deletion lib/dbservice_web/views/form_schema_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ defmodule DbserviceWeb.FormSchemaView do

def render("form_schema.json", %{form_schema: form_schema}) do
%{
id: form_schema.id,
name: form_schema.name,
attributes: form_schema.attributes
attributes: form_schema.attributes,
meta_data: form_schema.meta_data
}
end
end
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ defmodule Dbservice.MixProject do
{:ex_check, "~> 0.14.0", only: [:dev], runtime: false},
{:dialyxir, ">= 0.0.0", only: [:dev], runtime: false},
{:credo, ">= 0.0.0", only: [:dev], runtime: false},
{:dotenvy, "~> 0.8.0"}
{:dotenvy, "~> 0.8.0"},
{:cors_plug, "~> 3.0"},
{:logger_file_backend, "~> 0.0.13"}
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Dbservice.Repo.Migrations.AlterStudentTable do
use Ecto.Migration

def change do
alter table(:session) do
alter table(:student) do
modify(:has_internet_access, :string)
end
end
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20230816173334_alter_form_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Dbservice.Repo.Migrations.AlterFormTable do
use Ecto.Migration

def change do
alter table(:form_schema) do
add(:meta_data, :map)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Dbservice.Repo.Migrations.ModifyDataTypesInSessionTable do
use Ecto.Migration

def up do
execute("""
ALTER TABLE session
ALTER COLUMN activate_signup TYPE BOOLEAN USING (activate_signup::boolean),
ALTER COLUMN id_generation TYPE BOOLEAN USING (id_generation::boolean),
ALTER COLUMN redirection TYPE BOOLEAN USING (redirection::boolean),
ALTER COLUMN pop_up_form TYPE BOOLEAN USING (pop_up_form::boolean),
ALTER COLUMN number_of_fields_in_pop_form TYPE INTEGER USING (number_of_fields_in_pop_form::integer);
""")
end

def down do
execute("""
ALTER TABLE session
ALTER COLUMN activate_signup TYPE VARCHAR,
ALTER COLUMN id_generation TYPE VARCHAR,
ALTER COLUMN redirection TYPE VARCHAR,
ALTER COLUMN pop_up_form TYPE VARCHAR,
ALTER COLUMN number_of_fields_in_pop_form TYPE INTEGER;
""")
end
end
Loading

0 comments on commit 840277d

Please sign in to comment.