Добавление докерфайла

This commit is contained in:
Кобелев Андрей Андреевич 2022-10-13 16:00:35 +03:00
parent c4db474fc8
commit 971dc91dfd
8 changed files with 154 additions and 1116 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
/target
cats.db
.DS_Store

3
.gitignore vendored
View File

@ -1,5 +1,6 @@
/target
/frontend
cats.db
.DS_Store
settings.yml
settings.yml

1034
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,17 +1,14 @@
[package]
name = "loyalty_rust"
name = "cat_rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sqlx = { version = "0.6", features = [ "runtime-async-std-native-tls" ] }
actix-web = "4"
config = "0.13.1"
serde = "1.0.8"
deadpool-postgres = { version = "0.10.2", features = ["serde"] }
tokio-postgres = "0.7.6"
rusqlite = { version = "0.28.0", features = ["bundled"] }
r2d2 = "0.8"
r2d2_sqlite = "0.21"

21
Dockerfile Normal file
View File

@ -0,0 +1,21 @@
FROM rust:1.62.1-alpine3.16
# This is important, see https://github.com/rust-lang/docker-rust/issues/85
ENV RUSTFLAGS="-C target-feature=-crt-static"
# if needed, add additional dependencies here
RUN apk add --no-cache musl-dev
# set the workdir and copy the source into it
WORKDIR /app
COPY ./ /app
# do a release build
RUN cargo build --release
RUN strip target/release/cat_rust
# use a plain alpine image, the alpine version needs to match the builder
FROM alpine:3.16
WORKDIR /app
# if needed, install additional dependencies here
RUN apk add --no-cache libgcc
# copy the binary into the final image
COPY --from=0 /app/target/release/cat_rust .
# set the binary as entrypoint
ENTRYPOINT ["/app/cat_rust"]

18
docker-compose.yml Normal file
View File

@ -0,0 +1,18 @@
version: "3.9"
services:
app:
image: docker.io/library/cat-rust
volumes:
- ./settings.yml:/app/settings.yml
- ./static:/app/static
ports:
- 8080:8080
dozzle:
container_name: dozzle
image: amir20/dozzle:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 8070:8080

View File

@ -1,4 +1,5 @@
use rusqlite::{Error, Result};
use actix_web::{web, Error};
use rusqlite::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@ -19,6 +20,7 @@ pub type CatsResult = Result<Vec<Cat>, Error>;
pub type ColorsResult = Result<Vec<Color>, Error>;
pub type CatResult = Result<Cat, Error>;
pub type ColorResult = Result<Color, Error>;
pub type Pool = r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>;
// Сервис базы данных
@ -76,31 +78,35 @@ impl Db {
}
// Получение кошек
pub fn get_cats(&self) -> CatsResult {
let conn = match self.pool.get() {
Ok(conn) => conn,
Err(err) => panic!("{}", err),
};
let mut stmt = conn.prepare(
"SELECT c.id, c.name, cc.name from cats c INNER JOIN cat_colors cc ON cc.id = c.color_id;",
)?;
return stmt
.query_map([], |row| {
Ok(Cat {
id: row.get(0)?,
name: row.get(1)?,
color: row.get(2)?,
})
})
.and_then(Iterator::collect);
pub async fn get_cats(&self) -> CatsResult {
let pool = self.pool.clone();
let conn = web::block(move || pool.get())
.await?
.map_err(actix_web::error::ErrorInternalServerError)?;
return web::block(move || {
let mut stmt = conn.prepare(
"SELECT c.id, c.name, cc.name from cats c INNER JOIN cat_colors cc ON cc.id = c.color_id;",
)?;
return stmt
.query_map([], |row| {
Ok(Cat {
id: row.get(0)?,
name: row.get(1)?,
color: row.get(2)?,
})
})
.and_then(Iterator::collect);
}).await?
.map_err(actix_web::error::ErrorInternalServerError);
}
// Получение cлучайной кошки
pub fn get_random_cat(&self) -> CatResult {
let conn = match self.pool.get() {
Ok(conn) => conn,
Err(err) => panic!("{}", err),
};
return conn.query_row("SELECT c.id, c.name, cc.name from cats c INNER JOIN cat_colors cc ON cc.id = c.color_id ORDER BY random() LIMIT 1;",
pub async fn get_random_cat(&self) -> CatResult {
let pool = self.pool.clone();
let conn = web::block(move || pool.get())
.await?
.map_err(actix_web::error::ErrorInternalServerError)?;
return web::block(move || {
return conn.query_row("SELECT c.id, c.name, cc.name from cats c INNER JOIN cat_colors cc ON cc.id = c.color_id ORDER BY random() LIMIT 1;",
[],
|row| {
Ok(Cat {
@ -108,33 +114,40 @@ impl Db {
name: row.get(1)?,
color: row.get(2)?,
})
},
);
})
})
.await?
.map_err(actix_web::error::ErrorInternalServerError);
}
// Получение цветов
pub fn get_colors(&self) -> ColorsResult {
let conn = match self.pool.get() {
Ok(conn) => conn,
Err(err) => panic!("{}", err),
};
let mut stmt = conn.prepare("SELECT id, name from cat_colors")?;
return stmt
.query_map([], |row| {
Ok(Color {
id: row.get(0)?,
name: row.get(1)?,
pub async fn get_colors(&self) -> ColorsResult {
let pool = self.pool.clone();
let conn = web::block(move || pool.get())
.await?
.map_err(actix_web::error::ErrorInternalServerError)?;
return web::block(move || {
let mut stmt = conn.prepare("SELECT id, name from cat_colors")?;
return stmt
.query_map([], |row| {
Ok(Color {
id: row.get(0)?,
name: row.get(1)?,
})
})
})
.and_then(Iterator::collect);
.and_then(Iterator::collect);
})
.await?
.map_err(actix_web::error::ErrorInternalServerError);
}
// Добавление кошки
pub fn add_cat(&self, cat_name: String, cat_color: String) -> CatResult {
let conn = match self.pool.get() {
Ok(conn) => conn,
Err(err) => panic!("{}", err),
};
pub async fn add_cat(&self, cat_name: String, cat_color: String) -> CatResult {
let pool = self.pool.clone();
let conn = web::block(move || pool.get())
.await?
.map_err(actix_web::error::ErrorInternalServerError)?;
return web::block(move || {
return conn.query_row("INSERT INTO cats (name, color_id) SELECT ?1, id FROM cat_colors cc WHERE name = ?2 RETURNING id, name, ?2",
[cat_name, cat_color],
|row| {
@ -144,23 +157,28 @@ impl Db {
color: row.get(2)?,
})
},
);
)
}).await?.map_err(actix_web::error::ErrorInternalServerError);
}
// Добавление цвета
pub fn add_color(&self, color_name: String) -> ColorResult {
let conn = match self.pool.get() {
Ok(conn) => conn,
Err(err) => panic!("{}", err),
};
return conn.query_row(
"INSERT INTO cat_colors (name) VALUES (?1) RETURNING id, name",
[color_name],
|row| {
Ok(Color {
id: row.get(0)?,
name: row.get(1)?,
})
},
);
pub async fn add_color(&self, color_name: String) -> ColorResult {
let pool = self.pool.clone();
let conn = web::block(move || pool.get())
.await?
.map_err(actix_web::error::ErrorInternalServerError)?;
return web::block(move || {
return conn.query_row(
"INSERT INTO cat_colors (name) VALUES (?1) RETURNING id, name",
[color_name],
|row| {
Ok(Color {
id: row.get(0)?,
name: row.get(1)?,
})
},
);
})
.await?
.map_err(actix_web::error::ErrorInternalServerError);
}
}

View File

@ -1,5 +1,5 @@
pub mod db;
use actix_web::{get, post, web, HttpResponse, Responder, Result};
use actix_web::{get, post, web, Error, HttpResponse, Responder, Result};
use db::Db;
use serde::{Deserialize, Serialize};
@ -15,32 +15,20 @@ pub struct AddColorRequest {
#[get("/api/cats")]
pub async fn get_cats(db: web::Data<Db>) -> Result<impl Responder> {
let cats = db.get_cats();
let res = match cats {
Ok(v) => ArrayResponse { result: v },
Err(_err) => ArrayResponse { result: vec![] },
};
return Ok(HttpResponse::Ok().json(res));
let cats = db.get_cats().await;
return Ok(HttpResponse::Ok().json(cats.map_err(Error::from)?));
}
#[get("/api/cat/random")]
pub async fn get_random_cat(db: web::Data<Db>) -> Result<impl Responder> {
let cats = db.get_random_cat();
let res = match cats {
Ok(v) => v,
Err(_err) => panic!("{:?}", _err),
};
return Ok(HttpResponse::Ok().json(res));
let cat = db.get_random_cat().await;
return Ok(HttpResponse::Ok().json(cat.map_err(Error::from)?));
}
#[get("/colors")]
#[get("/api/colors")]
pub async fn get_colors(db: web::Data<Db>) -> Result<impl Responder> {
let colors = db.get_colors();
let res = match colors {
Ok(v) => ArrayResponse { result: v },
Err(_err) => ArrayResponse { result: vec![] },
};
return Ok(HttpResponse::Ok().json(res));
let colors = db.get_colors().await;
return Ok(HttpResponse::Ok().json(colors.map_err(Error::from)?));
}
#[derive(Debug, Serialize, Deserialize)]
@ -49,27 +37,19 @@ pub struct AddCatRequest {
pub color: String,
}
#[post("/add/cat")]
#[post("/api/add/cat")]
pub async fn add_cat(db: web::Data<Db>, cat: web::Json<AddCatRequest>) -> Result<impl Responder> {
let _cat = cat.into_inner();
let cat = db.add_cat(_cat.name, _cat.color);
let res = match cat {
Ok(v) => ArrayResponse { result: vec![v] },
Err(_err) => panic!("{:?}", _err),
};
return Ok(HttpResponse::Ok().json(res));
let cat = db.add_cat(_cat.name, _cat.color).await;
return Ok(HttpResponse::Ok().json(cat.map_err(Error::from)?));
}
#[post("/add/color")]
#[post("/api/add/color")]
pub async fn add_color(
db: web::Data<Db>,
cat: web::Json<AddColorRequest>,
) -> Result<impl Responder> {
let _color = cat.into_inner();
let cat = db.add_color(_color.name);
let res = match cat {
Ok(v) => ArrayResponse { result: vec![v] },
Err(_err) => panic!("{:?}", _err),
};
return Ok(HttpResponse::Ok().json(res));
let cat = db.add_color(_color.name).await;
return Ok(HttpResponse::Ok().json(cat.map_err(Error::from)?));
}