Skip to content

Commit

Permalink
chore: try url domain, resource
Browse files Browse the repository at this point in the history
  • Loading branch information
fivehanz committed Aug 1, 2024
1 parent ea1bc43 commit a108ef8
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ npm-debug.log
/assets/node_modules/

.env*
.DS_Store
39 changes: 11 additions & 28 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Config

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

# Configure your database
config :qwynk, Qwynk.Repo,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "qwynk_dev",
url: database_url,
stacktrace: true,
show_sensitive_data_on_connection_error: true,
pool_size: 10
Expand All @@ -19,38 +23,17 @@ config :qwynk, Qwynk.Repo,
config :qwynk, QwynkWeb.Endpoint,
# Binding to loopback ipv4 address prevents access from other machines.
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
http: [ip: {127, 0, 0, 1}, port: 4000],
http: [ip: {0, 0, 0, 0}, port: 4000],
check_origin: false,
code_reloader: true,
debug_errors: true,
secret_key_base: "YNPKe8GUFA25IC1RsI9b+tp8nRrKuvXPqveigrWofk6MVZSmZGvelIhVs36E49P7",
secret_key_base: System.get_env("SECRET_KEY_BASE"),
watchers: [
esbuild: {Esbuild, :install_and_run, [:qwynk, ~w(--sourcemap=inline --watch)]},
tailwind: {Tailwind, :install_and_run, [:qwynk, ~w(--watch)]}
]

# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task:
#
# mix phx.gen.cert
#
# Run `mix help phx.gen.cert` for more information.
#
# The `http:` config above can be replaced with:
#
# https: [
# port: 4001,
# cipher_suite: :strong,
# keyfile: "priv/cert/selfsigned_key.pem",
# certfile: "priv/cert/selfsigned.pem"
# ],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.


# Watch static and templates for browser reloading.
config :qwynk, QwynkWeb.Endpoint,
Expand Down
46 changes: 46 additions & 0 deletions dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ARG ELIXIR_VERSION=1.17.2
ARG OTP_VERSION=27.0.1
ARG DEBIAN_VERSION=bookworm-20240722-slim
ARG BUN_VERSION=1.1.21

ARG ASSETS_BUILDER_IMAGE="oven/bun:${BUN_VERSION}-slim"
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"

################################################################################

FROM ${ASSETS_BUILDER_IMAGE} as assets_builder

WORKDIR /app

COPY assets assets
RUN cd assets && bun --bun install

################################################################################

FROM ${BUILDER_IMAGE} as builder

# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git curl unzip inotify-tools \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*

# prepare build dir
WORKDIR /app

# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force

# set build ENV
ENV MIX_ENV="dev"

#! https://elixirforum.com/t/arm64-dockerfile-failing/57317/12
ENV ERL_FLAGS="+JPperf true"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get

COPY --from=assets_builder /app/assets assets

CMD ["mix", "phx.server"]

45 changes: 45 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
volumes:
qwynk_postgres_data:

networks:
qwynk-nw:
driver: bridge

services:
web:
container_name: qwynk-web
build:
context: .
dockerfile: dev.Dockerfile
ports:
- "4000:4000"
volumes:
- .:/app
- ./deps:/app/deps
- ./_build:/app/_build
depends_on:
- db
env_file:
- .env
environment:
DATABASE_URL: postgres://user:password@db/qwynk_db
SECRET_KEY_BASE: e9Etkgml4tYuWm2KR90WMKn8PLUGd0RV08FnZ+XFpHcK02PeGIu3E364tkfSU0BR
MIX_ENV: dev
networks:
- qwynk-nw

db:
image: postgres:16-alpine
volumes:
- qwynk_postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=qwynk_db
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
networks:
- qwynk-nw

# redis:
# image: redis:7-alpine
# networks:
# - twizl-nw
10 changes: 7 additions & 3 deletions lib/qwynk/repo.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
defmodule Qwynk.Repo do
use Ecto.Repo,
otp_app: :qwynk,
adapter: Ecto.Adapters.Postgres
use AshPostgres.Repo,
otp_app: :qwynk

# Installs extensions that ash commonly uses
def installed_extensions do
["ash-functions", "uuid-ossp", "citext"]
end
end
11 changes: 11 additions & 0 deletions lib/qwynk/url_shortener/url_shortener.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Qwynk.UrlShortener do
use Ash.Domain

resources do
resource Qwynk.UrlShortener.Urls do
end

# resource Qwynk.UrlShortener.Analytics do
# end
end
end
41 changes: 41 additions & 0 deletions lib/qwynk/url_shortener/urls.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Qwynk.UrlShortener.Urls do
use Ash.Resource,
domain: Qwynk.UrlShortener,
data_layer: AshPostgres.DataLayer

postgres do
table "urls"
repo Qwynk.Repo
end

actions do
defaults [:read, :create, :update, :destroy]

create :shorten do
accept [:original_url]
# change fn changeset ->
# changeset
# |> Ash.Changeset.change([shortened_url: Qwynk.UrlShortener.Generators.Slug.generate()])
# |> Qwynk.UrlShortener.Services.UrlValidator.sanitize_inputs()
# end
end

read :get do
argument :shortened_url, :string, allow_nil?: false
get? true
filter expr(shortened_url == ^arg(:shortened_url))
end
end

attributes do
uuid_primary_key :id
attribute :original_url, :string do
allow_nil? false
end
attribute :shortened_url, :string do
# generate fn _ ->
# Qwynk.UrlShortener.Generators.Slug.generate()
# end
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Qwynk.MixProject do
def application do
[
mod: {Qwynk.Application, []},
extra_applications: [:logger, :runtime_tools]
extra_applications: [:logger, :runtime_tools, :os_mon]
]
end

Expand Down

0 comments on commit a108ef8

Please sign in to comment.