Skip to content

Commit

Permalink
Merge pull request #35 from CaptainFact/staging
Browse files Browse the repository at this point in the history
Release 0.8.17
  • Loading branch information
Betree committed Nov 1, 2018
2 parents 2547482 + aad9969 commit 6945766
Show file tree
Hide file tree
Showing 43 changed files with 298 additions and 208 deletions.
19 changes: 13 additions & 6 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
.git
.gitignore
.gitlab-ci.yml
.idea
.travis.yml

tmp
erl_crash.dump
*.ez

# Elixir
.elixir_ls
_build
deps

# Project-specific
rel/docker
resources
dev

# Secrets
apps/*/priv/secrets/*
!apps/*/priv/secrets/.keep
!apps/*/priv/secrets/.keep

# File types
tmp
erl_crash.dump
*.ez
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ apps/*/cover
until_fail.sh

# Secrets
**/secrets/
apps/*/priv/secrets/*
!apps/*/priv/secrets/.keep

# Elixir LS
.elixir_ls
28 changes: 11 additions & 17 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# ----- Config -----

stages:
stages:
- name: test
- name: release

env:
# Temporary build image
CF_API_BUILD_IMAGE: captain-fact-api-build:$TRAVIS_BRANCH
# Releases
CF_API_REST_IMAGE: captainfact/rest-api:$TRAVIS_BRANCH
CF_API_GRAPHQL_IMAGE: captainfact/graphql-api:$TRAVIS_BRANCH
CF_API_ATOM_FEED: captainfact/atom-feed:$TRAVIS_BRANCH
Expand All @@ -19,10 +16,10 @@ jobs:
include:
- stage: test
language: elixir
elixir: '1.6'
otp_release: '21.0'
cache: {paths: ["_build", "deps"]}
addons: {postgresql: "9.6"}
elixir: "1.6"
otp_release: "21.0"
cache: { paths: ["_build", "deps"] }
addons: { postgresql: "9.6" }
env:
- MIX_ENV=test
before_script:
Expand All @@ -42,15 +39,12 @@ jobs:
services: [docker]
script:
- set -e
- # ---- Build ----
- docker build -t ${CF_API_BUILD_IMAGE} -f ./rel/docker/Dockerfile.build .
- ./rel/docker/build_release.sh
${CF_API_BUILD_IMAGE}
${CF_API_REST_IMAGE}
${CF_API_GRAPHQL_IMAGE}
${CF_API_ATOM_FEED}
${CF_API_OPENGRAPH_IMAGE}
- # ---- Push release ----
- # ---- Build ----
- docker build --build-arg APP=captain_fact -t ${CF_API_REST_IMAGE} .
- docker build --build-arg APP=cf_graphql -t ${CF_API_GRAPHQL_IMAGE} .
- docker build --build-arg APP=cf_atom_feed -t ${CF_API_ATOM_FEED} .
- docker build --build-arg APP=cf_opengraph -t ${CF_API_OPENGRAPH_IMAGE} .
- # ---- Push release ----
- docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
- docker push $CF_API_REST_IMAGE
- docker push $CF_API_GRAPHQL_IMAGE
Expand Down
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM bitwalker/alpine-elixir:1.6.6
RUN apk add bash imagemagick curl gcc make libc-dev libgcc && rm -rf /var/cache/apk/*

ENV HOME=/opt/app/ SHELL=/bin/bash MIX_ENV=prod
WORKDIR /opt/build

ARG APP

# Cache dependencies
COPY mix.exs mix.lock ./
COPY apps/captain_fact/mix.exs ./apps/captain_fact/
COPY apps/cf_atom_feed/mix.exs ./apps/cf_atom_feed/
COPY apps/cf_graphql/mix.exs ./apps/cf_graphql/
COPY apps/cf_opengraph/mix.exs ./apps/cf_opengraph/
COPY apps/cf_utils/mix.exs ./apps/cf_utils/
COPY apps/db/mix.exs ./apps/db/
RUN HEX_HTTP_CONCURRENCY=4 HEX_HTTP_TIMEOUT=180 mix deps.get

COPY . .
RUN mix release --name ${APP} --env=$MIX_ENV

WORKDIR /opt/app
RUN cp -R /opt/build/_build/$MIX_ENV/rel/${APP}/* /opt/app/
RUN rm -rf /opt/build
RUN ln -s /opt/app/bin/${APP} bin/entrypoint

EXPOSE 80
ENTRYPOINT ["./bin/entrypoint"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

### Start DB

Create / launch a postrges instance on your local machine. If you have
Create / launch a postgres instance on your local machine. If you have
docker installed, you can use the pre-seed postgres docker image:

`docker run -d --name cf_dev_db -p 5432:5432 captainfact/dev-db:latest`

### Start API services

* Without Docker
* Without Docker (recommended if you want to make changes in the API)
- `mix deps.get`
- `mix ecto.migrate`
- `iex -S mix`
Expand Down
3 changes: 3 additions & 0 deletions apps/captain_fact/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ config :captain_fact, CaptainFact.Authenticator.Guardian,
serializer: CaptainFact.Accounts.GuardianSerializer,
permissions: %{default: [:read, :write]}

config :captain_fact,
captions_fetcher: CaptainFact.Videos.CaptionsFetcherYoutube

config :guardian, Guardian.DB, repo: DB.Repo

config :weave,
Expand Down
14 changes: 1 addition & 13 deletions apps/captain_fact/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@ dev_secret = "8C6FsJwjV11d+1WPUIbkEH6gB/VavJrcXWoPLujgpclfxjkLkoNFSjVU9XfeNm6s"
# General config
config :captain_fact,
frontend_url: "http://localhost:3333",
cors_origins: [
# Frontend Dev
"http://localhost:3333",
"https://localhost:3333",
"http://192.168.20.6:3333",
# Overlay injector tester
"http://localhost:3342",
# Misc
"http://localhost",
"https://localhost",
# Extension
"chrome-extension://fnnhlmbnlbgomamcolcpgncflofhjckm"
],
cors_origins: "*",
oauth: [
facebook: [
client_id: "506726596325615",
Expand Down
6 changes: 4 additions & 2 deletions apps/captain_fact/config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use Mix.Config

# --------------------------------------------------------------------------------------
# Prod config / secrets are set at runtime using weave. See `lib/captain_fact/weave.ex`
# Prod config / secrets are set at runtime using weave.
# See `lib/captain_fact/weave.ex`
# --------------------------------------------------------------------------------------

config :captain_fact, CaptainFactWeb.Endpoint,
url: [port: 80],
http: [port: 80],
force_ssl: false
force_ssl: false,
check_origin: []

# Do not print debug messages in production
config :logger, level: :info
Expand Down
4 changes: 4 additions & 0 deletions apps/captain_fact/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ config :captain_fact, CaptainFactMailer, adapter: Bamboo.TestAdapter

# Reduce the number of round for encryption during tests
config :bcrypt_elixir, :log_rounds, 4

# Captions mock for testing
config :captain_fact,
captions_fetcher: CaptainFact.Videos.CaptionsFetcherTest
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule CaptainFact.Authenticator do
@moduledoc """
Handle all authentication intelligence
"""
import Ecto.Query

alias DB.Repo
alias DB.Schema.User
Expand All @@ -10,11 +11,16 @@ defmodule CaptainFact.Authenticator do
alias Kaur.Result

@doc """
Get user from its email address and check password.
Get user from its email address or user name and check password.
Returns nil if no User for email or if password is invalid.
"""
def get_user_for_email_password(email, password) do
with user when not is_nil(user) <- Repo.get_by(User, email: email),
def get_user_for_email_or_name_password(email_or_name, password) do
user =
User
|> where([u], u.email == ^email_or_name or u.username == ^email_or_name)
|> Repo.one()

with user when not is_nil(user) <- user,
true <- validate_pass(user.encrypted_password, password) do
user
else
Expand Down
8 changes: 8 additions & 0 deletions apps/captain_fact/lib/captain_fact/videos/captions_fetcher.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule CaptainFact.Videos.CaptionsFetcher do
@moduledoc """
Fetch captions for videos.
"""

@callback fetch(String.t(), String.t()) ::
{:ok, DB.Schema.VideoCaption.t()} | {:error, binary()}
end
17 changes: 17 additions & 0 deletions apps/captain_fact/lib/captain_fact/videos/captions_fetcher_test.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule CaptainFact.Videos.CaptionsFetcherTest do
@moduledoc """
A mock for faking captions fetching requests.
"""

@behaviour CaptainFact.Videos.CaptionsFetcher

@impl true
def fetch(_provider_id, _locale) do
captions = %DB.Schema.VideoCaption{
content: "__TEST-CONTENT__",
format: "xml"
}

{:ok, captions}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule CaptainFact.Videos.CaptionsFetcherYoutube do
@moduledoc """
A captions fetcher for YouTube.
"""

@behaviour CaptainFact.Videos.CaptionsFetcher

@impl true
def fetch(youtube_id, locale) do
with {:ok, content} <- fetch_captions_content(youtube_id, locale) do
captions = %DB.Schema.VideoCaption{
content: content,
format: "xml"
}

{:ok, captions}
end
end

defp fetch_captions_content(video_id, locale) do
case HTTPoison.get("http://video.google.com/timedtext?lang=#{locale}&v=#{video_id}") do
{:ok, %HTTPoison.Response{status_code: 200, body: ""}} ->
{:error, :not_found}

{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
{:ok, body}

{:ok, %HTTPoison.Response{status_code: 404}} ->
{:error, :not_found}

{:ok, %HTTPoison.Response{status_code: _}} ->
{:error, :unknown}

{:error, %HTTPoison.Error{reason: reason}} ->
{:error, reason}
end
end
end
22 changes: 22 additions & 0 deletions apps/captain_fact/lib/captain_fact/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule CaptainFact.Videos do

import Ecto.Query, warn: false
import CaptainFact.Videos.MetadataFetcher
import CaptainFact.Videos.CaptionsFetcher

alias Ecto.Multi
alias DB.Repo
Expand All @@ -13,10 +14,13 @@ defmodule CaptainFact.Videos do
alias DB.Schema.Statement
alias DB.Schema.Speaker
alias DB.Schema.VideoSpeaker
alias DB.Schema.VideoCaption

alias CaptainFact.Actions.ActionCreator
alias CaptainFact.Accounts.UserPermissions

@captions_fetcher Application.get_env(:captain_fact, :captions_fetcher)

@doc """
TODO with_speakers param is only required by REST API
List videos. `filters` may contain the following entries:
Expand Down Expand Up @@ -153,4 +157,22 @@ defmodule CaptainFact.Videos do

Enum.group_by(Repo.all(query), &elem(&1, 0), &elem(&1, 1))
end

@doc """
Download and store captions for a video.
Returns captions if success or {:error, reason} if something bad happend.
Usage:
iex> download_captions(video)
iex> download_captions(video)
"""
def download_captions(video = %Video{}) do
with {:ok, captions} <- @captions_fetcher.fetch(video.provider_id, video.language) do
captions
|> VideoCaption.changeset(%{video_id: video.id})
|> Repo.insert()

{:ok, captions}
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ defmodule CaptainFactWeb.StatementsChannel do
Shift all video's statements
"""
def handle_in_authenticated!("shift_all", offset, socket) do
user = Repo.get(DB.Schema.User, socket.assigns.user_id)

case Videos.shift_statements(
socket.assigns.user_id,
user,
socket.assigns.video_id,
String.to_integer(offset)
) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ defmodule CaptainFactWeb.AuthController do
@err_invalid_email_password "invalid_email_password"

@doc """
Auth with identity (email + password)
Auth with identity (name|email + password)
"""
def callback(conn, %{"provider" => "identity", "email" => email, "password" => password}) do
case Authenticator.get_user_for_email_password(email, password) do
case Authenticator.get_user_for_email_or_name_password(email, password) do
nil ->
conn
|> put_status(:unauthorized)
Expand Down
9 changes: 8 additions & 1 deletion apps/captain_fact/lib/captain_fact_web/cors.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
defmodule CaptainFactWeb.CORS do
@spec check_origin(String.t()) :: boolean()
def check_origin(origin) do
origin in Application.get_env(:captain_fact, :cors_origins)
case Application.get_env(:captain_fact, :cors_origins) do
"*" ->
true

origins ->
origin in origins
end
end
end

0 comments on commit 6945766

Please sign in to comment.