Skip to content

Commit d23b877

Browse files
committed
fixed issue with websocket callback location configuration
1 parent ecca9c2 commit d23b877

File tree

11 files changed

+231
-151
lines changed

11 files changed

+231
-151
lines changed

Dockerfile

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ WORKDIR /usr/src/app
77
COPY . .
88
RUN cargo build --bin monolith --release
99

10-
11-
1210
# Build the chiselled filesystem based on the desired slices.
1311
FROM ubuntu:$UBUNTU_RELEASE AS chiselled
1412
ARG UBUNTU_RELEASE
@@ -18,23 +16,26 @@ ARG TARGETARCH
1816
ADD https://github.com/canonical/chisel/releases/download/v0.9.1/chisel_v0.9.1_linux_$TARGETARCH.tar.gz chisel.tar.gz
1917
RUN tar -xvf chisel.tar.gz -C /usr/bin/
2018
RUN apt-get update \
21-
&& DEBIAN_FRONTEND=noninteractive apt-get install -y ca-certificates
19+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y ca-certificates
2220

2321
WORKDIR /rootfs
2422

23+
#RUN chisel cut --release ubuntu-$UBUNTU_RELEASE --root /rootfs \
24+
# base-files_base \
25+
# base-files_release-info \
26+
# ca-certificates_data \
27+
# libgcc-s1_libs \
28+
# libc6_libs \
29+
# libssl3_libs \
30+
# openssl_bins
2531

26-
RUN chisel cut --release ubuntu-$UBUNTU_RELEASE --root /rootfs \
27-
base-files_base \
28-
base-files_release-info \
29-
ca-certificates_data \
30-
libgcc-s1_libs \
31-
libc6_libs \
32-
libssl3_libs \
33-
openssl_bins
32+
FROM ubuntu:22.04
33+
RUN apt-get update \
34+
&& DEBIAN_FRONTEND=noninteractive apt-get install -y ca-certificates
3435

35-
FROM scratch
3636
COPY ./ai_manager_service/migrations /var/lib/db/migrations
3737
EXPOSE 9000
38-
COPY --from=chiselled /rootfs /
38+
# COPY --from=chiselled /rootfs /
3939
COPY --from=builder /usr/src/app/target/release/monolith /usr/local/bin/twitch-alerts
40+
COPY ./frontend_api/assets /var/lib/assets/
4041
CMD ["/usr/local/bin/twitch-alerts"]

deployment/twitch-alerts-deployment.yaml

Lines changed: 103 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,102 +18,116 @@ spec:
1818
labels:
1919
app: twitch-alerts
2020
spec:
21-
2221
# The config map is used to pass in our Litestream configuration file.
2322
volumes:
24-
- name: configmap
25-
configMap:
26-
name: litestream
27-
- name: data
28-
emptyDir:
29-
sizeLimit: 5G
30-
23+
- name: configmap
24+
configMap:
25+
name: litestream
26+
- name: data
27+
emptyDir:
28+
sizeLimit: 5G
3129

3230
# Initialize using Litestream to restore data automatically
3331
# before starting the application.
3432
initContainers:
35-
- name: init-litestream
36-
image: litestream/litestream:0.3.6
37-
args: ['restore','-config','/etc/config/litestream.yaml', '-if-db-not-exists', '-if-replica-exists', '-v', '/var/lib/twitch-alerts/db']
38-
volumeMounts:
39-
- name: data
40-
mountPath: /var/lib/twitch-alerts
41-
- name: configmap
42-
mountPath: /etc/config
43-
env:
44-
- name: LITESTREAM_ACCESS_KEY_ID
45-
valueFrom:
46-
secretKeyRef:
47-
name: litestream
48-
key: LITESTREAM_ACCESS_KEY_ID
49-
- name: LITESTREAM_SECRET_ACCESS_KEY
50-
valueFrom:
51-
secretKeyRef:
52-
name: litestream
53-
key: LITESTREAM_SECRET_ACCESS_KEY
33+
- name: init-litestream
34+
image: litestream/litestream:0.3.6
35+
args:
36+
[
37+
"restore",
38+
"-config",
39+
"/etc/config/litestream.yaml",
40+
"-if-db-not-exists",
41+
"-if-replica-exists",
42+
"-v",
43+
"/var/lib/twitch-alerts/db",
44+
]
45+
volumeMounts:
46+
- name: data
47+
mountPath: /var/lib/twitch-alerts
48+
- name: configmap
49+
mountPath: /etc/config
50+
env:
51+
- name: LITESTREAM_ACCESS_KEY_ID
52+
valueFrom:
53+
secretKeyRef:
54+
name: litestream
55+
key: LITESTREAM_ACCESS_KEY_ID
56+
- name: LITESTREAM_SECRET_ACCESS_KEY
57+
valueFrom:
58+
secretKeyRef:
59+
name: litestream
60+
key: LITESTREAM_SECRET_ACCESS_KEY
5461

5562
# Start your application & Litestream to share a PVC data directory.
5663
containers:
57-
- name: twitch-alerts
58-
image: nullchannel/twitch-alerts:0.1.24
59-
ports:
60-
- name: websocket
61-
containerPort: 9000
62-
volumeMounts:
63-
- name: data
64-
mountPath: /var/lib/twitch-alerts
65-
env:
66-
- name: GPT_KEY
67-
valueFrom:
68-
secretKeyRef:
69-
name: twitch-alerts-secret
70-
key: GPT_KEY
71-
- name: ACCESS_TOKEN
72-
valueFrom:
73-
secretKeyRef:
74-
name: twitch-alerts-secret
75-
key: ACCESS_TOKEN
76-
- name: CHANNEL_ID
77-
valueFrom:
78-
secretKeyRef:
79-
name: twitch-alerts-secret
80-
key: CHANNEL_ID
81-
- name: DB_PATH
82-
valueFrom:
83-
secretKeyRef:
84-
name: twitch-alerts-secret
85-
key: DB_PATH
86-
- name: ENV
87-
valueFrom:
88-
secretKeyRef:
89-
name: twitch-alerts-secret
90-
key: ENV
91-
- name: AI_MIGRATIONS_DIR
92-
valueFrom:
93-
secretKeyRef:
94-
name: twitch-alerts-secret
95-
key: AI_MIGRATIONS_DIR
96-
97-
- name: litestream
98-
image: litestream/litestream:0.3.6
99-
args: ['replicate','-config','/etc/config/litestream.yaml',]
100-
volumeMounts:
101-
- name: data
102-
mountPath: /var/lib/twitch-alerts
103-
- name: configmap
104-
mountPath: /etc/config
105-
env:
106-
- name: LITESTREAM_ACCESS_KEY_ID
107-
valueFrom:
108-
secretKeyRef:
109-
name: litestream
110-
key: LITESTREAM_ACCESS_KEY_ID
111-
- name: LITESTREAM_SECRET_ACCESS_KEY
112-
valueFrom:
113-
secretKeyRef:
114-
name: litestream
115-
key: LITESTREAM_SECRET_ACCESS_KEY
116-
ports:
117-
- name: metrics
118-
containerPort: 9090
64+
- name: twitch-alerts
65+
image: nullchannel/twitch-alerts:0.1.35
66+
ports:
67+
- name: httpport
68+
containerPort: 8080
69+
- name: websocket
70+
containerPort: 9000
71+
volumeMounts:
72+
- name: data
73+
mountPath: /var/lib/twitch-alerts
74+
env:
75+
- name: FRONTEND_ASSETS
76+
value: "/var/lib/assets"
77+
- name: HTTP_PORT
78+
value: "8080"
79+
- name: WEBSOCKET_HOST
80+
value: "10.1.1.53"
81+
- name: GPT_KEY
82+
valueFrom:
83+
secretKeyRef:
84+
name: twitch-alerts-secret
85+
key: GPT_KEY
86+
- name: ACCESS_TOKEN
87+
valueFrom:
88+
secretKeyRef:
89+
name: twitch-alerts-secret
90+
key: ACCESS_TOKEN
91+
- name: CHANNEL_ID
92+
valueFrom:
93+
secretKeyRef:
94+
name: twitch-alerts-secret
95+
key: CHANNEL_ID
96+
- name: DB_PATH
97+
valueFrom:
98+
secretKeyRef:
99+
name: twitch-alerts-secret
100+
key: DB_PATH
101+
- name: ENV
102+
valueFrom:
103+
secretKeyRef:
104+
name: twitch-alerts-secret
105+
key: ENV
106+
- name: AI_MIGRATIONS_DIR
107+
valueFrom:
108+
secretKeyRef:
109+
name: twitch-alerts-secret
110+
key: AI_MIGRATIONS_DIR
119111

112+
- name: litestream
113+
image: litestream/litestream:0.3.6
114+
args: ["replicate", "-config", "/etc/config/litestream.yaml"]
115+
volumeMounts:
116+
- name: data
117+
mountPath: /var/lib/twitch-alerts
118+
- name: configmap
119+
mountPath: /etc/config
120+
env:
121+
- name: LITESTREAM_ACCESS_KEY_ID
122+
valueFrom:
123+
secretKeyRef:
124+
name: litestream
125+
key: LITESTREAM_ACCESS_KEY_ID
126+
- name: LITESTREAM_SECRET_ACCESS_KEY
127+
valueFrom:
128+
secretKeyRef:
129+
name: litestream
130+
key: LITESTREAM_SECRET_ACCESS_KEY
131+
ports:
132+
- name: metrics
133+
containerPort: 9090

deployment/twitch-alerts-lb.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ spec:
1313
name: websocket
1414
- protocol: TCP
1515
port: 80
16-
targetPort: 80
16+
targetPort: 8080
1717
name: frontend

frontend_api/src/lib.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use axum::{routing::get, Router};
22
use futures_channel::mpsc::unbounded;
3+
use futures_util::sink::With;
34
use futures_util::{SinkExt, StreamExt};
45
use maud::html;
56
use messages::DisplayMessage;
@@ -8,6 +9,7 @@ use std::{
89
collections::HashMap,
910
sync::{Arc, Mutex},
1011
};
12+
1113
use tokio::{
1214
net::{TcpListener, TcpStream},
1315
sync::mpsc,
@@ -27,17 +29,42 @@ use routes::{admin, index};
2729
use crate::types::{ConnectionMap, EventQueues, Queues};
2830

2931
pub struct FrontendApi {
30-
ws_address: String,
31-
http_address: String,
32-
connection_state: ConnectionMap,
33-
asset_path: String,
32+
pub host_info: HostInfo,
33+
pub connection_state: ConnectionMap,
34+
pub asset_path: String,
35+
}
36+
37+
#[derive(Clone)]
38+
pub struct HostInfo {
39+
pub websocket_host: String,
40+
pub ws_port: u16,
41+
pub http_port: u16,
42+
}
43+
44+
impl HostInfo {
45+
pub fn get_http_address(&self) -> String {
46+
format!("{}:{}", "0.0.0.0", self.http_port)
47+
}
48+
49+
pub fn get_ws_address(&self) -> String {
50+
format!("{}:{}", "0.0.0.0", self.ws_port)
51+
}
52+
53+
pub fn get_frontend_ws_address(&self) -> String {
54+
format!("{}:{}", self.websocket_host, self.ws_port)
55+
}
56+
}
57+
58+
#[derive(Clone)]
59+
pub struct UnitedStates {
60+
pub host_info: HostInfo,
61+
pub event_queues: EventQueues,
3462
}
3563

3664
impl FrontendApi {
37-
pub fn new(ws_address: String, http_address: String, asset_path: String) -> FrontendApi {
65+
pub fn new(host_info: HostInfo, asset_path: String) -> FrontendApi {
3866
FrontendApi {
39-
ws_address,
40-
http_address,
67+
host_info,
4168
connection_state: ConnectionMap::new(Mutex::new(HashMap::new())),
4269
asset_path,
4370
}
@@ -47,10 +74,10 @@ impl FrontendApi {
4774
&self,
4875
mut receiver: mpsc::UnboundedReceiver<DisplayMessage>,
4976
) -> Result<(), eyre::Error> {
50-
let listener = TcpListener::bind(&self.ws_address)
77+
let listener = TcpListener::bind(&self.host_info.get_ws_address())
5178
.await
5279
.expect("Can't listen");
53-
println!("Listening on: {}", self.ws_address);
80+
println!("Listening on: {}", self.host_info.get_ws_address());
5481

5582
let connection_state = self.connection_state.clone();
5683
let message_queue_arc: EventQueues = Arc::new(Mutex::new(Queues::new()));
@@ -139,9 +166,14 @@ impl FrontendApi {
139166
}
140167
});
141168

142-
let https_address = self.http_address.clone();
169+
let https_address = self.host_info.get_http_address();
170+
171+
let united_states = UnitedStates {
172+
host_info: self.host_info.clone(),
173+
event_queues: message_queue_arc.clone(),
174+
};
175+
143176
print!("Frontend HTTP is Listening on: {}", https_address);
144-
let event_queues = message_queue_arc.clone();
145177
let asset_path = self.asset_path.clone();
146178
tokio::spawn(async move {
147179
let listener = TcpListener::bind(&https_address)
@@ -160,7 +192,7 @@ impl FrontendApi {
160192
//TODO: understand where to put our assets
161193
// Remember that these need served by nginx in production
162194
.nest_service("/assets", ServeDir::new(asset_path.clone()))
163-
.with_state(event_queues.clone());
195+
.with_state(united_states.clone());
164196

165197
// run it
166198
axum::serve(listener, app).await.unwrap();

0 commit comments

Comments
 (0)