mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-08 02:52:12 +00:00
enable-alpha cookie endpoint
This commit is contained in:
1 parent
5e434498ac
commit
bedfe75355
7 files changed
+72
-3
No files matched your search
@@ -4,3 +4,6 @@ DATABASE_URL=mysql://root:root@localhost/storyteller
|
||||
# This setting turns that on and requires a `sql-data.json` cache file instead.
|
||||
# See https://github.com/launchbadge/sqlx/tree/master/sqlx-cli#readme
|
||||
SQLX_OFFLINE=true
|
||||
|
||||
# We can't serve HTTPS locally without self-signed certificate nonsense.
|
||||
COOKIE_SECURE=false
|
||||
@@ -0,0 +1,53 @@
|
||||
use actix_http::http::StatusCode;
|
||||
use actix_web::{HttpResponse, HttpRequest, Responder, get, web, HttpMessage};
|
||||
use log::info;
|
||||
use actix_http::cookie::Cookie;
|
||||
use crate::server_state::ServerState;
|
||||
use std::sync::Arc;
|
||||
use actix_web::web::Query;
|
||||
|
||||
const ALPHA_COOKIE_NAME : &'static str = "enable-alpha";
|
||||
const CONTENT_TYPE : &'static str = "text/html; charset=utf-8";
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct QueryFields {
|
||||
enable: Option<bool>,
|
||||
}
|
||||
|
||||
#[get("/alpha")]
|
||||
pub async fn enable_alpha(http_request: HttpRequest,
|
||||
query: Query<QueryFields>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> impl Responder
|
||||
{
|
||||
info!("GET /alpha");
|
||||
|
||||
let cookie = Cookie::build(ALPHA_COOKIE_NAME, "true")
|
||||
.domain(&server_state.env_config.cookie_domain)
|
||||
.secure(server_state.env_config.cookie_secure) // HTTPS-only
|
||||
.http_only(false) // This is meant to be exposed to Javascript!
|
||||
.permanent()
|
||||
.finish();
|
||||
|
||||
match query.enable {
|
||||
None => {
|
||||
let cookie_exists = http_request.cookie(ALPHA_COOKIE_NAME).is_some();
|
||||
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
.content_type(CONTENT_TYPE)
|
||||
.body(format!("<h1>use ?enable={{true, false}}; current `{}` cookie state = {}</h1>",
|
||||
ALPHA_COOKIE_NAME, cookie_exists))
|
||||
}
|
||||
Some(true) => {
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
.content_type(CONTENT_TYPE)
|
||||
.cookie(cookie)
|
||||
.body(format!("<h1>?enable=true; setting `{}` cookie</h1>", ALPHA_COOKIE_NAME))
|
||||
}
|
||||
Some(false) => {
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
.content_type(CONTENT_TYPE)
|
||||
.del_cookie(&cookie)
|
||||
.body(format!("<h1>?enable=false; unsetting `{}` mode cookie</h1>", ALPHA_COOKIE_NAME))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod enable_alpha;
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod default_route_404;
|
||||
pub mod misc;
|
||||
pub mod root_index;
|
||||
pub mod users;
|
||||
@@ -1,10 +1,10 @@
|
||||
use actix_http::http::StatusCode;
|
||||
use actix_web::{HttpResponse, HttpRequest, Responder, get};
|
||||
use log::info;
|
||||
use log::debug;
|
||||
|
||||
#[get("/")]
|
||||
pub async fn get_root_index() -> impl Responder {
|
||||
info!("GET /");
|
||||
debug!("GET /"); // NB: Google load balancer hits this a lot, and it spams.
|
||||
HttpResponse::build(StatusCode::OK)
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body("<h1>hello friend</h1>")
|
||||
|
||||
+9
-1
@@ -30,6 +30,7 @@ use crate::util::session_checker::SessionChecker;
|
||||
use crate::endpoints::users::session_info::session_info_handler;
|
||||
use crate::endpoints::root_index::get_root_index;
|
||||
use crate::endpoints::default_route_404::default_route_404;
|
||||
use crate::endpoints::misc::enable_alpha::enable_alpha;
|
||||
|
||||
const DEFAULT_BIND_ADDRESS : &'static str = "0.0.0.0:12345";
|
||||
const DEFAULT_RUST_LOG: &'static str = "debug,actix_web=info";
|
||||
@@ -64,16 +65,22 @@ async fn main() -> AnyhowResult<()> {
|
||||
info!("Reading env vars and setting up utils...");
|
||||
|
||||
let bind_address = easyenv::get_env_string_or_default("BIND_ADDRESS", DEFAULT_BIND_ADDRESS);
|
||||
let num_workers = easyenv::get_env_num("NUM_WORKERS", 4)?;
|
||||
let hmac_secret = easyenv::get_env_string_or_default("COOKIE_SECRET", "notsecret");
|
||||
let cookie_domain = easyenv::get_env_string_or_default("COOKIE_DOMAIN", ".vo.codes");
|
||||
let cookie_secure = easyenv::get_env_bool_or_default("COOKIE_SECURE", true);
|
||||
let cookie_http_only = easyenv::get_env_bool_or_default("COOKIE_HTTP_ONLY", true);
|
||||
|
||||
let cookie_manager = CookieManager::new(&cookie_domain, &hmac_secret);
|
||||
let session_checker = SessionChecker::new(&cookie_manager);
|
||||
|
||||
let server_state = ServerState {
|
||||
env_config: EnvConfig {
|
||||
num_workers: 4,
|
||||
num_workers,
|
||||
bind_address,
|
||||
cookie_domain,
|
||||
cookie_secure,
|
||||
cookie_http_only,
|
||||
},
|
||||
hostname: server_hostname,
|
||||
mysql_pool: pool,
|
||||
@@ -160,6 +167,7 @@ pub async fn serve(server_state: ServerState) -> AnyhowResult<()>
|
||||
.route(web::head().to(|| HttpResponse::Ok()))
|
||||
)
|
||||
.service(get_root_index)
|
||||
.service(enable_alpha)
|
||||
.default_service( web::route().to(default_route_404))
|
||||
})
|
||||
.bind(bind_address)?
|
||||
|
||||
@@ -22,4 +22,7 @@ pub struct EnvConfig {
|
||||
// Number of thread workers.
|
||||
pub num_workers: usize,
|
||||
pub bind_address: String,
|
||||
pub cookie_domain: String,
|
||||
pub cookie_secure: bool,
|
||||
pub cookie_http_only: bool,
|
||||
}
|
||||
Reference in new issue
Block a user