diff options
author | fx <[email protected]> | 2023-10-14 22:01:33 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-14 22:01:33 +0200 |
commit | 6648d1da1df9a4e4e2945739fc0fd3c7f77643cb (patch) | |
tree | e487bbf77c5280a58ee5bcd69f6b74018be96430 | |
parent | 25885b8e08252db20f2a50e64184b6a59e546a73 (diff) | |
download | webol-6648d1da1df9a4e4e2945739fc0fd3c7f77643cb.tar webol-6648d1da1df9a4e4e2945739fc0fd3c7f77643cb.tar.gz webol-6648d1da1df9a4e4e2945739fc0fd3c7f77643cb.zip |
test this fing shit on pi wohoo
-rw-r--r-- | Dockerfile.arm.build | 16 | ||||
-rw-r--r-- | Dockerfile.build | 24 | ||||
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | docker-compose.yml | 1 | ||||
-rw-r--r-- | nobuild.rs.old (renamed from build.rs) | 0 | ||||
-rw-r--r-- | src/main.rs | 15 | ||||
-rw-r--r-- | src/routes/device.rs | 4 | ||||
-rw-r--r-- | src/routes/start.rs | 2 |
8 files changed, 54 insertions, 18 deletions
diff --git a/Dockerfile.arm.build b/Dockerfile.arm.build new file mode 100644 index 0000000..1845afe --- /dev/null +++ b/Dockerfile.arm.build | |||
@@ -0,0 +1,16 @@ | |||
1 | FROM rust:1.73 as builder | ||
2 | WORKDIR /usr/src/webol | ||
3 | COPY . . | ||
4 | #RUN rustup target add armv7-unknown-linux-gnueabihf | ||
5 | #RUN apt update && apt install gcc-arm-linux-gnueabihf -y | ||
6 | #RUN CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc cargo install --path . --target armv7-unknown-linux-gnueabihf | ||
7 | RUN cargo install --path . | ||
8 | |||
9 | #FROM --platform=arm64 debian:bookworm-slim | ||
10 | FROM debian:bookworm-slim | ||
11 | RUN apt update && apt install -y libc6 && rm -rf /var/lib/apt/lists/* | ||
12 | WORKDIR /usr/local/webol | ||
13 | COPY --from=builder /usr/local/cargo/bin/webol /usr/local/bin/webol | ||
14 | |||
15 | EXPOSE 7229 | ||
16 | CMD ["webol"] | ||
diff --git a/Dockerfile.build b/Dockerfile.build index 3bbaf39..0158b8a 100644 --- a/Dockerfile.build +++ b/Dockerfile.build | |||
@@ -1,16 +1,18 @@ | |||
1 | FROM debian:bookworm AS deb_extractor | ||
2 | RUN cd /tmp && \ | ||
3 | apt-get update && apt-get download \ | ||
4 | libc6 && \ | ||
5 | mkdir /dpkg && \ | ||
6 | for deb in *.deb; do dpkg --extract $deb /dpkg || exit 10; done | ||
7 | |||
1 | FROM rust:1.73 as builder | 8 | FROM rust:1.73 as builder |
2 | WORKDIR /usr/src/webol | 9 | WORKDIR /app |
3 | COPY . . | 10 | COPY . . |
4 | # RUN rustup target add armv7-unknown-linux-gnueabihf | 11 | RUN cargo build --release |
5 | # RUN apt update && apt install gcc-arm-linux-gnueabihf -y | ||
6 | # RUN CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc cargo install --path . --target armv7-unknown-linux-gnueabihf | ||
7 | RUN cargo install --path . | ||
8 | 12 | ||
9 | # FROM --platform=arm64 debian:bullseye-slim | 13 | FROM gcr.io/distroless/cc |
10 | FROM debian:trixie-slim | 14 | COPY --from=builder /app/target/release/webol / |
11 | # RUN apt-get update && apt-get install -y libc6 && rm -rf /var/lib/apt/lists/* | 15 | COPY --from=deb_extractor /dpkg / |
12 | WORKDIR /usr/local/webol | ||
13 | COPY --from=builder /usr/local/cargo/bin/webol /usr/local/bin/webol | ||
14 | 16 | ||
15 | EXPOSE 7229 | 17 | EXPOSE 7229 |
16 | CMD ["webol"] | 18 | ENTRYPOINT ["./webol"] |
@@ -1 +1,9 @@ | |||
1 | # webol \ No newline at end of file | 1 | # webol |
2 | |||
3 | DATABASE_URL: `String` | ||
4 | |||
5 | WEBOL_APIKEY: `String` | ||
6 | |||
7 | WEBOL_SERVERADDR: `Option<String>` | ||
8 | |||
9 | WEBOL_BINDADDR: `Option<String>` | ||
diff --git a/docker-compose.yml b/docker-compose.yml index 1980356..9d73e9b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml | |||
@@ -3,6 +3,7 @@ version: '3.1' | |||
3 | services: | 3 | services: |
4 | db: | 4 | db: |
5 | image: postgres | 5 | image: postgres |
6 | container_name: webol_dev_postgres | ||
6 | restart: no | 7 | restart: no |
7 | environment: | 8 | environment: |
8 | POSTGRES_PASSWORD: postgres | 9 | POSTGRES_PASSWORD: postgres |
diff --git a/build.rs b/nobuild.rs.old index 7609593..7609593 100644 --- a/build.rs +++ b/nobuild.rs.old | |||
diff --git a/src/main.rs b/src/main.rs index e6c746c..b7306ea 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -7,6 +7,7 @@ use sqlx::postgres::PgPoolOptions; | |||
7 | use time::util::local_offset; | 7 | use time::util::local_offset; |
8 | use tracing::{debug, info, level_filters::LevelFilter}; | 8 | use tracing::{debug, info, level_filters::LevelFilter}; |
9 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 9 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
10 | use crate::config::SETTINGS; | ||
10 | use crate::routes::device::{get_device, post_device, put_device}; | 11 | use crate::routes::device::{get_device, post_device, put_device}; |
11 | use crate::routes::start::start; | 12 | use crate::routes::start::start; |
12 | 13 | ||
@@ -37,13 +38,12 @@ async fn main() { | |||
37 | 38 | ||
38 | let version = env!("CARGO_PKG_VERSION"); | 39 | let version = env!("CARGO_PKG_VERSION"); |
39 | 40 | ||
40 | info!("starting webol v{}", version); | 41 | info!("start webol v{}", version); |
41 | 42 | ||
42 | let db = init_db_pool().await; | 43 | let db = init_db_pool().await; |
43 | 44 | ||
44 | let shared_state = Arc::new(AppState { db }); | 45 | let shared_state = Arc::new(AppState { db }); |
45 | 46 | ||
46 | // build our application with a single route | ||
47 | let app = Router::new() | 47 | let app = Router::new() |
48 | .route("/start", post(start)) | 48 | .route("/start", post(start)) |
49 | .route("/device", get(get_device)) | 49 | .route("/device", get(get_device)) |
@@ -51,8 +51,9 @@ async fn main() { | |||
51 | .route("/device", post(post_device)) | 51 | .route("/device", post(post_device)) |
52 | .with_state(shared_state); | 52 | .with_state(shared_state); |
53 | 53 | ||
54 | // TODO: Add to config | 54 | let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); |
55 | axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) | 55 | info!("start server on {}", addr); |
56 | axum::Server::bind(&addr.parse().unwrap()) | ||
56 | .serve(app.into_make_service()) | 57 | .serve(app.into_make_service()) |
57 | .await | 58 | .await |
58 | .unwrap(); | 59 | .unwrap(); |
@@ -63,9 +64,13 @@ pub struct AppState { | |||
63 | } | 64 | } |
64 | 65 | ||
65 | async fn init_db_pool() -> PgPool { | 66 | async fn init_db_pool() -> PgPool { |
67 | #[cfg(not(debug_assertions))] | ||
68 | let db_url = SETTINGS.get_string("database.url").unwrap(); | ||
69 | |||
70 | #[cfg(debug_assertions)] | ||
66 | let db_url = env::var("DATABASE_URL").unwrap(); | 71 | let db_url = env::var("DATABASE_URL").unwrap(); |
67 | 72 | ||
68 | debug!("attempting to connect dbPool to '{}'", db_url); | 73 | debug!("attempt to connect dbPool to '{}'", db_url); |
69 | 74 | ||
70 | let pool = PgPoolOptions::new() | 75 | let pool = PgPoolOptions::new() |
71 | .max_connections(5) | 76 | .max_connections(5) |
diff --git a/src/routes/device.rs b/src/routes/device.rs index d5d7144..025c7d0 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -4,11 +4,13 @@ use axum::headers::HeaderMap; | |||
4 | use axum::Json; | 4 | use axum::Json; |
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | use serde_json::{json, Value}; | 6 | use serde_json::{json, Value}; |
7 | use tracing::info; | ||
7 | use crate::auth::auth; | 8 | use crate::auth::auth; |
8 | use crate::db::Device; | 9 | use crate::db::Device; |
9 | use crate::error::WebolError; | 10 | use crate::error::WebolError; |
10 | 11 | ||
11 | pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, WebolError> { | 12 | pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, WebolError> { |
13 | info!("GET request"); | ||
12 | let secret = headers.get("authorization"); | 14 | let secret = headers.get("authorization"); |
13 | if auth(secret).map_err(WebolError::Auth)? { | 15 | if auth(secret).map_err(WebolError::Auth)? { |
14 | let device = sqlx::query_as!( | 16 | let device = sqlx::query_as!( |
@@ -33,6 +35,7 @@ pub struct GetDevicePayload { | |||
33 | } | 35 | } |
34 | 36 | ||
35 | pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, WebolError> { | 37 | pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, WebolError> { |
38 | info!("PUT request"); | ||
36 | let secret = headers.get("authorization"); | 39 | let secret = headers.get("authorization"); |
37 | if auth(secret).map_err(WebolError::Auth)? { | 40 | if auth(secret).map_err(WebolError::Auth)? { |
38 | sqlx::query!( | 41 | sqlx::query!( |
@@ -64,6 +67,7 @@ pub struct PutDeviceResponse { | |||
64 | } | 67 | } |
65 | 68 | ||
66 | pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, WebolError> { | 69 | pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, WebolError> { |
70 | info!("POST request"); | ||
67 | let secret = headers.get("authorization"); | 71 | let secret = headers.get("authorization"); |
68 | if auth(secret).map_err(WebolError::Auth)? { | 72 | if auth(secret).map_err(WebolError::Auth)? { |
69 | let device = sqlx::query_as!( | 73 | let device = sqlx::query_as!( |
diff --git a/src/routes/start.rs b/src/routes/start.rs index d16ea4e..2e6a648 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -28,7 +28,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
28 | 28 | ||
29 | let bind_addr = SETTINGS | 29 | let bind_addr = SETTINGS |
30 | .get_string("bindaddr") | 30 | .get_string("bindaddr") |
31 | .map_err(|err| WebolError::Server(Box::new(err)))?; | 31 | .unwrap_or("0.0.0.0:1111".to_string()); |
32 | 32 | ||
33 | let _ = send_packet( | 33 | let _ = send_packet( |
34 | &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, | 34 | &bind_addr.parse().map_err(|err| WebolError::Server(Box::new(err)))?, |