This repository was archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
125 lines (114 loc) · 3.91 KB
/
main.rs
File metadata and controls
125 lines (114 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#![doc=include_str!("../README.md")]
#![forbid(unsafe_code)]
mod graphql;
mod migrations;
mod resolvers;
mod tables;
use async_graphql::{extensions::Tracing, SDLExportOptions};
use axum::{routing::get, Router, Server};
use clap::Parser;
use graphql::{root_schema_builder, RootSchema};
use graphql_endpoints::{GraphQLHandler, GraphQLSubscription, GraphiQLHandler};
use migrations::Migrator;
use opa_client::OPAClient;
use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr, TransactionError};
use sea_orm_migration::MigratorTrait;
use std::{
fs::File,
io::Write,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
path::PathBuf,
};
use url::Url;
fn setup_router(schema: RootSchema) -> Router {
const GRAPHQL_ENDPOINT: &str = "/";
const SUBSCRIPTION_ENDPOINT: &str = "/ws";
Router::new()
.route(
GRAPHQL_ENDPOINT,
get(GraphiQLHandler::new(
GRAPHQL_ENDPOINT,
SUBSCRIPTION_ENDPOINT,
))
.post(GraphQLHandler::new(schema.clone())),
)
.route_service(SUBSCRIPTION_ENDPOINT, GraphQLSubscription::new(schema))
}
async fn setup_database(
mut database_url: Url,
) -> Result<DatabaseConnection, TransactionError<DbErr>> {
if database_url.path().is_empty() {
database_url.set_path("pin_packing");
}
let connection_options = ConnectOptions::new(database_url.to_string());
let connection = Database::connect(connection_options).await?;
Migrator::up(&connection, None).await?;
Ok(connection)
}
async fn serve(router: Router, port: u16) {
let socket_addr: SocketAddr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port));
println!("GraphiQL IDE: {}", socket_addr);
Server::bind(&socket_addr)
.serve(router.into_make_service())
.await
.unwrap();
}
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
#[allow(clippy::large_enum_variant)]
enum Cli {
/// Starts a webserver serving the GraphQL API
Serve(ServeArgs),
/// Prints the GraphQL API to stdout
Schema(SchemaArgs),
}
#[derive(Debug, Parser)]
struct ServeArgs {
/// The port number to serve on.
#[arg(short, long, default_value_t = 80, env = "PIN_PACKING_PORT")]
port: u16,
/// The URL of a postgres database which will be used to persist service data.
#[arg(long, env)]
database_url: Url,
/// The URL of an Open Policy Agent instance serving the required policy endpoints.
#[arg(long, env)]
opa_url: Url,
}
#[derive(Debug, Parser)]
struct SchemaArgs {
/// The file path to write the schema to. If not supplied the schema will be printed to stdout.
#[arg(short, long)]
path: Option<PathBuf>,
}
#[tokio::main]
async fn main() {
dotenvy::dotenv().ok();
let args = Cli::parse();
let tracing_subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.finish();
tracing::subscriber::set_global_default(tracing_subscriber).unwrap();
match args {
Cli::Serve(args) => {
let opa_client = OPAClient::new(args.opa_url);
let database = setup_database(args.database_url).await.unwrap();
let schema = root_schema_builder()
.extension(Tracing)
.data(opa_client)
.data(database)
.finish();
let router = setup_router(schema);
serve(router, args.port).await;
}
Cli::Schema(args) => {
let schema = root_schema_builder().finish();
let schema_string = schema.sdl_with_options(SDLExportOptions::default().federation());
if let Some(path) = args.path {
let mut file = File::create(path).unwrap();
file.write_all(schema_string.as_bytes()).unwrap();
} else {
println!("{}", schema_string);
}
}
}
}