Commit 3e18aeef authored by Bijun Li's avatar Bijun Li
Browse files

Update for merge

parent dae24b82
Showing with 41 additions and 64 deletions
+41 -64
......@@ -15,8 +15,8 @@ COPY Cargo.toml Cargo.toml
COPY Settings.toml Settings.toml
COPY src src
RUN cargo build
RUN cargo build --release
EXPOSE 3030
CMD ["./target/debug/pod"]
CMD ["./target/release/pod"]
......@@ -3,7 +3,7 @@ FROM ubuntu:latest
WORKDIR /usr/src/pod
COPY Settings.toml Settings.toml
COPY target/debug/pod pod
COPY target/release/pod pod
EXPOSE 3030
......
# ------------------------------------------------------------------------------
# Cargo Build Stage
# ------------------------------------------------------------------------------
FROM rust:latest as cargo-build
RUN apt-get update
RUN apt-get install -y musl-tools cmake golang
RUN ln -s /usr/bin/musl-gcc /usr/bin/musl-g++
RUN rustup target add x86_64-unknown-linux-musl
WORKDIR /usr/src/pod
COPY Cargo.toml Cargo.toml
COPY Cargo.lock Cargo.lock
RUN mkdir src \
&& echo "fn main() {print!(\"Dummy main\");} // dummy file" > src/main.rs
RUN cargo build --release --target=x86_64-unknown-linux-musl
RUN rm -f target/x86_64-unknown-linux-musl/release/deps/pod*
COPY . .
RUN RUSTFLAGS="-C linker=musl-gcc -C link-arg=-L/usr/local/musl/lib" cargo build --release --target=x86_64-unknown-linux-musl
# ------------------------------------------------------------------------------
# Final Stage
# ------------------------------------------------------------------------------
FROM alpine:latest
COPY --from=cargo-build /usr/src/myapp/target/x86_64-unknown-linux-musl/release/pod /usr/local/bin/pod
CMD ["pod"]
drop = false
set = false
# Config option for dropping schema
schema_drop = false
# Config option for adding schema to dgraph
schema_set = false
# dgraph host
dgraph_host = "localhost"
version: '3.0'
version: '3'
services:
dgraph:
image: dgraph/standalone:latest
ports:
- "8000:8000"
- "5080:5080"
- "6080:6080"
- "8080:8080"
- "9080:9080"
network_mode: "host"
networks:
- my-net
pod:
build:
context: .
args:
SCHEMA_DROP: 'false'
SCHEMA_SET: 'true'
dockerfile: Dockerfile
restart: always
environment:
- SCHEMA_DROP=false
- SCHEMA_SET=true
- DGRAPH_HOST=pod_dgraph_1
image: pod:latest
ports:
- "3030:3030"
depends_on:
- dgraph
network_mode: "host"
networks:
- my-net
networks:
my-net:
use crate::data_model;
use config::Config;
use dgraph::*;
/// Create dgraph gRPC connection.
pub fn create_dgraph() -> Dgraph {
let dgraph_client = dgraph::new_dgraph_client("localhost:9080");
pub fn create_dgraph(settings: &Config) -> Dgraph {
let dgraph_host = settings.get_str("dgraph_host").unwrap();
let dgraph_client = dgraph::new_dgraph_client((dgraph_host + ":9080").as_str());
Dgraph::new(vec![dgraph_client])
}
......
......@@ -8,15 +8,13 @@ use std::collections::HashMap;
use std::str;
use std::sync::Arc;
/// Check if the `uid` exists in DB, or if DB contains any items
fn item_not_found(result: &Value) -> bool {
/// Verify if the response contains the content of the requested node.
/// Return `true` if response doesn't contain any content.
/// Return `false` if the node is found and its content is included in the response.
fn response_is_empty(result: &Value) -> bool {
let result = result.get("items").unwrap().as_array().unwrap();
let empty = result.len() == 0 || result.first().unwrap().get("type") == Option::None;
if empty {
true
} else {
false
}
empty
}
/// Get project version as seen by Cargo.
......@@ -48,7 +46,7 @@ pub fn get_item(_dgraph: &Arc<Dgraph>, uid: u64) -> Option<String> {
.expect("query");
let result: Value = serde_json::from_slice(&resp.json).unwrap();
if item_not_found(&result) {
if response_is_empty(&result) {
None
} else {
Some(sync_state::set_syncstate(result))
......@@ -69,7 +67,7 @@ pub fn get_all_items(_dgraph: &Arc<Dgraph>) -> Option<String> {
let resp = _dgraph.new_readonly_txn().query(query).expect("query");
let result: Value = serde_json::from_slice(&resp.json).unwrap();
if item_not_found(&result) {
if response_is_empty(&result) {
None
} else {
Some(sync_state::set_syncstate_all(result))
......
......@@ -22,22 +22,22 @@ async fn main() {
})
.init();
let dgraph = dgraph_database::create_dgraph();
let mut settings = config::Config::default();
settings
.merge(config::File::with_name("Settings"))
.unwrap()
.merge(config::Environment::with_prefix("SCHEMA"))
.merge(config::Environment::new())
.unwrap();
let dgraph = dgraph_database::create_dgraph(&settings);
// Drop schema only in DROP mode.
// Add "SCHEMA_DROP=true" before executable, default is FALSE.
if settings.get_str("drop").unwrap().eq("true") {
if settings.get_bool("schema_drop").unwrap() {
dgraph_database::drop_schema_and_all_data_irreversibly(&dgraph);
}
// Set schema only in SET mode.
// Add "SCHEMA_SET=true" before executable, default is FALSE.
if settings.get_str("set").unwrap().eq("true") {
if settings.get_bool("schema_set").unwrap() {
dgraph_database::set_schema(&dgraph);
}
// Start web framework warp.
......
......@@ -122,6 +122,6 @@ pub async fn run_server(server_name: String, dgraph: Dgraph) {
.or(update_item)
.or(delete_item),
)
.run(([127, 0, 0, 1], 3030))
.run(([0, 0, 0, 0], 3030))
.await;
}
docker run --rm -it -p 8000:8000 -p 5080:5080 -p 6080:6080 -p 8080:8080 -p 9080:9080 --network host dgraph/standalone:latest
docker run --rm -it -p 8000:8000 -p 8080:8080 -p 9080:9080 --network my-net --name pod_dgraph_1 dgraph/standalone:latest
docker run --rm -it --network host -p 3030:3030 -e SCHEMA_SET=true pod:latest
docker run --rm -it -p 3030:3030 -e SCHEMA_SET=true -e DGRAPH_HOST=pod_dgraph_1 --network my-net --name pod_pod_1 pod:latest
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment