adding docker and docker compose files to make app ready for deployment

This commit is contained in:
2021-09-05 20:53:21 +02:00
parent d672849a25
commit bf0e8f6306
5 changed files with 44 additions and 7 deletions

1
.dockerignore Normal file
View File

@@ -0,0 +1 @@
target

7
.env
View File

@@ -1,3 +1,8 @@
DATABASE_URL=sqlite://db/db.db DATABASE_URL=sqlite://db/db.db
# PROTOCOL and BASE_URL are used to build links in application
PROTOCOL=http PROTOCOL=http
BASE_URL=localhost:8080 BASE_URL=localhost:8080
# HOST tells the app which address to bind to (do not change for docker) default is 0.0.0.0:8080
# HOST=localhost:8080

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM rust:1.54 as builder
RUN apt-get update && apt-get install -y sqlite3 && rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/ics-proxy
COPY . .
RUN cd db && ./create_db.sh
RUN cargo install --path .
FROM debian:buster-slim
RUN apt-get update && apt-get install -y sqlite3 openssl && rm -rf /var/lib/apt/lists/*
WORKDIR app
COPY --from=builder /usr/local/cargo/bin/ics-proxy ./ics-proxy
COPY --from=builder /usr/src/ics-proxy/db db
COPY templates ./templates
CMD ["./ics-proxy"]

12
docker-compose.yml Normal file
View File

@@ -0,0 +1,12 @@
version: '3'
services:
ics-proxy:
container_name: ics-proxy
build: .
env_file: .env
ports:
- "8080:8080"
volumes:
- database:/app/db
volumes:
database:

View File

@@ -9,7 +9,6 @@ use uuid::Uuid;
extern crate dotenv; extern crate dotenv;
use dotenv::dotenv; use dotenv::dotenv;
use std::env;
mod model; mod model;
use model::Link; use model::Link;
@@ -279,6 +278,11 @@ async fn main() -> std::io::Result<()> {
let base_url = let base_url =
std::env::var("BASE_URL").expect("BASE_URL environemt variable error, make sure it is set"); std::env::var("BASE_URL").expect("BASE_URL environemt variable error, make sure it is set");
let host = match std::env::var("HOST") {
Ok(host) => host,
Err(_e) => "0.0.0.0:8080".to_string(),
};
let conf = Config { let conf = Config {
root: format!("{}://{}", protocol, base_url), root: format!("{}://{}", protocol, base_url),
}; };
@@ -287,9 +291,12 @@ async fn main() -> std::io::Result<()> {
.await .await
.expect("could not create db pool"); .expect("could not create db pool");
println!("Listening on: 127.0.0.1:8080, open browser and visit have a try!"); println!(
"Listening on: {}://{}, open browser and visit have a try!",
protocol, base_url
);
HttpServer::new(move || { HttpServer::new(move || {
let tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap(); let tera = Tera::new("templates/**/*.html").unwrap();
App::new() App::new()
.data(db_pool.clone()) // pass database pool to application so we can access it inside handlers .data(db_pool.clone()) // pass database pool to application so we can access it inside handlers
@@ -301,9 +308,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/index_process").route(web::get().to(index_process))) .service(web::resource("/index_process").route(web::get().to(index_process)))
.service(web::resource("/edit_process").route(web::get().to(edit_process))) .service(web::resource("/edit_process").route(web::get().to(edit_process)))
}) })
.bind("127.0.0.1:8080")? .bind(host)?
.run() .run()
.await .await
//.route("/{id}/events.ics", web::get().to(make_ics_request))
} }