feat: use wss:// if https

This commit is contained in:
Asger Juul Brunshøj 2023-06-13 21:31:06 +02:00
parent d90daa0423
commit 9a064a339e
3 changed files with 43 additions and 2 deletions

View File

@ -15,8 +15,15 @@ pub struct WebsocketService {
impl WebsocketService { impl WebsocketService {
pub fn connect() -> Self { pub fn connect() -> Self {
let hostname = web_sys::window().unwrap().location().hostname().unwrap(); let window = web_sys::window().expect("no global `window` exists");
let ws_addr = format!("ws://{hostname}:4000/ws"); let location = window.location();
let hostname = location.hostname().expect("should have a hostname");
let protocol = match location.protocol().expect("should have a protocol").as_str() {
"https:" => "wss",
_ => "ws",
};
let ws_addr = format!("{protocol}://{hostname}:4000/ws");
let ws = WebSocket::open(&ws_addr).unwrap(); let ws = WebSocket::open(&ws_addr).unwrap();
log::info!("Opened websocket connection to {ws_addr}"); log::info!("Opened websocket connection to {ws_addr}");

View File

@ -30,3 +30,9 @@ subscribe-ws:
trunk-serve: trunk-serve:
trunk serve --address=0.0.0.0 crates/frontend/index.html trunk serve --address=0.0.0.0 crates/frontend/index.html
deploy-frontend:
trunk build --release --dist=dist crates/frontend/index.html
ssh root@ajb.dk rm -r /var/www/achievements
rsync -avz dist/ root@ajb.dk:/var/www/achievements

28
nginx.conf Normal file
View File

@ -0,0 +1,28 @@
# Backend
server {
listen 4000 ssl http2;
listen [::]:4000 ssl http2;
server_name achievements.ajb.dk;
ssl_certificate /etc/letsencrypt/live/ajb.dk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ajb.dk/privkey.pem;
location / {
proxy_pass http://localhost:4001/;
}
}
# Frontend (serve yew app static files)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name achievements.ajb.dk;
ssl_certificate /etc/letsencrypt/live/ajb.dk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ajb.dk/privkey.pem;
location / {
alias /var/www/achievements/;
try_files $uri $uri/ /index.html;
}
}