main.rs

  1mod admin;
  2mod assets;
  3mod auth;
  4mod db;
  5mod env;
  6mod errors;
  7mod expiring;
  8mod github;
  9mod home;
 10mod rpc;
 11mod team;
 12#[cfg(test)]
 13mod tests;
 14
 15use self::errors::TideResultExt as _;
 16use anyhow::{Context, Result};
 17use async_std::{net::TcpListener, sync::RwLock as AsyncRwLock};
 18use async_trait::async_trait;
 19use auth::RequestExt as _;
 20use db::{Db, DbOptions};
 21use handlebars::{Handlebars, TemplateRenderError};
 22use parking_lot::RwLock;
 23use rust_embed::RustEmbed;
 24use serde::{Deserialize, Serialize};
 25use std::sync::Arc;
 26use surf::http::cookies::SameSite;
 27use tide::{log, sessions::SessionMiddleware};
 28use tide_compress::CompressMiddleware;
 29use zrpc::Peer;
 30
 31type Request = tide::Request<Arc<AppState>>;
 32
 33#[derive(RustEmbed)]
 34#[folder = "templates"]
 35struct Templates;
 36
 37#[derive(Default, Deserialize)]
 38pub struct Config {
 39    pub http_port: u16,
 40    pub database_url: String,
 41    pub session_secret: String,
 42    pub github_app_id: usize,
 43    pub github_client_id: String,
 44    pub github_client_secret: String,
 45    pub github_private_key: String,
 46}
 47
 48pub struct AppState {
 49    db: Db,
 50    handlebars: RwLock<Handlebars<'static>>,
 51    auth_client: auth::Client,
 52    github_client: Arc<github::AppClient>,
 53    repo_client: github::RepoClient,
 54    rpc: AsyncRwLock<rpc::State>,
 55    config: Config,
 56}
 57
 58impl AppState {
 59    async fn new(config: Config) -> tide::Result<Arc<Self>> {
 60        let db = Db(DbOptions::new()
 61            .max_connections(5)
 62            .connect(&config.database_url)
 63            .await
 64            .context("failed to connect to postgres database")?);
 65
 66        let github_client =
 67            github::AppClient::new(config.github_app_id, config.github_private_key.clone());
 68        let repo_client = github_client
 69            .repo("zed-industries/zed".into())
 70            .await
 71            .context("failed to initialize github client")?;
 72
 73        let this = Self {
 74            db,
 75            handlebars: Default::default(),
 76            auth_client: auth::build_client(&config.github_client_id, &config.github_client_secret),
 77            github_client,
 78            repo_client,
 79            rpc: Default::default(),
 80            config,
 81        };
 82        this.register_partials();
 83        Ok(Arc::new(this))
 84    }
 85
 86    fn register_partials(&self) {
 87        for path in Templates::iter() {
 88            if let Some(partial_name) = path
 89                .strip_prefix("partials/")
 90                .and_then(|path| path.strip_suffix(".hbs"))
 91            {
 92                let partial = Templates::get(path.as_ref()).unwrap();
 93                self.handlebars
 94                    .write()
 95                    .register_partial(partial_name, std::str::from_utf8(partial.as_ref()).unwrap())
 96                    .unwrap()
 97            }
 98        }
 99    }
100
101    fn render_template(
102        &self,
103        path: &'static str,
104        data: &impl Serialize,
105    ) -> Result<String, TemplateRenderError> {
106        #[cfg(debug_assertions)]
107        self.register_partials();
108
109        self.handlebars.read().render_template(
110            std::str::from_utf8(Templates::get(path).unwrap().as_ref()).unwrap(),
111            data,
112        )
113    }
114}
115
116#[async_trait]
117trait RequestExt {
118    async fn layout_data(&mut self) -> tide::Result<Arc<LayoutData>>;
119    fn db(&self) -> &Db;
120}
121
122#[async_trait]
123impl RequestExt for Request {
124    async fn layout_data(&mut self) -> tide::Result<Arc<LayoutData>> {
125        if self.ext::<Arc<LayoutData>>().is_none() {
126            self.set_ext(Arc::new(LayoutData {
127                current_user: self.current_user().await?,
128            }));
129        }
130        Ok(self.ext::<Arc<LayoutData>>().unwrap().clone())
131    }
132
133    fn db(&self) -> &Db {
134        &self.state().db
135    }
136}
137
138#[derive(Serialize)]
139struct LayoutData {
140    current_user: Option<auth::User>,
141}
142
143#[async_std::main]
144async fn main() -> tide::Result<()> {
145    log::start();
146
147    if let Err(error) = env::load_dotenv() {
148        log::error!(
149            "error loading .env.toml (this is expected in production): {}",
150            error
151        );
152    }
153
154    let config = envy::from_env::<Config>().expect("error loading config");
155    let state = AppState::new(config).await?;
156    let rpc = Peer::new();
157    run_server(
158        state.clone(),
159        rpc,
160        TcpListener::bind(&format!("0.0.0.0:{}", state.config.http_port)).await?,
161    )
162    .await?;
163    Ok(())
164}
165
166pub async fn run_server(
167    state: Arc<AppState>,
168    rpc: Arc<Peer>,
169    listener: TcpListener,
170) -> tide::Result<()> {
171    let mut web = tide::with_state(state.clone());
172    web.with(CompressMiddleware::new());
173    web.with(
174        SessionMiddleware::new(
175            db::SessionStore::new_with_table_name(&state.config.database_url, "sessions")
176                .await
177                .unwrap(),
178            state.config.session_secret.as_bytes(),
179        )
180        .with_same_site_policy(SameSite::Lax), // Required obtain our session in /auth_callback
181    );
182    web.with(errors::Middleware);
183    home::add_routes(&mut web);
184    team::add_routes(&mut web);
185    admin::add_routes(&mut web);
186    auth::add_routes(&mut web);
187    assets::add_routes(&mut web);
188
189    let mut app = tide::with_state(state.clone());
190    rpc::add_routes(&mut app, &rpc);
191    app.at("/").nest(web);
192
193    app.listen(listener).await?;
194
195    Ok(())
196}