Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
SECRET_TOKEN=

DATABASE_URL=postgres://postgres:postgres@db/api
DATABASE_URL_TEST=postgres://postgres:postgres@db_test/api_test
DATABASE_MAX_CONNECTIONS=30

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
AWS_REGION=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ schemaspy/.DS_Store
.DS_Store
.vscode
.env
shell.nix
19 changes: 15 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
FROM rustlang/rust:nightly
FROM ubuntu:22.04

WORKDIR /app

ADD src src
COPY Cargo.toml .

RUN cargo install cargo-watch
RUN cargo install diesel_cli --no-default-features --features postgres
RUN cargo build
# Dependencies
RUN apt-get -y update
RUN apt-get -y install curl build-essential libpq-dev pkg-config netcat systemd

# Rustup install (not yet packaged on Ubuntu)
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

# Rust toolchain
RUN ${HOME}/.cargo/bin/rustup install 1.63.0

# Cargo stuff
RUN ${HOME}/.cargo/bin/cargo install cargo-watch
RUN ${HOME}/.cargo/bin/cargo install diesel_cli --no-default-features --features postgres
RUN ${HOME}/.cargo/bin/cargo build
35 changes: 7 additions & 28 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ services:

api:
container_name: api
image: rustlang/rust:nightly
environment:
SECRET_TOKEN: 'Xqv8jTGLxT'
DATABASE_URL: 'postgres://postgres:postgres@db/api'
DATABASE_URL_TEST: 'postgres://postgres:postgres@db_test/api_test'
DATABASE_MAX_CONNECTIONS: '30'
init: true
privileged: true
devices:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
env_file:
- .env
build:
context: ./
volumes:
Expand All @@ -19,50 +19,29 @@ services:
depends_on:
- db
- db_test
command: >
bash -c "diesel setup && diesel migration run && diesel migration run --database-url=$${DATABASE_URL_TEST} && cargo watch -x run"
entrypoint: ["/bin/bash", "./entrypoint.sh"]

db:
container_name: db
image: postgres:14.2
environment:
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
POSTGRES_DB: 'api'
PGDATA: /data/postgres
volumes:
- db:/data/postgres
ports:
- "5433:5432"
restart: always

db_test:
container_name: db_test
image: postgres:14.2
environment:
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'postgres'
POSTGRES_DB: 'api_test'
PGDATA: /data/postgres
volumes:
- db_test:/data/postgres
ports:
- "5434:5432"
restart: always
schemaspy:
image: schemaspy/schemaspy:snapshot
volumes:
- ./schemaspy/output:/output
- ./schemaspy/config:/config
container_name: "schemaspy_local"
command: [
"-configFile",
"/config/schemaspy.properties",
"-imageformat",
"svg"
]
depends_on:
- api

volumes:
db:
Expand Down
12 changes: 12 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Wait for PostgreSQL
for d in db db_test; do
until nc -z $d 5432
do
echo "Waiting for connection to MySQL database..."
sleep 2
done
done

${HOME}/.cargo/bin/diesel migration run &&
${HOME}/.cargo/bin/diesel migration run --database-url=${DATABASE_URL_TEST} &&
${HOME}/.cargo/bin/cargo watch -x run
74 changes: 74 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
inputs = {
utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
};

outputs = { self, nixpkgs, utils, naersk }:
utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages."${system}";
naersk-lib = naersk.lib."${system}";
in rec {
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo rust-analyzer ];
};
});
}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ async fn main() -> std::io::Result<()> {
.route("/", web::get().to(index))
.service(metrics::add_metrics)
.service(organizations::add_organization)
.service(monitors::add_monitor)
// .service(monitors::get_all_monitors_of_user)
.service(agents::add_agents)
.service(roles::add_role)
.service(roles::get_roles)
.service(users::register)
.service(users::login)
.service(users::user_informations)
.service(monitors::add_monitor)
})
.bind(("0.0.0.0", 8080))?
.run()
Expand Down
49 changes: 10 additions & 39 deletions src/monitors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
use self::model::{FormMonitor, InsertableMonitor, Monitor};
use super::DbPool;
use crate::middlewares::auth::AuthorizationService;
use actix_web::{get, post, web, Error, HttpRequest, HttpResponse};
pub mod model;
use actix_web::{post, web, Result, HttpResponse, Error};
use uuid::Uuid;

/// Inserts new monitor with name, id_agent, id_organization defined in form.
#[post("/monitors")]
pub async fn add_monitor(
pool: web::Data<DbPool>,
form: web::Json<FormMonitor>,
_req: HttpRequest,
_: AuthorizationService,
) -> Result<HttpResponse, Error> {
let token = crate::users::model::User::get_token_from_request(&_req);
use crate::monitors::model::*;
mod model;
mod util;

let monitor = web::block(move || {
let conn = pool.get()?;
Monitor::insert_new_monitor(&token, &form, &conn)
})
.await?
.map_err(actix_web::error::ErrorInternalServerError)?;

Ok(HttpResponse::Created().json(monitor))
#[post("/monitors")]
async fn add_monitor(monitor: web::Json<Monitor>) -> Result<HttpResponse, Error> {
Monitor::write_new_monitor(monitor)?;
//Monitor::insert_new_monitor(monitor)?;
Ok(HttpResponse::Ok().body("Service created"))
}

// Get all monitors of the user
// #[get("/monitors")]
// pub async fn get_all_monitors_of_user(
// pool: web::Data<DbPool>,
// _req: HttpRequest,
// _: AuthorizationService,
// ) -> Result<HttpResponse, Error> {
// let token = crate::users::model::User::get_token_from_request(&_req);

// let monitors = web::block(move || {
// let conn = pool.get()?;
// Monitor::get_all_monitors_of_user(&token, &conn)
// })
// .await?
// .map_err(actix_web::error::ErrorInternalServerError)?;

// Ok(HttpResponse::Ok().json(monitors))
// }
Loading