Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NCrashed committed Apr 5, 2022
0 parents commit b17c96f
Show file tree
Hide file tree
Showing 25 changed files with 758 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
result
*.dia~
docker-image-hexstody.tar.gz
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]

members = [
"hexstody-hot",
"hexstody-db",
]
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rust:1 as builder
RUN apt-get update && apt-get install -y clang postgresql sudo
COPY . .
RUN ./docker/setup-build-postgresql.sh

FROM debian:bullseye-slim
COPY --from=builder target/release/hexstody-hot /hexstody-hot
VOLUME /data
WORKDIR /data
RUN apt update && apt install -y libssl1.1 ca-certificates && rm -rf /var/lib/apt/lists/*
COPY ./docker/wait-for-it.sh /wait-for-it.sh
42 changes: 42 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let
sources = import ./nix/sources.nix;
nixpkgs-mozilla = import sources.nixpkgs-mozilla;
pkgs = import sources.nixpkgs {
overlays =
[
nixpkgs-mozilla
(self: super:
let chan = self.rustChannelOf { date = "2022-01-25"; channel = "nightly"; };
in {
rustc = chan.rust;
cargo = chan.rust;
}
)
];
};
naersk = pkgs.callPackage sources.naersk {};
merged-openssl = pkgs.symlinkJoin { name = "merged-openssl"; paths = [ pkgs.openssl.out pkgs.openssl.dev ]; };
in
naersk.buildPackage {
name = "hexstody";
root = pkgs.lib.sourceFilesBySuffices ./. [".rs" ".toml" ".lock" ".html" ".css" ".png" ".sh" ".sql"];
buildInputs = with pkgs; [ openssl pkgconfig clang llvm llvmPackages.libclang zlib cacert curl postgresql ];
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang}/lib";
OPENSSL_DIR = "${merged-openssl}";
preBuild = ''
echo "Deploying local PostgreSQL"
initdb ./pgsql-data --auth=trust
echo "unix_socket_directories = '$PWD'" >> ./pgsql-data/postgresql.conf
pg_ctl start -D./pgsql-data -l psqlog
psql --host=$PWD -d postgres -c "create role \"hexstody\" with login password 'hexstody';"
psql --host=$PWD -d postgres -c "create database \"hexstody\" owner \"hexstody\";"
cp -r ${./hexstody-db/migrations} ./hexstody-db/migrations
for f in ./hexstody-db/migrations/*.sql
do
echo "Applying $f"
psql --host=$PWD -U hexstody -d hexstody -f $f
done
export DATABASE_URL=postgres://hexstody:hexstody@localhost/hexstody
echo "Local database accessible by $DATABASE_URL"
'';
}
23 changes: 23 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "3.9"
services:
postgres:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=hexstody
- POSTGRES_USER=hexstody
- POSTGRES_DB=hexstody

service:
image: hexstody:latest
ports:
- "8081:8081"
hostname: kolliderhedge
environment:
- HEXSTODY_POSTGRES=postgres://hexstody:hexstody@postgres:5432/hexstody
- HEXSTODY_PORT=8081
- RUST_LOG=hexstody::api,hexstody=debug,hexstody_domain=debug
# Set it via .env file
- HEXSTODY_API_KEY=$HEXSTODY_API_KEY
command: /wait-for-it.sh postgres:5432 -- /hexstody-hot serve
links:
- postgres
14 changes: 14 additions & 0 deletions docker/setup-build-postgresql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Designed for debian
echo "Deploying local PostgreSQL"
pg_ctlcluster 13 main start
sudo -u postgres psql -d postgres -c "create role \"hexstody\" with login password 'hexstody';"
sudo -u postgres psql -d postgres -c "create database \"hexstody\" owner \"hexstody\";"
for f in ./hexstody-hedge-db/migrations/*.sql
do
echo "Applying $f"
sudo -u postgres psql -d hexstody -f $f
done
sudo -u postgres psql -d hexstody -c "GRANT ALL PRIVILEGES ON TABLE updates TO hexstody;"
export DATABASE_URL=postgres://hexstody:hexstody@localhost/hexstody
echo "Local database accessible by $DATABASE_URL"
cargo build --release
182 changes: 182 additions & 0 deletions docker/wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available

WAITFORIT_cmdname=${0##*/}

echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
cat << USAGE >&2
Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}

wait_for()
{
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
else
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
fi
WAITFORIT_start_ts=$(date +%s)
while :
do
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
WAITFORIT_result=$?
else
(echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
fi
if [[ $WAITFORIT_result -eq 0 ]]; then
WAITFORIT_end_ts=$(date +%s)
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
break
fi
sleep 1
done
return $WAITFORIT_result
}

wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
else
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
fi
WAITFORIT_PID=$!
trap "kill -INT -$WAITFORIT_PID" INT
wait $WAITFORIT_PID
WAITFORIT_RESULT=$?
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
fi
return $WAITFORIT_RESULT
}

# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
shift 1
;;
--child)
WAITFORIT_CHILD=1
shift 1
;;
-q | --quiet)
WAITFORIT_QUIET=1
shift 1
;;
-s | --strict)
WAITFORIT_STRICT=1
shift 1
;;
-h)
WAITFORIT_HOST="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
WAITFORIT_HOST="${1#*=}"
shift 1
;;
-p)
WAITFORIT_PORT="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
WAITFORIT_PORT="${1#*=}"
shift 1
;;
-t)
WAITFORIT_TIMEOUT="$2"
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
WAITFORIT_TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
WAITFORIT_CLI=("$@")
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi

WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}

# Check to see if timeout is from busybox?
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)

WAITFORIT_BUSYTIMEFLAG=""
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
WAITFORIT_ISBUSY=1
# Check if busybox timeout uses -t flag
# (recent Alpine versions don't support -t anymore)
if timeout &>/dev/stdout | grep -q -e '-t '; then
WAITFORIT_BUSYTIMEFLAG="-t"
fi
else
WAITFORIT_ISBUSY=0
fi

if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for
WAITFORIT_RESULT=$?
fi
fi

if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
exit $WAITFORIT_RESULT
fi
exec "${WAITFORIT_CLI[@]}"
else
exit $WAITFORIT_RESULT
fi
Binary file added docs/diagrams/hotwallet_components.dia
Binary file not shown.
Binary file added docs/diagrams/hotwallet_components.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions hexstody-db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "hexstody-db"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "3.0.0-rc.4", features = ["derive", "env"] }
dotenv = "0.15.0"
env_logger = { version = "0.9.0" }
log = "0.4.14"
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "migrate", "macros", "postgres", "json", "chrono" ] }
tokio = { version = "1", features = ["full"] }
3 changes: 3 additions & 0 deletions hexstody-db/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rerun-if-changed=migrations");
}
Empty file.
47 changes: 47 additions & 0 deletions hexstody-db/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use clap::Parser;
use std::error::Error;
use sqlx::postgres::PgPoolOptions;
use log::*;

#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
/// PostgreSQL connection string
#[clap(
long,
short,
default_value = "postgres://hexstody:hexstody@localhost/hexstody",
env = "HEXSTODY_POSTGRES"
)]
dbconnect: String,
#[clap(subcommand)]
subcmd: SubCommand,
}

#[derive(Parser, Debug)]
enum SubCommand {
/// Apply migrations to the given database
Migrate,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();

env_logger::init();

match args.subcmd {
SubCommand::Migrate => {
info!("Connecting to database");
let pool = PgPoolOptions::new()
.max_connections(1)
.connect(&args.dbconnect)
.await?;

info!("Applying migrations");
sqlx::migrate!("./migrations").run(&pool).await?;
info!("Done");
}
}
Ok(())
}
27 changes: 27 additions & 0 deletions hexstody-hot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "hexstody-hot"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = { version = "0.4.19", features = [ "serde" ] }
clap = { version = "3.0.0-rc.4", features = ["derive", "env"] }
dotenv = "0.15.0"
env_logger = { version = "0.9.0" }
futures = "0.3.19"
futures-channel = "0.3"
futures-util = "0.3.19"
log = "0.4.14"
rweb = { version = "0.15.0", features = ["openapi", "chrono"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "migrate", "macros", "postgres", "json", "chrono" ] }
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
uuid = { version = "0.8.2", features = ["v4"]}

[dev-dependencies]
maplit = "1.0.2"
sqlx-database-tester = { version = "0.2.0", features = [ "runtime-tokio" ] }
1 change: 1 addition & 0 deletions hexstody-hot/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod public;
Empty file added hexstody-hot/src/api/public.rs
Empty file.
Empty file added hexstody-hot/src/db.rs
Empty file.
Loading

0 comments on commit b17c96f

Please sign in to comment.