Skip to content

Commit

Permalink
add fly.io deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaven committed Nov 2, 2024
1 parent 9cef1fb commit e6b8839
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 17 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

name: Fly Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
22 changes: 7 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@ WORKDIR /usr/src/app

COPY . .
RUN cargo build --bin monolith --release
ADD https://github.com/benbjohnson/litestream/releases/download/v0.3.8/litestream-v0.3.8-linux-amd64-static.tar.gz /tmp/litestream.tar.gz
RUN tar -C /usr/local/bin -xzf /tmp/litestream.tar.gz

# Build the chiselled filesystem based on the desired slices.
FROM ubuntu:$UBUNTU_RELEASE AS chiselled
ARG UBUNTU_RELEASE
ARG TARGETARCH

# Get chisel binary
ADD https://github.com/canonical/chisel/releases/download/v0.9.1/chisel_v0.9.1_linux_$TARGETARCH.tar.gz chisel.tar.gz
RUN tar -xvf chisel.tar.gz -C /usr/bin/
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y ca-certificates

WORKDIR /rootfs

#RUN chisel cut --release ubuntu-$UBUNTU_RELEASE --root /rootfs \
# RUN chisel cut --release ubuntu-$UBUNTU_RELEASE --root /rootfs \
# base-files_base \
# base-files_release-info \
# ca-certificates_data \
Expand All @@ -36,6 +25,9 @@ RUN apt-get update \
COPY ./ai_manager_service/migrations /var/lib/db/migrations
EXPOSE 9000
# COPY --from=chiselled /rootfs /
COPY --from=builder /usr/local/bin/litestream /usr/local/bin/litestream
COPY --from=builder /usr/src/app/target/release/monolith /usr/local/bin/twitch-alerts
COPY ./frontend_api/assets /var/lib/assets/
CMD ["/usr/local/bin/twitch-alerts"]
COPY scripts/start.sh /scripts/start.sh
COPY scripts/litestream.yaml /etc/litestream.yml
CMD ["/scripts/start.sh"]
2 changes: 1 addition & 1 deletion deployment/twitch-alerts-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ spec:
# Start your application & Litestream to share a PVC data directory.
containers:
- name: twitch-alerts
image: nullchannel/twitch-alerts:0.1.36
image: nullchannel/twitch-alerts:0.1.37
ports:
- name: httpport
containerPort: 8080
Expand Down
40 changes: 40 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# fly.toml app configuration file generated for twitch-alerts on 2024-08-15T18:18:25-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'twitch-alerts'
primary_region = 'atl'

[build]

[[services]]
protocol = "tcp"
internal_port = 9000
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0

[[services.ports]]
port = 9000
handlers = ["tls", "http"]

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[env]
FRONTEND_ASSETS = "/var/lib/assets"
HTTP_PORT = "8080"
WEBSOCKET_HOST = "twitch-alerts.fly.dev"
CHANNEL_ID = "99431252"
RUST_ENV = "production"
ENV = "production"
AI_MIGRATIONS_DIR = "/var/lib/db"

[[vm]]
size = 'shared-cpu-1x'
2 changes: 1 addition & 1 deletion frontend_api/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<body>
<main class="flex flex-row justify-center w-full">
<div hx-ext="ws" ws-connect="ws://{{ hostname }}:{{ port }}/">
<div hx-ext="ws" ws-connect="wss://{{ hostname }}:{{ port }}/">
<div id="notifications"></div>
</div>
</main>
Expand Down
20 changes: 20 additions & 0 deletions messages/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub enum EventMessage {
/// A new event message from Twitch.
TwitchEvent(NewTwitchEventMessage),
/// A new message to display.
YoutubeEvent(YoutubeEventMessage),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct YoutubeEventMessage {
pub event: YoutubeEvent,
pub message_id: String,
pub message_at: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub enum YoutubeEvent {
Subscribe(String),
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NewTwitchEventMessage {
pub event: TwitchEvent,
Expand Down
4 changes: 4 additions & 0 deletions scripts/litestream.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dbs:
- path: /data/db
replicas:
- url: s3://twitch-alerts.us-ord-1.linodeobjects.com/db
13 changes: 13 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

# Restore the database if it does not already exist.
if [ -f /data/db ]; then
echo "Database already exists, skipping restore"
else
echo "No database found, restoring from replica if exists"
litestream restore -v -if-replica-exists -o /data/db s3://twitch-alerts.us-ord-1.linodeobjects.com/db
fi

# Run litestream with your app as the subprocess.
exec litestream replicate -exec "/usr/local/bin/twitch-alerts --db-path /data/db"

0 comments on commit e6b8839

Please sign in to comment.