main.rs

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