Skip to content
Open
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
4 changes: 4 additions & 0 deletions xx_ex_ussd/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
import_deps: [:phoenix],
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"]
]
28 changes: 28 additions & 0 deletions xx_ex_ussd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
app-*.tar

# Since we are building assets from assets/,
# we ignore priv/static. You may want to comment
# this depending on your deployment strategy.
/priv/static/
18 changes: 18 additions & 0 deletions xx_ex_ussd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# App

To start your Phoenix server:

* Install dependencies with `mix deps.get`
* Start Phoenix endpoint with `mix phx.server`

Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.

Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html).

## Learn more

* Official website: https://www.phoenixframework.org/
* Guides: https://hexdocs.pm/phoenix/overview.html
* Docs: https://hexdocs.pm/phoenix
* Forum: https://elixirforum.com/c/phoenix-forum
* Source: https://github.com/phoenixframework/phoenix
27 changes: 27 additions & 0 deletions xx_ex_ussd/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.

# General application configuration
use Mix.Config

# Configures the endpoint
config :app, AppWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "3PdUemfBrX7aJXqxVnAXfE0kYAOCBKeCrTVhzlOXUxcYWbnIkoVyzfvqMkonZ8lL",
render_errors: [view: AppWeb.ErrorView, accepts: ~w(json), layout: false],
pubsub_server: App.PubSub

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

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
22 changes: 22 additions & 0 deletions xx_ex_ussd/config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use Mix.Config

config :app, AppWeb.Endpoint,
http: [port: 4000],
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: []

config :ex_ussd_simulator,
callback_url: "http://localhost:4000/api/callback",
service_code: "*456#"

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime
55 changes: 55 additions & 0 deletions xx_ex_ussd/config/prod.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use Mix.Config

# For production, don't forget to configure the url host
# to something meaningful, Phoenix uses this information
# when generating URLs.
#
# Note we also include the path to a cache manifest
# containing the digested version of static files. This
# manifest is generated by the `mix phx.digest` task,
# which you should run after static files are built and
# before starting your production server.
config :app, AppWeb.Endpoint,
url: [host: "example.com", port: 80],
cache_static_manifest: "priv/static/cache_manifest.json"

# Do not print debug messages in production
config :logger, level: :info

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
# to the previous section and set your `:url` port to 443:
#
# config :app, AppWeb.Endpoint,
# ...
# url: [host: "example.com", port: 443],
# https: [
# port: 443,
# cipher_suite: :strong,
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH"),
# transport_options: [socket_opts: [:inet6]]
# ]
#
# The `cipher_suite` is set to `:strong` to support only the
# latest and more secure SSL ciphers. This means old browsers
# and clients may not be supported. You can set it to
# `:compatible` for wider support.
#
# `:keyfile` and `:certfile` expect an absolute path to the key
# and cert in disk or a relative path inside priv, for example
# "priv/ssl/server.key". For all supported SSL configuration
# options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
#
# We also recommend setting `force_ssl` in your endpoint, ensuring
# no data is ever sent via http, always redirecting to https:
#
# config :app, AppWeb.Endpoint,
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.

# Finally import the config/prod.secret.exs which loads secrets
# and configuration from environment variables.
import_config "prod.secret.exs"
29 changes: 29 additions & 0 deletions xx_ex_ussd/config/prod.secret.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# In this file, we load production configuration and secrets
# from environment variables. You can also hardcode secrets,
# although such is generally not recommended and you have to
# remember to add this file to your .gitignore.
use Mix.Config

secret_key_base =
System.get_env("SECRET_KEY_BASE") ||
raise """
environment variable SECRET_KEY_BASE is missing.
You can generate one by calling: mix phx.gen.secret
"""

config :app, AppWeb.Endpoint,
http: [
port: String.to_integer(System.get_env("PORT") || "4000"),
transport_options: [socket_opts: [:inet6]]
],
secret_key_base: secret_key_base

# ## Using releases (Elixir v1.9+)
#
# If you are doing OTP releases, you need to instruct Phoenix
# to start each relevant endpoint:
#
# config :app, AppWeb.Endpoint, server: true
#
# Then you can assemble a release by calling `mix release`.
# See `mix help release` for more information.
10 changes: 10 additions & 0 deletions xx_ex_ussd/config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use Mix.Config

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :app, AppWeb.Endpoint,
http: [port: 4002],
server: false

# Print only warnings and errors during test
config :logger, level: :warn
9 changes: 9 additions & 0 deletions xx_ex_ussd/lib/app.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule App do
@moduledoc """
App keeps the contexts that define your domain
and business logic.

Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
end
32 changes: 32 additions & 0 deletions xx_ex_ussd/lib/app/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule App.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false

use Application

def start(_type, _args) do
children = [
# Start the Telemetry supervisor
AppWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: App.PubSub},
# Start the Endpoint (http/https)
AppWeb.Endpoint
# Start a worker by calling: App.Worker.start_link(arg)
# {App.Worker, arg}
]

# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: App.Supervisor]
Supervisor.start_link(children, opts)
end

# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
AppWeb.Endpoint.config_change(changed, removed)
:ok
end
end
75 changes: 75 additions & 0 deletions xx_ex_ussd/lib/app_web.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule AppWeb do
@moduledoc """
The entrypoint for defining your web interface, such
as controllers, views, channels and so on.

This can be used in your application as:

use AppWeb, :controller
use AppWeb, :view

The definitions below will be executed for every view,
controller, etc, so keep them short and clean, focused
on imports, uses and aliases.

Do NOT define functions inside the quoted expressions
below. Instead, define any helper function in modules
and import those modules here.
"""

def controller do
quote do
use Phoenix.Controller, namespace: AppWeb

import Plug.Conn
alias AppWeb.Router.Helpers, as: Routes
end
end

def view do
quote do
use Phoenix.View,
root: "lib/app_web/templates",
namespace: AppWeb

# Import convenience functions from controllers
import Phoenix.Controller,
only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]

# Include shared imports and aliases for views
unquote(view_helpers())
end
end

def router do
quote do
use Phoenix.Router

import Plug.Conn
import Phoenix.Controller
end
end

def channel do
quote do
use Phoenix.Channel
end
end

defp view_helpers do
quote do
# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View

import AppWeb.ErrorHelpers
alias AppWeb.Router.Helpers, as: Routes
end
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
21 changes: 21 additions & 0 deletions xx_ex_ussd/lib/app_web/controllers/callback_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule AppWeb.CallbackController do
use AppWeb, :controller

alias AppWeb.Handlers.Home

def callback(
conn,
%{"text" => text, "sessionId" => session_id, "serviceCode" => service_code} = request
) do
menu = ExUssd.Menu.render(name: "Home", handler: Home)

{:ok, response} =
ExUssd.goto(
internal_routing: %{text: text, session_id: session_id, service_code: service_code},
menu: menu,
api_parameters: request
)

send_resp(conn, 200, response)
end
end
41 changes: 41 additions & 0 deletions xx_ex_ussd/lib/app_web/endpoint.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule AppWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :app

# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
@session_options [
store: :cookie,
key: "_app_key",
signing_salt: "we8N9jCm"
]

# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phx.digest
# when deploying your static files in production.
plug Plug.Static,
at: "/",
from: :app,
gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
plug Phoenix.CodeReloader
end

plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]

plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()

plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug AppWeb.Router
end
13 changes: 13 additions & 0 deletions xx_ex_ussd/lib/app_web/router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule AppWeb.Router do
use AppWeb, :router

pipeline :api do
plug :accepts, ["json"]
end

scope "/api", AppWeb do
pipe_through :api

post "/callback", CallbackController, :callback
end
end
Loading