diff options
| -rw-r--r-- | Dockerfile | 34 | ||||
| -rw-r--r-- | docker-compose.yml | 26 | ||||
| -rw-r--r-- | server/src/main.rs | 9 |
3 files changed, 53 insertions, 16 deletions
diff --git a/Dockerfile b/Dockerfile index b9a177a..e003845 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,26 @@ -FROM ekidd/rust-musl-builder:latest AS rust -ADD --chown=rust:rust . ./ +FROM ekidd/rust-musl-builder:latest AS server-cache +WORKDIR /server +COPY server/Cargo.* ./ +RUN sudo mkdir src +RUN echo "fn main() {}" | sudo dd of=src/main.rs +RUN sudo chown rust:rust . RUN cargo build --release +RUN rm -f target/x86_64-unknown-linux-musl/release/deps/shorest* -FROM node:alpine -RUN apk --no-cache add ca-certificates -COPY --from=rust \ - /home/rust/ \ - /main -WORKDIR /main/src -RUN cd ./client && yarn install && yarn build; -RUN mv ./target/x86_64-unknown-linux-musl/release/shorest ./shorest -CMD ./shorest +FROM node:alpine as client +WORKDIR /client +COPY client/package*.json ./ +RUN yarn install +COPY client ./ +RUN yarn build + +FROM server-cache as server +WORKDIR /server +COPY server/src src +RUN cargo build --release +FROM alpine:latest +WORKDIR /shorest +COPY --from=client /client/build client/ +COPY --from=server /server/target/x86_64-unknown-linux-musl/release/shorest shorest +CMD ./shorest diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5195255 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "3.8" +services: + server: + build: + context: . + container_name: shorest-server + ports: + - "127.0.0.1:${PORT}:${PORT}" + depends_on: + - postgres + links: + - postgres:postgres + environment: + - PORT + - POSTGRES_PASSWORD + postgres: + image: "postgres:alpine" + container_name: shorest-postgres + volumes: + - database:/var/lib/postgresql/data + environment: + - POSTGRES_DB=shorest + - POSTGRES_PASSWORD + +volumes: + database: diff --git a/server/src/main.rs b/server/src/main.rs index 6ca2fe3..92df222 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -11,12 +11,12 @@ use types::*; use actix_web::{middleware, web, HttpServer, App, HttpResponse, Result, HttpRequest}; use actix_web::web::{Json, Path, Data}; use diesel::{PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, QueryResult}; -use dotenv::dotenv; use diesel::r2d2::{ConnectionManager, Pool}; use actix_files::{Files, NamedFile}; fn establish_connection() -> Pool<ConnectionManager<PgConnection>> { - let database_url = std::env::var("DATABASE_URL").expect("Cannot find DATABASE_URL. Check .env file."); + let password = std::env::var("POSTGRES_PASSWORD").expect("Cannot find POSTGRES_PASSWORD in environment."); + let database_url = format!("postgresql://postgres:{}@postgres/shorest", password); let manager = ConnectionManager::<PgConnection>::new(database_url); Pool::builder().max_size(4).build(manager).expect("Failed to create pool.") } @@ -70,7 +70,7 @@ fn add_to_database_safely(mut hash: String, user_url: String, connection: &PgCon } async fn root(req: HttpRequest) -> HttpResponse { - NamedFile::open("./client/build/index.html").unwrap().into_response(&req).unwrap() + NamedFile::open("./client/index.html").unwrap().into_response(&req).unwrap() } async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse { @@ -94,7 +94,6 @@ async fn redirect(info: Path<String>, state: Data<PoolState>) -> HttpResponse { #[actix_rt::main] async fn main() -> std::io::Result<()> { - dotenv().ok(); std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); @@ -118,7 +117,7 @@ async fn main() -> std::io::Result<()> { .route(web::get().to(redirect)) ) .service( - Files::new("/client/", "./client/build/") + Files::new("/client/", "./client/") ) }) .bind(("0.0.0.0", port))? |
