Skip to content

Commit

Permalink
Upgraded Phoenix (#75)
Browse files Browse the repository at this point in the history
* removed old web app

* added base phoenix application

* ignoring secret files

* ignoring secret files

* added back web files

* removed unused files

* formatting

* checkpoint

* checkpoint
  • Loading branch information
lucapasquale authored Feb 5, 2024
1 parent 7f850ab commit 98fcf5e
Show file tree
Hide file tree
Showing 51 changed files with 1,362 additions and 473 deletions.
6 changes: 4 additions & 2 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
import_deps: [:ecto, :ecto_sql, :phoenix],
subdirectories: ["priv/*/migrations"],
plugins: [Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/*/seeds.exs"]
]
17 changes: 9 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# The directory Mix downloads your dependencies sources to.
/deps/

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

# Ignore .fetch files in case you like to edit your project deps locally.
Expand All @@ -19,20 +19,21 @@ erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Temporary files, for example, from tests.
/tmp/

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

# Temporary files for e.g. tests
/tmp

# Ignore assets that are produced by build tools.
/priv/static/assets/

# Ignore digested assets cache.
/priv/static/cache_manifest.json

# Secrets
config/*.secret.exs
# In case you use Node.js/npm, you want to ignore these.
npm-debug.log
/assets/node_modules/

# ElixirLS
.elixir_ls
# Ignore secret env vars
/config/dev.secret.exs
2 changes: 2 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";

/* This file is for your main application CSS */
Binary file removed assets/favicon.ico
Binary file not shown.
Binary file removed assets/images/twitch_example.png
Binary file not shown.
Binary file removed assets/images/youtube_example.png
Binary file not shown.
12 changes: 6 additions & 6 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// We import the CSS which is extracted to its own file by esbuild.
// Remove this line if you add a your own CSS build pipeline (e.g postcss).

// If you want to use Phoenix channels, run `mix help phx.gen.channel`
// to get started and then uncomment the line below.
// import "./user_socket.js"
Expand All @@ -26,12 +23,15 @@ import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
let liveSocket = new LiveSocket("/live", Socket, {
longPollFallbackMs: 2500,
params: {_csrf_token: csrfToken}
})

// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", info => topbar.show())
window.addEventListener("phx:page-loading-stop", info => topbar.hide())
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())

// connect if there are any LiveViews on the page
liveSocket.connect()
Expand Down
5 changes: 0 additions & 5 deletions assets/robots.txt

This file was deleted.

72 changes: 64 additions & 8 deletions assets/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,80 @@
const colors = require("tailwindcss/colors");

// See the Tailwind configuration guide for advanced usage
// https://tailwindcss.com/docs/configuration

const colors = require("tailwindcss/colors")
const plugin = require("tailwindcss/plugin")
const fs = require("fs")
const path = require("path")

module.exports = {
darkMode: "class",
content: [
'./js/**/*.js',
'../lib/*_web.ex',
'../lib/*_web/**/*.*ex',
"../deps/petal_components/**/*.*ex",
"./js/**/*.js",
"../lib/botchini_web.ex",
"../lib/botchini_web/**/*.*ex"
],
theme: {
extend: {
colors: {
brand: "#FD4F00",

primary: colors.blue,
secondary: colors.pink,
},
}
},
},
plugins: [
require('@tailwindcss/forms')
require("@tailwindcss/forms"),
// Allows prefixing tailwind classes with LiveView classes to add rules
// only when LiveView classes are applied, for example:
//
// <div class="phx-click-loading:animate-ping">
//
plugin(({addVariant}) => addVariant("phx-no-feedback", [".phx-no-feedback&", ".phx-no-feedback &"])),
plugin(({addVariant}) => addVariant("phx-click-loading", [".phx-click-loading&", ".phx-click-loading &"])),
plugin(({addVariant}) => addVariant("phx-submit-loading", [".phx-submit-loading&", ".phx-submit-loading &"])),
plugin(({addVariant}) => addVariant("phx-change-loading", [".phx-change-loading&", ".phx-change-loading &"])),

// Embeds Heroicons (https://heroicons.com) into your app.css bundle
// See your `CoreComponents.icon/1` for more information.
//
plugin(function({matchComponents, theme}) {
let iconsDir = path.join(__dirname, "../deps/heroicons/optimized")
let values = {}
let icons = [
["", "/24/outline"],
["-solid", "/24/solid"],
["-mini", "/20/solid"],
["-micro", "/16/solid"]
]
icons.forEach(([suffix, dir]) => {
fs.readdirSync(path.join(iconsDir, dir)).forEach(file => {
let name = path.basename(file, ".svg") + suffix
values[name] = {name, fullPath: path.join(iconsDir, dir, file)}
})
})
matchComponents({
"hero": ({name, fullPath}) => {
let content = fs.readFileSync(fullPath).toString().replace(/\r?\n|\r/g, "")
let size = theme("spacing.6")
if (name.endsWith("-mini")) {
size = theme("spacing.5")
} else if (name.endsWith("-micro")) {
size = theme("spacing.4")
}
return {
[`--hero-${name}`]: `url('data:image/svg+xml;utf8,${content}')`,
"-webkit-mask": `var(--hero-${name})`,
"mask": `var(--hero-${name})`,
"mask-repeat": "no-repeat",
"background-color": "currentColor",
"vertical-align": "middle",
"display": "inline-block",
"width": size,
"height": size
}
}
}, {values})
})
]
}
42 changes: 25 additions & 17 deletions assets/vendor/topbar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license MIT
* topbar 1.0.0, 2021-01-06
* topbar 2.0.0, 2023-02-04
* https://buunguyen.github.io/topbar
* Copyright (c) 2021 Buu Nguyen
*/
Expand Down Expand Up @@ -35,10 +35,11 @@
})();

var canvas,
progressTimerId,
fadeTimerId,
currentProgress,
showing,
progressTimerId = null,
fadeTimerId = null,
delayTimerId = null,
addEvent = function (elem, type, handler) {
if (elem.addEventListener) elem.addEventListener(type, handler, false);
else if (elem.attachEvent) elem.attachEvent("on" + type, handler);
Expand Down Expand Up @@ -95,21 +96,26 @@
for (var key in opts)
if (options.hasOwnProperty(key)) options[key] = opts[key];
},
show: function () {
show: function (delay) {
if (showing) return;
showing = true;
if (fadeTimerId !== null) window.cancelAnimationFrame(fadeTimerId);
if (!canvas) createCanvas();
canvas.style.opacity = 1;
canvas.style.display = "block";
topbar.progress(0);
if (options.autoRun) {
(function loop() {
progressTimerId = window.requestAnimationFrame(loop);
topbar.progress(
"+" + 0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2)
);
})();
if (delay) {
if (delayTimerId) return;
delayTimerId = setTimeout(() => topbar.show(), delay);
} else {
showing = true;
if (fadeTimerId !== null) window.cancelAnimationFrame(fadeTimerId);
if (!canvas) createCanvas();
canvas.style.opacity = 1;
canvas.style.display = "block";
topbar.progress(0);
if (options.autoRun) {
(function loop() {
progressTimerId = window.requestAnimationFrame(loop);
topbar.progress(
"+" + 0.05 * Math.pow(1 - Math.sqrt(currentProgress), 2)
);
})();
}
}
},
progress: function (to) {
Expand All @@ -125,6 +131,8 @@
return currentProgress;
},
hide: function () {
clearTimeout(delayTimerId);
delayTimerId = null;
if (!showing) return;
showing = false;
if (progressTimerId != null) {
Expand Down
53 changes: 38 additions & 15 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.

# General application configuration
import Config

config :botchini,
Expand All @@ -18,31 +25,37 @@ config :botchini, Botchini.Scheduler,
# Configures the endpoint
config :botchini, BotchiniWeb.Endpoint,
url: [host: "localhost"],
render_errors: [view: BotchiniWeb.ErrorView, accepts: ~w(html json), layout: false],
adapter: Bandit.PhoenixAdapter,
render_errors: [
formats: [html: BotchiniWeb.ErrorHTML, json: BotchiniWeb.ErrorJSON],
layout: false
],
pubsub_server: Botchini.PubSub,
live_view: [signing_salt: "ZIYuYInA"]
live_view: [signing_salt: "yFQUW8gj"]

# 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 :botchini, Botchini.Mailer, adapter: Swoosh.Adapters.Local

# Configure esbuild (the version is required)
config :esbuild,
version: "0.14.29",
default: [
version: "0.17.11",
botchini: [
args:
~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
cd: Path.expand("../assets", __DIR__),
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
]

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

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

# Configure tailwind (the version is required)
config :tailwind,
version: "3.1.2",
default: [
version: "3.4.0",
botchini: [
args: ~w(
--config=tailwind.config.js
--input=css/app.css
Expand All @@ -51,4 +64,14 @@ config :tailwind,
cd: Path.expand("../assets", __DIR__)
]

import_config "#{Mix.env()}.exs"
# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id, :interaction_data, :guild_id, :channel_id, :user_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 "#{config_env()}.exs"
12 changes: 7 additions & 5 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ if config_env() == :prod do
For example: ecto://USER:PASS@HOST/DATABASE
"""

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

config :botchini, Botchini.Repo,
ssl: false,
# ssl: true,
url: database_url,
socket_options: maybe_ipv6,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
socket_options: maybe_ipv6

# 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 All @@ -51,12 +51,14 @@ if config_env() == :prod do
host = System.get_env("PHX_HOST") || "example.com"
port = String.to_integer(System.get_env("PORT") || "4000")

config :botchini, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")

config :botchini, BotchiniWeb.Endpoint,
url: [host: host, port: 443, scheme: "https"],
http: [
# Enable IPv6 and bind on all interfaces.
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
# See the documentation on https://hexdocs.pm/bandit/Bandit.html#t:options/0
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
ip: {0, 0, 0, 0, 0, 0, 0, 0},
port: port
Expand Down
23 changes: 17 additions & 6 deletions lib/botchini/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ defmodule Botchini.Application do
def start(_type, _args) do
children =
[
BotchiniWeb.Telemetry,
Botchini.Repo,
Botchini.Cache,
Botchini.Scheduler,
# Start the Telemetry supervisor
BotchiniWeb.Telemetry,
# Start the PubSub system
Botchini.Services.Twitch.AuthMiddleware,
{DNSCluster, query: Application.get_env(:botchini, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: Botchini.PubSub},
BotchiniWeb.Endpoint,
# Twitch auth middleware
Botchini.Services.Twitch.AuthMiddleware
# Start the Finch HTTP client for sending emails
{Finch, name: Botchini.Finch},
# Start a worker by calling: Botchini.Worker.start_link(arg)
# {Botchini.Worker, arg},
# Start to serve requests, typically the last entry
BotchiniWeb.Endpoint
]
|> start_nostrum(Application.fetch_env!(:botchini, :environment))

Expand All @@ -30,4 +33,12 @@ defmodule Botchini.Application do

defp start_nostrum(children, :test), do: children
defp start_nostrum(children, _env), do: children ++ [BotchiniDiscord.Consumer]

# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
BotchiniWeb.Endpoint.config_change(changed, removed)
:ok
end
end
Loading

0 comments on commit 98fcf5e

Please sign in to comment.