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

feat: sqlite #17

Merged
merged 2 commits into from
Nov 22, 2023
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ harpoon-*.tar
# In case you use Node.js/npm, you want to ignore these.
npm-debug.log
/assets/node_modules/

/priv/data
2 changes: 1 addition & 1 deletion .prod.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DATABASE_URL="psql://postgres:postgres@db/harpoon_prod"
DATABASE_FILE="/app/data/harpoon_prod.sqlite"
SECRET_KEY_BASE="vFZutSk8Cyq+CoJwIuHhqeJ7Y9OBf3LGVoQ9hm/IkfHU5MVa/BmJTZOYLyeOutC6"
PHX_SERVER="true"
PHX_HOST="localhost"
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,6 @@ USER nobody
# above and adding an entrypoint. See https://github.com/krallin/tini for details
# ENTRYPOINT ["/tini", "--"]

ENV DATABASE_FILE /app/data/harpoon_prod.sqlite

CMD ["/app/bin/server"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The tool is intended for testing, debugging, and monitoring webhooks, APIs, and

- Create temporary endpoints for testing and monitoring HTTP requests.
- Inspect HTTP requests in real-time using Phoenix LiveView.
- Store request data in a PostgreSQL database for future analysis and reference.
- Store request data in a Sqlite database for easy analysis and setup.
- Lightweight and easy to use.

## Prerequisites
Expand All @@ -40,7 +40,7 @@ The tool is intended for testing, debugging, and monitoring webhooks, APIs, and
### Local Version
- Elixir
- Erlang
- PostgreSQL
- Sqlite

## Installation

Expand Down
9 changes: 0 additions & 9 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ config :harpoon, HarpoonWeb.Endpoint,
pubsub_server: Harpoon.PubSub,
live_view: [signing_salt: "Rl8aH79d"]

# Configures the mailer
#
# By default it uses the "Local" adapter which stores the emails
# locally. You can see the emails in your browser, at "/dev/mailbox".
#
# For production it's recommended to configure a different adapter
# at the `config/runtime.exs`.
config :harpoon, Harpoon.Mailer, adapter: Swoosh.Adapters.Local

# Configure esbuild (the version is required)
config :esbuild,
version: "0.17.11",
Expand Down
8 changes: 1 addition & 7 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import Config

# Configure your database
config :harpoon, Harpoon.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "harpoon_dev",
database: "priv/data/harpoon_dev.sqlite",
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
Expand Down Expand Up @@ -77,6 +74,3 @@ config :phoenix, :plug_init_mode, :runtime

# Include HEEx debug annotations as HTML comments in rendered markup
config :phoenix_live_view, :debug_heex_annotations, true

# Disable swoosh api client as it is only required for production adapters.
config :swoosh, :api_client, false
6 changes: 0 additions & 6 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import Config
# before starting your production server.
config :harpoon, HarpoonWeb.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"

# Configures Swoosh API Client
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: Harpoon.Finch

# Disable Swoosh Local Memory Storage
config :swoosh, local: false

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

Expand Down
32 changes: 2 additions & 30 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,10 @@ if System.get_env("PHX_SERVER") do
end

if config_env() == :prod do
database_url =
System.get_env("DATABASE_URL") ||
raise """
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
"""

maybe_ipv6 = if System.get_env("ECTO_IPV6") in ~w(true 1), do: [:inet6], else: []

config :harpoon, Harpoon.Repo,
# ssl: true,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
socket_options: maybe_ipv6
database: System.get_env("DATABASE_FILE", "~/.harpoon/harpoon_prod.sqlite"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

# The secret key base is used to sign/encrypt cookies and other secrets.
# A default value is used in config/dev.exs and config/test.exs but you
Expand Down Expand Up @@ -96,22 +86,4 @@ if config_env() == :prod do
# force_ssl: [hsts: true]
#
# Check `Plug.SSL` for all available options in `force_ssl`.

# ## Configuring the mailer
#
# In production you need to configure the mailer to use a different adapter.
# Also, you may need to configure the Swoosh API client of your choice if you
# are not using SMTP. Here is an example of the configuration:
#
# config :harpoon, Harpoon.Mailer,
# adapter: Swoosh.Adapters.Mailgun,
# api_key: System.get_env("MAILGUN_API_KEY"),
# domain: System.get_env("MAILGUN_DOMAIN")
#
# For this example you need include a HTTP client required by Swoosh API client.
# Swoosh supports Hackney and Finch out of the box:
#
# config :swoosh, :api_client, Swoosh.ApiClient.Hackney
#
# See https://hexdocs.pm/swoosh/Swoosh.html#module-installation for details.
end
11 changes: 1 addition & 10 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import Config
# to provide built-in test partitioning in CI environment.
# Run `mix help test` for more information.
config :harpoon, Harpoon.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "harpoon_test#{System.get_env("MIX_TEST_PARTITION")}",
database: "priv/data/harpoon_test#{System.get_env("MIX_TEST_PARTITION")}",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: 10

Expand All @@ -20,12 +17,6 @@ config :harpoon, HarpoonWeb.Endpoint,
secret_key_base: "6AyjzUWkEzxZTWHtark38i+9IbfkFh6LOcs4t/rylbncz8PxRHOw5EmHQrrP+GyC",
server: false

# In test we don't send emails.
config :harpoon, Harpoon.Mailer, adapter: Swoosh.Adapters.Test

# Disable swoosh api client as it is only required for production adapters.
config :swoosh, :api_client, false

# Print only warnings and errors during test
config :logger, level: :warning

Expand Down
9 changes: 0 additions & 9 deletions docker-compose-dev.yaml

This file was deleted.

18 changes: 0 additions & 18 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
version: "3"
services:
db:
image: postgres:16.0
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: harpoon_prod

migration:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile
env_file: .prod.env
command: ["/app/bin/migrate"]

app:
build:
context: .
dockerfile: Dockerfile
depends_on:
- migration
env_file: .prod.env
ports:
- 4000:4000
6 changes: 2 additions & 4 deletions lib/harpoon/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ defmodule Harpoon.Application do
use Application

alias Harpoon.Workers.CapturedRequestsWorker
alias Harpoon.Workers.MigrationRunnerWorker

@impl true
def start(_type, _args) do
children = [
HarpoonWeb.Telemetry,
Harpoon.Repo,
MigrationRunnerWorker,
{DNSCluster, query: Application.get_env(:harpoon, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: Harpoon.PubSub},
# Start the Finch HTTP client for sending emails
{Finch, name: Harpoon.Finch},
CapturedRequestsWorker,
# Start a worker by calling: Harpoon.Worker.start_link(arg)
# {Harpoon.Worker, arg},
# Start to serve requests, typically the last entry
HarpoonWeb.Endpoint
]

Expand Down
4 changes: 0 additions & 4 deletions lib/harpoon/mailer.ex

This file was deleted.

6 changes: 4 additions & 2 deletions lib/harpoon/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ defmodule Harpoon.Release do
"""
@app :harpoon

def migrate do
load_app()
def migrate(should_load_app \\ true) do
if should_load_app do
load_app()
end

for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
Expand Down
2 changes: 1 addition & 1 deletion lib/harpoon/repo.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Harpoon.Repo do
use Ecto.Repo,
otp_app: :harpoon,
adapter: Ecto.Adapters.Postgres
adapter: Ecto.Adapters.SQLite3
end
20 changes: 20 additions & 0 deletions lib/harpoon/workers/migration_runner_worker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Harpoon.Workers.MigrationRunnerWorker do
@moduledoc false
use GenServer, restart: :transient

def start_link(_) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end

def init(_args) do
Harpoon.Repo
|> Ecto.Migrator.migrations()
|> Enum.any?(&match?({:down, _, _}, &1))
|> case do
true -> Harpoon.Release.migrate(false)
false -> :ok
end

:ignore
end
end
3 changes: 1 addition & 2 deletions lib/harpoon_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule HarpoonWeb.Router do
# pipe_through :api
# end

# Enable LiveDashboard and Swoosh mailbox preview in development
# Enable LiveDashboard preview in development
if Application.compile_env(:harpoon, :dev_routes) do
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
Expand All @@ -40,7 +40,6 @@ defmodule HarpoonWeb.Router do
pipe_through :browser

live_dashboard "/dashboard", metrics: HarpoonWeb.Telemetry
forward "/mailbox", Plug.Swoosh.MailboxPreview
end
end
end
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ defmodule Harpoon.MixProject do
{:phoenix, "~> 1.7.9"},
{:phoenix_ecto, "~> 4.4"},
{:ecto_sql, "~> 3.10"},
{:postgrex, ">= 0.0.0"},
{:ecto_sqlite3, "~> 0.12.0"},
{:phoenix_html, "~> 3.3"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.20.1"},
{:floki, ">= 0.30.0", only: :test},
{:phoenix_live_dashboard, "~> 0.8.2"},
{:esbuild, "~> 0.7", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev},
{:swoosh, "~> 1.3"},
{:finch, "~> 0.13"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
Expand Down
Loading