Prepare dockerized build for Railway

This commit is contained in:
2023-06-16 20:14:59 -05:00
parent e8cffce4d1
commit ea22c91cac
6 changed files with 140 additions and 9 deletions

57
src/config.rs Normal file
View File

@@ -0,0 +1,57 @@
use serde::Deserialize;
use tracing::Level;
#[derive(Deserialize, Debug)]
pub enum Environment {
Production,
Development,
}
#[derive(Deserialize, Debug)]
pub struct Configuration {
#[serde(default = "default_env")]
pub env: Environment,
#[serde(default = "default_port")]
pub port: u16,
}
fn default_port() -> u16 {
3000
}
fn default_env() -> Environment {
Environment::Development
}
impl Configuration {
pub fn socket_addr(&self) -> [u8; 4] {
match self.env {
Environment::Production => {
let socket = [0, 0, 0, 0];
tracing::info!(
"Starting Production on {:?}:{}",
socket.as_slice(),
self.port
);
socket
}
Environment::Development => {
let socket = [127, 0, 0, 1];
tracing::info!(
"Starting Development on {:?}:{}",
socket.as_slice(),
self.port
);
socket
}
}
}
pub fn log_level(&self) -> Level {
match self.env {
Environment::Production => Level::INFO,
Environment::Development => Level::DEBUG,
}
}
}

View File

@@ -1,3 +1,5 @@
mod config;
use std::net::SocketAddr;
use axum::{http::StatusCode, Json, response::IntoResponse, Router, routing::{get, post}};
@@ -5,21 +7,33 @@ use axum::body::{Bytes, Full};
use axum::extract::ConnectInfo;
use axum::http::header;
use axum::response::Response;
use dotenvy::dotenv;
use config::Configuration;
mod svg;
#[tokio::main]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
// Parse dotenv files and expose them as environment variables
dotenv().ok();
let app = Router::new()
.route("/", get(root));
// envy uses our Configuration struct to parse environment variables
let config = envy::from_env::<Configuration>().expect("Please provide PORT env var");
// initialize tracing
tracing_subscriber::fmt()
// With the log_level from our config
.with_max_level(config.log_level())
.init();
// build our application with a route
let app = Router::new().route("/", get(root_handler));
// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
let addr = SocketAddr::from((config.socket_addr(), config.port));
axum::Server::bind(&addr)
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
.await
@@ -27,7 +41,7 @@ async fn main() {
}
// basic handler that responds with a static string
async fn root(connect_info: ConnectInfo<SocketAddr>) -> impl IntoResponse {
async fn root_handler(connect_info: ConnectInfo<SocketAddr>) -> impl IntoResponse {
let raw_image = svg::get();
if raw_image.is_err() {

View File

@@ -29,8 +29,6 @@ pub fn get() -> Result<Vec<u8>, RenderError> {
fontdb.load_system_fonts();
let svg_data = std::fs::read("test.svg").unwrap();
// print bytes as string
println!("{:?}", String::from_utf8(svg_data.clone()).unwrap());
let mut tree_result = usvg::Tree::from_data(&svg_data, &opt);
if tree_result.is_err() { return Err(RenderError { message: Some("Failed to parse".to_string()) }); }