Skip to content
Snippets Groups Projects
Commit ba4581ba authored by Éloïs's avatar Éloïs
Browse files

tmp: global context exemple

parent 56cbab91
No related branches found
No related tags found
No related merge requests found
......@@ -1009,6 +1009,7 @@ version = "0.1.0"
dependencies = [
"actix-web 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"dubp-currency-params 0.2.0",
"durs-bc-db-reader 0.3.0-dev",
"durs-common-tools 0.2.0",
"durs-conf 0.3.0-dev",
"durs-message 0.3.0-dev",
......
......@@ -10,6 +10,7 @@ edition = "2018"
path = "src/lib.rs"
[dependencies]
durs-bc-db-reader = { path = "../../modules-lib/bc-db-reader" }
durs-conf = { path = "../../core/conf" }
durs-message = { path = "../../core/message" }
durs-module = { path = "../../core/module" }
......
use durs_bc_db_reader::BcDbRo;
use durs_common_tools::fatal_error;
/// GVA context (access to database)
static mut CONTEXT: Option<Context> = None;
#[derive(Debug)]
pub struct Context {
db: BcDbRo,
}
impl juniper::Context for Context {}
impl Context {
pub fn new(db: BcDbRo) -> Self {
Context { db }
}
}
pub unsafe fn init(db: BcDbRo) {
CONTEXT.replace(Context::new(db));
}
pub unsafe fn get_context() -> &'static Context {
if let Some(ref context) = CONTEXT {
context
} else {
fatal_error!("GVA: no context");
}
}
......@@ -21,7 +21,6 @@
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications
......@@ -36,6 +35,7 @@ extern crate structopt;
extern crate juniper;
mod context;
mod schema;
use dubp_currency_params::CurrencyName;
......@@ -62,7 +62,7 @@ use juniper::http::graphiql::graphiql_source;
use juniper::http::GraphQLRequest;
/// Name of your module
pub static MODULE_NAME: &'static str = "gva";
pub static MODULE_NAME: &str = "gva";
fn graphiql() -> HttpResponse {
let html = graphiql_source("http://127.0.0.1:3000/graphql");
......@@ -76,9 +76,9 @@ fn graphql(
st: web::Data<Arc<Schema>>,
data: web::Json<GraphQLRequest>,
) -> impl Future<Item = HttpResponse, Error = Error> {
let context = Context::new();
let context = unsafe { crate::context::get_context() };
web::block(move || {
let res = data.execute(&st, &context);
let res = data.execute(&st, context);
Ok::<_, serde_json::error::Error>(serde_json::to_string(&res)?)
})
.map_err(Error::from)
......@@ -96,6 +96,15 @@ fn start_web_server() -> std::io::Result<()> {
// Create Juniper schema
let schema = std::sync::Arc::new(create_schema());
// Instanciate the context
if let Ok(db) = durs_bc_db_reader::open_db_ro(&std::path::Path::new("db_path")) {
unsafe {
context::init(db);
}
} else {
fatal_error!("GVA: fail to open DB.");
};
// Start http server
HttpServer::new(move || {
App::new()
......@@ -299,7 +308,6 @@ impl DursModule<DuRsConf, DursMsg> for GvaModule {
}
});
thread::spawn(move || {
let _ = start_web_server();
});
......
use crate::context::Context;
use juniper::Executor;
use juniper::FieldResult;
use juniper_from_schema::graphql_schema_from_file;
graphql_schema_from_file!("lib/modules/gva/resources/schema.gql");
pub struct Query;
impl QueryFields for Query {
fn field_hello_world(
&self,
......@@ -27,22 +25,6 @@ impl MutationFields for Mutation {
}
}
pub struct Context{
}
impl juniper::Context for Context {}
impl Context {
pub fn new() -> Self {
Context {}
}
}
pub fn create_schema() -> Schema {
Schema::new(Query {}, Mutation {})
}
......@@ -57,6 +57,7 @@ pub struct KvFileDbHandler {
}
/// Key-value file Database read-only handler
#[derive(Debug)]
pub struct KvFileDbRoHandler(KvFileDbHandler);
impl KvFileDbRoHandler {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment