This commit is contained in:
Untone 2023-10-11 18:01:18 +03:00
parent 86742bf982
commit 6c659e1c8e
2 changed files with 10 additions and 64 deletions

View File

@ -1,60 +0,0 @@
{{ $proxy_settings := "proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; proxy_set_header Host $http_host; proxy_set_header X-Request-Start $msec;" }}
{{ $gzip_settings := "gzip on; gzip_min_length 1100; gzip_buffers 4 32k; gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml; gzip_vary on; gzip_comp_level 6;" }}
{{ $cors_headers_options := "if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '$allow_origin' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; return 204; }" }}
{{ $cors_headers_post := "if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '$allow_origin' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; add_header 'Access-Control-Allow-Credentials' 'true' always; }" }}
{{ $cors_headers_get := "if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '$allow_origin' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; add_header 'Access-Control-Allow-Credentials' 'true' always; }" }}
map $http_origin $allow_origin {
~^https?:\/\/((.*\.)?localhost(:\d+)?|discoursio-webapp(-(.*))?\.vercel\.app|(.*\.)?discours\.io)$ $http_origin;
default "";
}
{{ range $port_map := .PROXY_PORT_MAP | split " " }}
{{ $port_map_list := $port_map | split ":" }}
{{ $scheme := index $port_map_list 0 }}
{{ $listen_port := index $port_map_list 1 }}
{{ $upstream_port := index $port_map_list 2 }}
{{ $listen_settings := "" }}
{{ if eq $scheme "http" }}
{{ $listen_settings := print "listen " $listen_port ";" }}
{{ else if eq $scheme "https" }}
{{ $listen_settings := print "listen " $listen_port " ssl; ssl_certificate " $.APP_SSL_PATH "/server.crt; ssl_certificate_key " $.APP_SSL_PATH "/server.key;" }}
{{ end }}
server {
{{ $listen_settings }}
location /connect {
proxy_pass http://{{ $.APP }}-{{ $upstream_port }};
{{ $proxy_settings }}
{{ $gzip_settings }}
{{ $cors_headers_options }}
{{ $cors_headers_post }}
{{ $cors_headers_get }}
}
location /disconnect {
proxy_pass http://{{ $.APP }}-{{ $upstream_port }};
{{ $proxy_settings }}
{{ $gzip_settings }}
{{ $cors_headers_options }}
{{ $cors_headers_post }}
{{ $cors_headers_get }}
}
}
{{ end }}
{{ if $.DOKKU_APP_WEB_LISTENERS }}
{{ range $upstream_port := $.PROXY_UPSTREAM_PORTS | split " " }}
upstream {{ $.APP }}-{{ $upstream_port }} {
{{ range $listeners := $.DOKKU_APP_WEB_LISTENERS | split " " }}
{{ $listener_list := $listeners | split ":" }}
{{ $listener_ip := index $listener_list 0 }}
server {{ $listener_ip }}:{{ $upstream_port }};
{{ end }}
}
{{ end }}
{{ end }}

View File

@ -10,6 +10,13 @@ use tokio::task::JoinHandle;
mod data; mod data;
macro_rules! log {
($($arg:tt)*) => {{
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S%.3f");
println!("[{}] {}", timestamp, format_args!($($arg)*));
}};
}
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
tasks: Arc<Mutex<HashMap<String, JoinHandle<()>>>>, tasks: Arc<Mutex<HashMap<String, JoinHandle<()>>>>,
@ -115,13 +122,12 @@ async fn main() -> std::io::Result<()> {
tasks: tasks.clone(), tasks: tasks.clone(),
redis: client.clone(), redis: client.clone(),
}; };
println!("Redis client initialized"); log!("Redis client initialized");
HttpServer::new(move || { HttpServer::new(move || {
println!("Webserver initialized"); log!("Webserver initialized");
App::new() App::new()
.app_data(web::Data::new(state.clone())) .app_data(web::Data::new(state.clone()))
.route("/connect", web::get().to(connect_handler)) .route("/", web::get().to(connect_handler))
.route("/disconnect", web::get().to(disconnect_handler))
}) })
.bind("127.0.0.1:8080")? .bind("127.0.0.1:8080")?
.run() .run()