Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt at deploying on shuttle.rs #2

Closed
wants to merge 16 commits into from
61 changes: 57 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Expand Up @@ -9,7 +9,12 @@ repository = "https://github.com/qwandor/dancelist"
keywords = ["folk", "dance", "balfolk", "website"]
categories = ["web-programming"]

[lib]
# "lib" is required by main.rs, "cdylib" is required by shuttle
crate-type = ["lib", "cdylib"]

[dependencies]
anyhow = "1.0.56"
askama = "0.11.0"
axum = { version = "0.5.0", features = ["headers"] }
chrono = { version = "0.4.19", features = ["serde"] }
Expand All @@ -27,7 +32,11 @@ serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.78"
serde_urlencoded = "0.7.1"
serde_yaml = "0.8.23"
# FIXME: shuttle doesn't seem to let you point at shuttle-service as a git dep?
# Should be unblocked by https://github.com/getsynth/shuttle/pull/118
shuttle-service = "0.2.5"
stable-eyre = "0.2.2"
sync_wrapper = "0.1.1"
tokio = { version = "1.15.0", features = ["macros", "rt-multi-thread"] }
toml = "0.5.8"
tower-http = { version = "0.2.1", features = ["fs"] }
Expand Down
2 changes: 1 addition & 1 deletion dancelist.example.toml
Expand Up @@ -3,7 +3,7 @@ public_dir = "/usr/share/dancelist"

# The file, directory or URL from which to read events data. This will probably be from the
# dancelist-data repository.
events = "/var/lib/dancelist"
events = "https://raw.githubusercontent.com/qwandor/dancelist-data/release/events.yaml"

# The address on which the server should listen.
bind_address = "0.0.0.0:3002"
Expand Down
8 changes: 7 additions & 1 deletion src/config.rs
Expand Up @@ -57,12 +57,18 @@ impl Config {
}
}

impl Default for Config {
fn default() -> Self {
toml::from_str("").unwrap()
}
}

fn default_public_dir() -> PathBuf {
Path::new("public").to_path_buf()
}

fn default_events() -> String {
"events".to_string()
"https://raw.githubusercontent.com/qwandor/dancelist-data/release/events.yaml".to_string()
}

fn default_bind_address() -> SocketAddr {
Expand Down
5 changes: 0 additions & 5 deletions src/errors.rs
Expand Up @@ -40,11 +40,6 @@ impl IntoResponse for InternalError {
}
}

/// Converts an error into an 'internal server error' response.
pub async fn internal_error<E: Debug>(e: E) -> Response {
internal_error_response(e)
}

fn internal_error_response<E: Debug>(e: E) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
207 changes: 207 additions & 0 deletions src/lib.rs
@@ -0,0 +1,207 @@
// Copyright 2022 the dancelist authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod config;
mod controllers;
mod errors;
mod extractors;
mod icalendar;
mod importers;
mod model;

use crate::{
config::Config,
controllers::{bands, callers, cities, index, organisations, reload},
importers::{balfolknl, folkbalbende, webfeet},
model::events::Events,
};
use axum::{
http::header,
routing::{get, post},
Extension, Router,
};
use eyre::Report;
use log::info;
use schemars::schema_for;
use shuttle_service::{IntoService, Service};
use std::{
net::SocketAddr,
sync::{Arc, Mutex},
};
use tokio::runtime::Runtime;

/// Load events from the given file, directory or URL, or from the one in the config file if no path
/// is provided.
pub async fn load_events(path: Option<&str>) -> Result<Events, Report> {
if let Some(path) = path {
Events::load_events(path).await
} else {
let config = Config::from_file()?;
Events::load_events(&config.events).await
}
}

pub async fn validate(path: Option<&str>) -> Result<(), Report> {
let events = load_events(path).await?;
println!("Successfully validated {} events.", events.events.len());

Ok(())
}

pub async fn concatenate(path: Option<&str>) -> Result<(), Report> {
let events = load_events(path).await?;
print!("{}", serde_yaml::to_string(&events)?);
Ok(())
}

pub async fn import_balbende() -> Result<(), Report> {
let events = folkbalbende::import_events().await?;
print_events(&events)
}

pub async fn import_webfeet() -> Result<(), Report> {
let events = webfeet::import_events().await?;
print_events(&events)
}

pub async fn import_balfolknl() -> Result<(), Report> {
let events = balfolknl::import_events().await?;
print_events(&events)
}

pub fn print_events(events: &Events) -> Result<(), Report> {
let yaml = serde_yaml::to_string(events)?;
let yaml = yaml.replacen(
"---",
"# yaml-language-server: $schema=../../events_schema.json",
1,
);
print!("{}", yaml);
Ok(())
}

pub async fn setup_app(config: &Config) -> Result<Router, Report> {
let events = Events::load_events(&config.events).await?;
let events = Arc::new(Mutex::new(events));

let app = Router::new()
.route("/", get(index::index))
.route("/index.ics", get(index::index_ics))
.route("/index.json", get(index::index_json))
.route("/index.toml", get(index::index_toml))
.route("/index.yaml", get(index::index_yaml))
.route("/bands", get(bands::bands))
.route("/callers", get(callers::callers))
.route("/cities", get(cities::cities))
.route("/organisations", get(organisations::organisations))
.route("/reload", post(reload::reload))
.route(
"/stylesheets/main.css",
get(|| async {
(
[(header::CONTENT_TYPE, "text/css")],
include_str!("../public/stylesheets/main.css"),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed this with Andrew - edit-compile-serve iteration times on axum are pretty slow, so his previous get_service(ServeDir::new(config.public_dir.join("stylesheets"))) approach is super valuable if you want to iterate quickly on your CSS.

)
}),
)
.layer(Extension(events));

Ok(app)
}

pub async fn serve() -> Result<(), Report> {
let config = Config::from_file()?;
let app = setup_app(&config).await?;

info!("Listening on {}", config.bind_address);
axum::Server::bind(&config.bind_address)
.serve(app.into_make_service())
.await?;

Ok(())
}

pub async fn serve_shuttle(addr: SocketAddr) -> Result<(), Report> {
println!("in shuttle_service()");
log::warn!("in shuttle_service()");
let config = Config::default();
let app = setup_app(&config).await?;

println!("Listening on {}", config.bind_address);
log::warn!("Listening on {}", config.bind_address);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get any logging out of shuttle. I don't they set anything up to consume logs before loading the .so, so all logging goes nowhere. println statements are visible in the docker logs though.

axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;

Ok(())
}
// We can't use the web-axum feature because there is no released 0.5 version on crates.io yet.
// We can't use the shuttle_service::main macro because that needs a SimpleService, and orphan rules
// do not allow us to impl anything useful for SimpleService outside of the shuttle_service crate.
struct MyService;
impl MyService {
Copy link
Author

@alsuren alsuren Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that we can't use the shuttle_service::main macro (due to interactions with the orphan rule) is super-annoying. That said, it feels philosophically aligned with the "batteries included" approach that shuttle has first-party support for all of the things, so they can handle automatic injection of logging and databases etc, and things "just work".

I was about to suggest documenting the "impl Service" workaround with an example, but maybe that's not the answer. Maybe it would be better to allow users to supply their own version of shuttle-service as a git dependency or something? I don't know.

Relatedly, Andrew suggests that maybe breaking with the .crate packaging format would be beneficial - some companies might not be comfortable with pushing their source code elsewhere? (the kind of company that prefers to run their own jenkins instances might not be the target market for shuttle though). If shuttle could ship a docker-compose based setup for building the .so file, then we might be able to sidestep the "no git dependencies" issue? I heard a rumour that synced volumes via virtiofs are pretty fast these days? I've not tried it myself though. Also, it's hard to beat the "just send it to us and we'll build it on our infini-core beast of a server that also has a cache of all of your crate's deps already" approach [edit: unless you're already committed to compiling locally for local dev?].

fn new() -> Self {
println!("in MyService::new()");
log::warn!("in MyService::new()");
Self
}
}

impl IntoService for MyService {
type Service = Self;

fn into_service(self) -> Self::Service {
println!("in into_service()");
log::warn!("in into_service()");
self
}
}

fn eyre_to_anyhow(e: Report) -> anyhow::Error {
let e: Box<dyn std::error::Error + Send + Sync + 'static> = e.into();
anyhow::anyhow!(dbg!(e))
}

impl Service for MyService {
fn bind(&mut self, addr: SocketAddr) -> Result<(), shuttle_service::error::Error> {
println!("in bind()");
log::warn!("in bind()");
let rt = Runtime::new().unwrap();
rt.block_on(serve_shuttle(addr)).map_err(eyre_to_anyhow)?;
println!("out bind()");
log::warn!("out bind()");
Ok(())
}
}
shuttle_service::declare_service!(MyService, MyService::new);

/// Returns the JSON schema for events.
pub fn event_schema() -> Result<String, Report> {
let schema = schema_for!(Events);
Ok(serde_json::to_string_pretty(&schema)?)
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;

#[test]
fn json_schema_matches() {
assert_eq!(
event_schema().unwrap(),
read_to_string("events_schema.json").unwrap()
);
}
}