MCPcopy Create free account
hub / github.com/emwalker/digraph / main

Function main

backend/src/bin/api.rs:100–159  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

98
99#[tokio::main]
100async fn main() -> async_graphql::Result<()> {
101 let config = Config::load()?;
102 env_logger::init();
103
104 log::info!("setting up database connection");
105 let pool = db::db_connection(&config).await?;
106
107 log::info!("reading data from {}", config.digraph_data_directory);
108 let root = git::DataRoot::new(PathBuf::from(&config.digraph_data_directory));
109
110 log::info!("running migrations");
111 sqlx::migrate!("./db/migrations").run(&pool).await?;
112
113 log::info!("loading graphql schema");
114 let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
115 .extension(extensions::Logger)
116 .finish();
117
118 log::info!("loading graphql schema");
119 let redis = Arc::new(redis::Redis::new(config.digraph_redis_url.to_owned())?);
120
121 log::info!("setting up app state");
122 let state = digraph::graphql::State::new(
123 pool,
124 root,
125 schema.clone(),
126 config.digraph_server_secret,
127 redis,
128 );
129
130 let socket = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0:8080".to_owned());
131 let listener = tokio::net::TcpListener::bind(socket).await.unwrap();
132
133 let cors = CorsLayer::new()
134 .allow_origin(AllowOrigin::predicate(
135 |origin: &HeaderValue, _request_parts: &request::Parts| {
136 let bytes = origin.as_bytes();
137 bytes == b"http://localhost:3002"
138 || bytes == b"https://digraph.app"
139 || bytes.ends_with(b".digraph.app")
140 },
141 ))
142 .allow_headers(vec![header::CONTENT_TYPE]);
143
144 // For metrics, see https://github.com/oliverjumpertz/axum-graphql/blob/main/src/main.rs
145 log::info!("starting server");
146 let app = Router::new()
147 .route("/health", get(health))
148 .route("/graphql", post(graphql_handler))
149 .layer(Extension(schema))
150 .layer(cors)
151 .with_state(state);
152
153 axum::serve(listener, app)
154 .with_graceful_shutdown(shutdown_signal())
155 .await
156 .unwrap();
157

Callers

nothing calls this directly

Calls 5

initFunction · 0.85
db_connectionFunction · 0.85
shutdown_signalFunction · 0.85
as_bytesMethod · 0.80
ends_withMethod · 0.80

Tested by

no test coverage detected