Compare commits
1 Commits
feat/v2
...
feat/with-
Author | SHA1 | Date | |
---|---|---|---|
0c0bbc4282
|
88
.drone.yml
Normal file
88
.drone.yml
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
kind: pipeline
|
||||||
|
name: default
|
||||||
|
type: docker
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: load_secret
|
||||||
|
image: debian:buster-slim
|
||||||
|
volumes:
|
||||||
|
- name: ssh
|
||||||
|
path: /root/.ssh/
|
||||||
|
environment:
|
||||||
|
SSH_KEY:
|
||||||
|
from_secret: gitea_id_ed25519
|
||||||
|
commands:
|
||||||
|
- mkdir -p $HOME/.ssh/
|
||||||
|
- echo "$SSH_KEY" | base64 -d > $HOME/.ssh/id_ed25519
|
||||||
|
|
||||||
|
- name: build
|
||||||
|
image: kasperhermansen/cuddle:latest
|
||||||
|
pull: always
|
||||||
|
volumes:
|
||||||
|
- name: ssh
|
||||||
|
path: /root/.ssh/
|
||||||
|
- name: dockersock
|
||||||
|
path: /var/run
|
||||||
|
commands:
|
||||||
|
- apk add bash git
|
||||||
|
- git remote set-url origin $DRONE_GIT_SSH_URL
|
||||||
|
- cuddle_cli x setup_ssh
|
||||||
|
- cuddle_cli x start_deployment
|
||||||
|
- cuddle_cli x render_templates
|
||||||
|
- cuddle_cli x render_como_templates
|
||||||
|
- cuddle_cli x build_release
|
||||||
|
- cuddle_cli x push_release
|
||||||
|
- cuddle_cli x deploy_release
|
||||||
|
environment:
|
||||||
|
DOCKER_BUILDKIT: 1
|
||||||
|
DOCKER_USERNAME:
|
||||||
|
from_secret: docker_username
|
||||||
|
DOCKER_PASSWORD:
|
||||||
|
from_secret: docker_password
|
||||||
|
SSH_KEY:
|
||||||
|
from_secret: gitea_id_ed25519
|
||||||
|
|
||||||
|
- name: push_tags
|
||||||
|
image: kasperhermansen/drone-semantic-release:latest
|
||||||
|
pull: always
|
||||||
|
volumes:
|
||||||
|
- name: ssh
|
||||||
|
path: /root/.ssh/
|
||||||
|
- name: dockersock
|
||||||
|
path: /var/run
|
||||||
|
commands:
|
||||||
|
- semantic-release --no-ci
|
||||||
|
environment:
|
||||||
|
DOCKER_BUILDKIT: 1
|
||||||
|
SSH_KEY:
|
||||||
|
from_secret: gitea_id_ed25519
|
||||||
|
depends_on:
|
||||||
|
- build
|
||||||
|
|
||||||
|
- name: send telegram notification
|
||||||
|
image: appleboy/drone-telegram
|
||||||
|
settings:
|
||||||
|
token:
|
||||||
|
from_secret: telegram_token
|
||||||
|
to: 2129601481
|
||||||
|
format: markdown
|
||||||
|
depends_on:
|
||||||
|
- build
|
||||||
|
- push_tags
|
||||||
|
when:
|
||||||
|
status: [failure, success]
|
||||||
|
|
||||||
|
services:
|
||||||
|
- name: docker
|
||||||
|
image: docker:dind
|
||||||
|
privileged: true
|
||||||
|
volumes:
|
||||||
|
- name: dockersock
|
||||||
|
path: /var/run
|
||||||
|
volumes:
|
||||||
|
- name: ssh
|
||||||
|
temp: {}
|
||||||
|
- name: dockersock
|
||||||
|
temp: {}
|
||||||
|
|
||||||
|
|
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
|||||||
target/
|
/target
|
||||||
.cuddle/
|
.cuddle/
|
||||||
|
node_modules/
|
||||||
|
.env
|
||||||
|
8
.releaserc.yml
Normal file
8
.releaserc.yml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
branches:
|
||||||
|
- "main"
|
||||||
|
|
||||||
|
plugins:
|
||||||
|
- "@semantic-release/commit-analyzer"
|
||||||
|
- "@semantic-release/release-notes-generator"
|
||||||
|
- "@semantic-release/changelog"
|
||||||
|
- "@semantic-release/git"
|
4062
Cargo.lock
generated
4062
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
52
Cargo.toml
52
Cargo.toml
@@ -1,3 +1,53 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [ "crates/*" ]
|
members = [
|
||||||
|
"como_bin",
|
||||||
|
"como_core",
|
||||||
|
"como_domain",
|
||||||
|
"como_infrastructure",
|
||||||
|
"como_gql",
|
||||||
|
"como_api",
|
||||||
|
]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
|
[workspace.dependencies]
|
||||||
|
como_bin = { path = "./como_bin/" }
|
||||||
|
como_core = { path = "./como_core/" }
|
||||||
|
como_domain = { path = "./como_domain/" }
|
||||||
|
como_infrastructure = { path = "./como_infrastructure/" }
|
||||||
|
como_gql = { path = "./como_gql/" }
|
||||||
|
como_api = { path = "./como_api/" }
|
||||||
|
|
||||||
|
async-trait = "0.1.68"
|
||||||
|
async-graphql = { version = "5.0.9", features = ["uuid"] }
|
||||||
|
async-graphql-axum = "5.0.9"
|
||||||
|
|
||||||
|
axum = { version = "0.6.18", features = ["headers", "macros"] }
|
||||||
|
axum-extra = { version = "0.7.4", features = ["cookie", "cookie-private"] }
|
||||||
|
axum-sessions = { version = "0.5.0" }
|
||||||
|
async-sqlx-session = { version = "0.4.0", features = ["pg"] }
|
||||||
|
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0.68"
|
||||||
|
|
||||||
|
sqlx = { version = "0.6.2", features = [
|
||||||
|
"runtime-tokio-rustls",
|
||||||
|
"postgres",
|
||||||
|
"migrate",
|
||||||
|
"uuid",
|
||||||
|
"offline",
|
||||||
|
"time",
|
||||||
|
"chrono",
|
||||||
|
] }
|
||||||
|
chrono = { version = "0.4.26", features = ["serde"] }
|
||||||
|
|
||||||
|
tokio = { version = "1.28.2", features = ["full"] }
|
||||||
|
|
||||||
|
uuid = { version = "1.3.3", features = ["v4", "fast-rng", "serde"] }
|
||||||
|
anyhow = "1.0.71"
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
tracing = "0.1.37"
|
||||||
|
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
|
||||||
|
clap = { version = "4.3.0", features = ["derive", "env"] }
|
||||||
|
|
||||||
|
argon2 = { version = "0.5.0" }
|
||||||
|
rand_core = { version = "0.6.4" }
|
||||||
|
77
README.md
77
README.md
@@ -1,76 +1,3 @@
|
|||||||
# Omnia
|
# Cibus Backend
|
||||||
|
|
||||||
`Omnia` is a tool to provide a common platform for everything. It is a tool
|
Some text
|
||||||
build to support note-taking, handling personal relationships, project
|
|
||||||
development, todos, research, up-keep of external work, and much more.
|
|
||||||
|
|
||||||
This may seem like a large list of tasks, and it is. However, Omnia is designed
|
|
||||||
to be opinionated, and provide a minimalistic approach to each of the above.
|
|
||||||
Omnia is not a general purpose tool, text editing tool, it is designed to fit
|
|
||||||
into your existing toolstack, with its opinionated project structure, and
|
|
||||||
workflow.
|
|
||||||
|
|
||||||
Alternatives to this tool:
|
|
||||||
|
|
||||||
- Notion: with second brain templates etc.
|
|
||||||
- Obsidian: with zen garden templates etc.
|
|
||||||
|
|
||||||
Omnia is a commandline tool, which functions using a terminal ui, or pure cli
|
|
||||||
commands. Called `interactive` for the former, and `prompt` for the latter. It
|
|
||||||
is designed to work with your favorite EDITOR, to keep the scope of this project
|
|
||||||
reasonable, `Omnia` doesn't try to bundle with an editor. Instead it will
|
|
||||||
respect your `OMNIA_EDITOR`, or `EDITOR` environment variables, and launch the
|
|
||||||
files using those.
|
|
||||||
|
|
||||||
## How to use `Omnia`
|
|
||||||
|
|
||||||
To launch the fully interactive view, simply type `omnia` in your shell of
|
|
||||||
choice. This will boot up the TUI, press `?` for help and it will show a brief,
|
|
||||||
menu as well as the most common keybinds.
|
|
||||||
|
|
||||||
Following each command will be shown separately, these will be available in the
|
|
||||||
UI as well, just follow along in the menu, or use the command key `:` to open
|
|
||||||
the command palette.
|
|
||||||
|
|
||||||
### Commands
|
|
||||||
|
|
||||||
In Omnia everything is designed to use Markdown files, even the templates are
|
|
||||||
markdown files, though with some special syntax to make prompting easier. This
|
|
||||||
also means that you can open your local ~/omnia directory using your favorite
|
|
||||||
editor, as everything is just markdown files.
|
|
||||||
|
|
||||||
Projects are the cornerstone of how Omnia functions. Every navigation item is a
|
|
||||||
project, be they todo lists, research items, external sites etc. Projects can be
|
|
||||||
nested, and projects can contain pages. External apps can be configured as a
|
|
||||||
project, and will need a certain interface to be functional.
|
|
||||||
|
|
||||||
This means that when you type a command:
|
|
||||||
|
|
||||||
`omnia --help`, each subcommand will be a project, some keywords are reserved:
|
|
||||||
(todo, inbox, project, etc.), each project decides which commands are available,
|
|
||||||
and this is fully customizeable, through our plugin system. Though note that we
|
|
||||||
ship the default view with a set of preconfigured plugins:
|
|
||||||
|
|
||||||
- todo
|
|
||||||
- inbox
|
|
||||||
- projects
|
|
||||||
- areas
|
|
||||||
|
|
||||||
Typing each of the commands above will open the fully interactive tui:
|
|
||||||
`omnia todo`. Todo has a set of commands available to it: `omnia todo create` as
|
|
||||||
an example. This will open the prompt view, which will interactive ask the user
|
|
||||||
to fill out a form. These questions will also be available using commandline
|
|
||||||
flags as well.
|
|
||||||
|
|
||||||
### Views
|
|
||||||
|
|
||||||
Some of the projects are built as views, this may be a list of recent
|
|
||||||
notifications on github, apis of interest, metrics and whatnot.
|
|
||||||
|
|
||||||
## Remote first
|
|
||||||
|
|
||||||
Omnia will sync remote first in nearly all cases. The only exception is in
|
|
||||||
progress forms and whatnot. This is to keep complexity down, as well as making
|
|
||||||
sure Omnia is as easily crossplatform as possible. You should be able to use
|
|
||||||
`Omnia` from all your terminal capable devices. This sprung out of my own need
|
|
||||||
for having my notes available everywhere, without having conflicts.
|
|
||||||
|
32
como_api/Cargo.toml
Normal file
32
como_api/Cargo.toml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
[package]
|
||||||
|
name = "como_api"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
como_gql.workspace = true
|
||||||
|
como_core.workspace = true
|
||||||
|
como_domain.workspace = true
|
||||||
|
como_infrastructure.workspace = true
|
||||||
|
|
||||||
|
async-graphql.workspace = true
|
||||||
|
async-graphql-axum.workspace = true
|
||||||
|
axum.workspace = true
|
||||||
|
axum-extra.workspace = true
|
||||||
|
axum-sessions.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
tokio.workspace = true
|
||||||
|
uuid.workspace = true
|
||||||
|
sqlx.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
async-sqlx-session.workspace = true
|
||||||
|
|
||||||
|
zitadel = { version = "3.3.1", features = ["axum"] }
|
||||||
|
tower = "0.4.13"
|
||||||
|
tower-http = { version = "0.4.0", features = ["cors", "trace"] }
|
||||||
|
oauth2 = "4.4.0"
|
||||||
|
openidconnect = "3.0.0"
|
186
como_api/src/controllers/auth.rs
Normal file
186
como_api/src/controllers/auth.rs
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use crate::router::AppState;
|
||||||
|
use crate::zitadel::{IntrospectionConfig, IntrospectionState};
|
||||||
|
|
||||||
|
use async_sqlx_session::PostgresSessionStore;
|
||||||
|
use axum::extract::{FromRef, FromRequestParts, Query, State};
|
||||||
|
use axum::headers::authorization::Basic;
|
||||||
|
use axum::headers::{Authorization, Cookie};
|
||||||
|
use axum::http::request::Parts;
|
||||||
|
use axum::http::StatusCode;
|
||||||
|
use axum::http::{header::SET_COOKIE, HeaderMap};
|
||||||
|
use axum::response::{IntoResponse, Redirect};
|
||||||
|
use axum::routing::get;
|
||||||
|
use axum::{async_trait, RequestPartsExt, Router, TypedHeader};
|
||||||
|
use axum_sessions::async_session::{Session, SessionStore};
|
||||||
|
use como_domain::users::User;
|
||||||
|
use como_infrastructure::register::ServiceRegister;
|
||||||
|
use oauth2::basic::BasicClient;
|
||||||
|
use oauth2::{reqwest::async_http_client, AuthorizationCode, CsrfToken, Scope, TokenResponse};
|
||||||
|
use oauth2::{RedirectUrl, TokenIntrospectionResponse};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use zitadel::oidc::introspection::introspect;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ZitadelAuthParams {
|
||||||
|
return_url: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn zitadel_auth(State(client): State<BasicClient>) -> impl IntoResponse {
|
||||||
|
let (auth_url, _csrf_token) = client
|
||||||
|
.authorize_url(CsrfToken::new_random)
|
||||||
|
.add_scope(Scope::new("identify".to_string()))
|
||||||
|
.add_scope(Scope::new("openid".to_string()))
|
||||||
|
.url();
|
||||||
|
|
||||||
|
Redirect::to(auth_url.as_ref())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub static COOKIE_NAME: &str = "SESSION";
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub struct AuthRequest {
|
||||||
|
code: String,
|
||||||
|
state: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn login_authorized(
|
||||||
|
Query(query): Query<AuthRequest>,
|
||||||
|
State(store): State<PostgresSessionStore>,
|
||||||
|
State(oauth_client): State<BasicClient>,
|
||||||
|
State(introspection_state): State<IntrospectionState>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let token = oauth_client
|
||||||
|
.exchange_code(AuthorizationCode::new(query.code.clone()))
|
||||||
|
.request_async(async_http_client)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let config = IntrospectionConfig::from_ref(&introspection_state);
|
||||||
|
|
||||||
|
let res = introspect(
|
||||||
|
&config.introspection_uri,
|
||||||
|
&config.authority,
|
||||||
|
&config.authentication,
|
||||||
|
token.access_token().secret(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut session = Session::new();
|
||||||
|
session
|
||||||
|
.insert(
|
||||||
|
"user",
|
||||||
|
User {
|
||||||
|
id: res.sub().unwrap().into(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let cookie = store.store_session(session).await.unwrap().unwrap();
|
||||||
|
|
||||||
|
let cookie = format!("{}={}; SameSite=Lax; Path=/", COOKIE_NAME, cookie);
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(SET_COOKIE, cookie.parse().unwrap());
|
||||||
|
|
||||||
|
(headers, Redirect::to("http://localhost:3000/dash/home"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AuthController;
|
||||||
|
|
||||||
|
impl AuthController {
|
||||||
|
pub async fn new_router(
|
||||||
|
_service_register: ServiceRegister,
|
||||||
|
app_state: AppState,
|
||||||
|
) -> anyhow::Result<Router> {
|
||||||
|
Ok(Router::new()
|
||||||
|
.route("/zitadel", get(zitadel_auth))
|
||||||
|
.route("/authorized", get(login_authorized))
|
||||||
|
.with_state(app_state))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UserFromSession {
|
||||||
|
pub user: User,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<S> FromRequestParts<S> for UserFromSession
|
||||||
|
where
|
||||||
|
PostgresSessionStore: FromRef<S>,
|
||||||
|
BasicClient: FromRef<S>,
|
||||||
|
IntrospectionState: FromRef<S>,
|
||||||
|
S: Send + Sync,
|
||||||
|
{
|
||||||
|
type Rejection = (StatusCode, &'static str);
|
||||||
|
|
||||||
|
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||||
|
let store = PostgresSessionStore::from_ref(state);
|
||||||
|
|
||||||
|
let cookie: Option<TypedHeader<Cookie>> = parts.extract().await.unwrap();
|
||||||
|
|
||||||
|
let session_cookie = cookie.as_ref().and_then(|cookie| cookie.get(COOKIE_NAME));
|
||||||
|
if let None = session_cookie {
|
||||||
|
let introspection_state = IntrospectionState::from_ref(state);
|
||||||
|
|
||||||
|
let basic: Option<TypedHeader<Authorization<Basic>>> = parts.extract().await.unwrap();
|
||||||
|
|
||||||
|
if let Some(basic) = basic {
|
||||||
|
let config = IntrospectionConfig::from_ref(&introspection_state);
|
||||||
|
|
||||||
|
let res = introspect(
|
||||||
|
&config.introspection_uri,
|
||||||
|
&config.authority,
|
||||||
|
&config.authentication,
|
||||||
|
basic.password(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
return Ok(UserFromSession {
|
||||||
|
user: User {
|
||||||
|
id: res.sub().unwrap().into(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Err((StatusCode::UNAUTHORIZED, "No session was found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let session_cookie = session_cookie.unwrap();
|
||||||
|
|
||||||
|
tracing::debug!(
|
||||||
|
"UserFromSession: got session cookie from user agent, {}={}",
|
||||||
|
COOKIE_NAME,
|
||||||
|
session_cookie
|
||||||
|
);
|
||||||
|
// continue to decode the session cookie
|
||||||
|
let user =
|
||||||
|
if let Some(session) = store.load_session(session_cookie.to_owned()).await.unwrap() {
|
||||||
|
if let Some(user) = session.get::<User>("user") {
|
||||||
|
tracing::debug!(
|
||||||
|
"UserFromSession: session decoded success, user_id={:?}",
|
||||||
|
user.id
|
||||||
|
);
|
||||||
|
user
|
||||||
|
} else {
|
||||||
|
return Err((
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"No `user_id` found in session",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tracing::debug!(
|
||||||
|
"UserIdFromSession: err session not exists in store, {}={}",
|
||||||
|
COOKIE_NAME,
|
||||||
|
session_cookie
|
||||||
|
);
|
||||||
|
return Err((StatusCode::BAD_REQUEST, "No session found for cookie"));
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(UserFromSession { user })
|
||||||
|
}
|
||||||
|
}
|
48
como_api/src/controllers/graphql.rs
Normal file
48
como_api/src/controllers/graphql.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use super::auth::UserFromSession;
|
||||||
|
use crate::router::AppState;
|
||||||
|
|
||||||
|
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
|
||||||
|
use async_graphql::{EmptySubscription, Schema};
|
||||||
|
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
|
||||||
|
use axum::response::Html;
|
||||||
|
use axum::{http::StatusCode, response::IntoResponse, routing::get, Extension, Router};
|
||||||
|
|
||||||
|
use como_domain::user::ContextUserExt;
|
||||||
|
use como_domain::Context;
|
||||||
|
use como_gql::graphql::{ComoSchema, MutationRoot, QueryRoot};
|
||||||
|
use como_infrastructure::register::ServiceRegister;
|
||||||
|
use tower::ServiceBuilder;
|
||||||
|
|
||||||
|
pub struct GraphQLController;
|
||||||
|
|
||||||
|
impl GraphQLController {
|
||||||
|
pub fn new_router(service_register: ServiceRegister, state: AppState) -> Router {
|
||||||
|
let schema = Schema::build(QueryRoot, MutationRoot, EmptySubscription)
|
||||||
|
.data(service_register)
|
||||||
|
.finish();
|
||||||
|
|
||||||
|
Router::new()
|
||||||
|
.route("/", get(graphql_playground).post(graphql_handler))
|
||||||
|
.layer(ServiceBuilder::new().layer(Extension(schema)))
|
||||||
|
.with_state(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn graphql_handler(
|
||||||
|
user: UserFromSession,
|
||||||
|
schema: Extension<ComoSchema>,
|
||||||
|
req: GraphQLRequest,
|
||||||
|
) -> Result<GraphQLResponse, StatusCode> {
|
||||||
|
let req = req.into_inner();
|
||||||
|
let req = req.data(user.user.clone());
|
||||||
|
|
||||||
|
let context = Context::new();
|
||||||
|
let context = context.set_user_id(user.user.id.clone());
|
||||||
|
let req = req.data(context);
|
||||||
|
|
||||||
|
Ok(schema.execute(req).await.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn graphql_playground() -> impl IntoResponse {
|
||||||
|
Html(playground_source(GraphQLPlaygroundConfig::new("/graphql")))
|
||||||
|
}
|
2
como_api/src/controllers/mod.rs
Normal file
2
como_api/src/controllers/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod auth;
|
||||||
|
pub mod graphql;
|
3
como_api/src/lib.rs
Normal file
3
como_api/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
mod controllers;
|
||||||
|
pub mod router;
|
||||||
|
pub mod zitadel;
|
103
como_api/src/router.rs
Normal file
103
como_api/src/router.rs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
use std::env;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
use async_sqlx_session::PostgresSessionStore;
|
||||||
|
use axum::extract::FromRef;
|
||||||
|
use axum::http::{HeaderValue, Method};
|
||||||
|
use axum::Router;
|
||||||
|
use como_infrastructure::register::ServiceRegister;
|
||||||
|
use oauth2::basic::BasicClient;
|
||||||
|
use tower::ServiceBuilder;
|
||||||
|
use tower_http::{cors::CorsLayer, trace::TraceLayer};
|
||||||
|
|
||||||
|
use crate::controllers::auth::AuthController;
|
||||||
|
use crate::controllers::graphql::GraphQLController;
|
||||||
|
use crate::zitadel::client::oauth_client;
|
||||||
|
use crate::zitadel::{IntrospectionState, IntrospectionStateBuilder};
|
||||||
|
|
||||||
|
pub struct Api;
|
||||||
|
|
||||||
|
impl Api {
|
||||||
|
pub async fn new(
|
||||||
|
port: u32,
|
||||||
|
cors_origin: &str,
|
||||||
|
service_register: ServiceRegister,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID!");
|
||||||
|
let client_secret = env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET!");
|
||||||
|
let zitadel_url = env::var("ZITADEL_URL").expect("missing ZITADEL_URL");
|
||||||
|
|
||||||
|
let is = IntrospectionStateBuilder::new(&zitadel_url)
|
||||||
|
.with_basic_auth(&client_id, &client_secret)
|
||||||
|
.build()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let oauth_client = oauth_client();
|
||||||
|
let app_state = AppState {
|
||||||
|
oauth_client,
|
||||||
|
store: service_register.session_store.clone(),
|
||||||
|
introspection_state: is,
|
||||||
|
};
|
||||||
|
|
||||||
|
let router = Router::new()
|
||||||
|
.nest(
|
||||||
|
"/auth",
|
||||||
|
AuthController::new_router(service_register.clone(), app_state.clone()).await?,
|
||||||
|
)
|
||||||
|
.nest(
|
||||||
|
"/graphql",
|
||||||
|
GraphQLController::new_router(service_register.clone(), app_state.clone()),
|
||||||
|
)
|
||||||
|
.layer(
|
||||||
|
ServiceBuilder::new()
|
||||||
|
.layer(TraceLayer::new_for_http())
|
||||||
|
.layer(
|
||||||
|
CorsLayer::new()
|
||||||
|
.allow_origin(
|
||||||
|
cors_origin
|
||||||
|
.parse::<HeaderValue>()
|
||||||
|
.context("could not parse cors origin as header")?,
|
||||||
|
)
|
||||||
|
.allow_headers([axum::http::header::CONTENT_TYPE])
|
||||||
|
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
|
||||||
|
.allow_credentials(true),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
let host = env::var("HOST").unwrap_or("0.0.0.0".to_string());
|
||||||
|
|
||||||
|
tracing::info!("running on: {host}:{}", port);
|
||||||
|
|
||||||
|
axum::Server::bind(&format!("{host}:{}", port).parse().unwrap())
|
||||||
|
.serve(router.into_make_service())
|
||||||
|
.await
|
||||||
|
.context("error while starting API")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct AppState {
|
||||||
|
oauth_client: BasicClient,
|
||||||
|
introspection_state: IntrospectionState,
|
||||||
|
store: PostgresSessionStore,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRef<AppState> for BasicClient {
|
||||||
|
fn from_ref(state: &AppState) -> Self {
|
||||||
|
state.oauth_client.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRef<AppState> for PostgresSessionStore {
|
||||||
|
fn from_ref(state: &AppState) -> Self {
|
||||||
|
state.store.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRef<AppState> for IntrospectionState {
|
||||||
|
fn from_ref(input: &AppState) -> Self {
|
||||||
|
input.introspection_state.clone()
|
||||||
|
}
|
||||||
|
}
|
20
como_api/src/zitadel/client.rs
Normal file
20
como_api/src/zitadel/client.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl};
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
pub fn oauth_client() -> BasicClient {
|
||||||
|
let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID!");
|
||||||
|
let client_secret = env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET!");
|
||||||
|
let redirect_url = env::var("REDIRECT_URL").expect("missing REDIRECT_URL");
|
||||||
|
|
||||||
|
let auth_url = env::var("AUTH_URL").expect("missing AUTH_URL");
|
||||||
|
|
||||||
|
let token_url = env::var("TOKEN_URL").expect("missing TOKEN_URL");
|
||||||
|
|
||||||
|
BasicClient::new(
|
||||||
|
ClientId::new(client_id),
|
||||||
|
Some(ClientSecret::new(client_secret)),
|
||||||
|
AuthUrl::new(auth_url).unwrap(),
|
||||||
|
Some(TokenUrl::new(token_url).unwrap()),
|
||||||
|
)
|
||||||
|
.set_redirect_uri(RedirectUrl::new(redirect_url).unwrap())
|
||||||
|
}
|
90
como_api/src/zitadel/mod.rs
Normal file
90
como_api/src/zitadel/mod.rs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
pub mod client;
|
||||||
|
|
||||||
|
use axum::extract::FromRef;
|
||||||
|
use openidconnect::IntrospectionUrl;
|
||||||
|
use zitadel::{
|
||||||
|
axum::introspection::IntrospectionStateBuilderError,
|
||||||
|
credentials::Application,
|
||||||
|
oidc::{discovery::discover, introspection::AuthorityAuthentication},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct IntrospectionState {
|
||||||
|
pub(crate) config: IntrospectionConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configuration that must be inject into the axum application state. Used by the
|
||||||
|
/// [IntrospectionStateBuilder](super::IntrospectionStateBuilder). This struct is also used to create the [IntrospectionState](IntrospectionState)
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct IntrospectionConfig {
|
||||||
|
pub(crate) authority: String,
|
||||||
|
pub(crate) authentication: AuthorityAuthentication,
|
||||||
|
pub(crate) introspection_uri: IntrospectionUrl,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FromRef<IntrospectionState> for IntrospectionConfig {
|
||||||
|
fn from_ref(input: &IntrospectionState) -> Self {
|
||||||
|
input.config.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IntrospectionStateBuilder {
|
||||||
|
authority: String,
|
||||||
|
authentication: Option<AuthorityAuthentication>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Builder for [IntrospectionConfig]
|
||||||
|
impl IntrospectionStateBuilder {
|
||||||
|
pub fn new(authority: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
authority: authority.to_string(),
|
||||||
|
authentication: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_basic_auth(
|
||||||
|
&mut self,
|
||||||
|
client_id: &str,
|
||||||
|
client_secret: &str,
|
||||||
|
) -> &mut IntrospectionStateBuilder {
|
||||||
|
self.authentication = Some(AuthorityAuthentication::Basic {
|
||||||
|
client_id: client_id.to_string(),
|
||||||
|
client_secret: client_secret.to_string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_jwt_profile(&mut self, application: Application) -> &mut IntrospectionStateBuilder {
|
||||||
|
self.authentication = Some(AuthorityAuthentication::JWTProfile { application });
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn build(&mut self) -> Result<IntrospectionState, IntrospectionStateBuilderError> {
|
||||||
|
if self.authentication.is_none() {
|
||||||
|
return Err(IntrospectionStateBuilderError::NoAuthSchema);
|
||||||
|
}
|
||||||
|
|
||||||
|
let metadata = discover(&self.authority)
|
||||||
|
.await
|
||||||
|
.map_err(|source| IntrospectionStateBuilderError::Discovery { source })?;
|
||||||
|
|
||||||
|
let introspection_uri = metadata
|
||||||
|
.additional_metadata()
|
||||||
|
.introspection_endpoint
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
if introspection_uri.is_none() {
|
||||||
|
return Err(IntrospectionStateBuilderError::NoIntrospectionUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(IntrospectionState {
|
||||||
|
config: IntrospectionConfig {
|
||||||
|
authority: self.authority.clone(),
|
||||||
|
introspection_uri: introspection_uri.unwrap(),
|
||||||
|
authentication: self.authentication.as_ref().unwrap().clone(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
23
como_bin/Cargo.toml
Normal file
23
como_bin/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
[package]
|
||||||
|
name = "como_bin"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
como_gql.workspace = true
|
||||||
|
como_core.workspace = true
|
||||||
|
como_domain.workspace = true
|
||||||
|
como_infrastructure.workspace = true
|
||||||
|
como_api.workspace = true
|
||||||
|
|
||||||
|
|
||||||
|
axum.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
tokio.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
dotenv.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
tracing-subscriber.workspace = true
|
||||||
|
clap.workspace = true
|
21
como_bin/src/error.rs
Normal file
21
como_bin/src/error.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use axum::{http::StatusCode, response::IntoResponse, Json};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum AppError {
|
||||||
|
WrongCredentials,
|
||||||
|
InternalServerError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for AppError {
|
||||||
|
fn into_response(self) -> axum::response::Response {
|
||||||
|
let (status, err_msg) = match self {
|
||||||
|
Self::WrongCredentials => (StatusCode::BAD_REQUEST, "invalid credentials"),
|
||||||
|
Self::InternalServerError => (
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"something went wrong with your request",
|
||||||
|
),
|
||||||
|
};
|
||||||
|
(status, Json(json!({ "error": err_msg }))).into_response()
|
||||||
|
}
|
||||||
|
}
|
41
como_bin/src/main.rs
Normal file
41
como_bin/src/main.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
|
|
||||||
|
use como_api::router::Api;
|
||||||
|
use como_infrastructure::{
|
||||||
|
configs::AppConfig, database::ConnectionPoolManager, register::ServiceRegister,
|
||||||
|
};
|
||||||
|
|
||||||
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
tracing::info!("Loading dotenv");
|
||||||
|
dotenv::dotenv()?;
|
||||||
|
|
||||||
|
let config = Arc::new(AppConfig::parse());
|
||||||
|
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(tracing_subscriber::EnvFilter::new(&config.rust_log))
|
||||||
|
.with(tracing_subscriber::fmt::layer())
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let pool = ConnectionPoolManager::new_pool(&config.database_url, true).await?;
|
||||||
|
|
||||||
|
let service_register = ServiceRegister::new(pool, config.clone()).await?;
|
||||||
|
|
||||||
|
Api::new(
|
||||||
|
config.api_port,
|
||||||
|
&config.cors_origin,
|
||||||
|
service_register.clone(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.context("could not initialize API")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
12
como_core/Cargo.toml
Normal file
12
como_core/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "como_core"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
como_domain.workspace = true
|
||||||
|
|
||||||
|
async-trait.workspace = true
|
||||||
|
anyhow.workspace = true
|
5
como_core/report.json
Normal file
5
como_core/report.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"version": 0,
|
||||||
|
"root_name": "Workspace",
|
||||||
|
"workspace_crates": {}
|
||||||
|
}
|
31
como_core/src/items/mod.rs
Normal file
31
como_core/src/items/mod.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use como_domain::{
|
||||||
|
item::{
|
||||||
|
queries::{GetItemQuery, GetItemsQuery},
|
||||||
|
requests::{CreateItemDto, UpdateItemDto},
|
||||||
|
responses::CreatedItemDto,
|
||||||
|
ItemDto,
|
||||||
|
},
|
||||||
|
Context,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type DynItemService = Arc<dyn ItemService + Send + Sync>;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait ItemService {
|
||||||
|
async fn add_item(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
item: CreateItemDto,
|
||||||
|
) -> anyhow::Result<CreatedItemDto>;
|
||||||
|
async fn get_item(&self, context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto>;
|
||||||
|
async fn get_items(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<ItemDto>>;
|
||||||
|
|
||||||
|
async fn update_item(&self, context: &Context, item: UpdateItemDto) -> anyhow::Result<ItemDto>;
|
||||||
|
}
|
3
como_core/src/lib.rs
Normal file
3
como_core/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod items;
|
||||||
|
pub mod projects;
|
||||||
|
pub mod users;
|
24
como_core/src/projects/mod.rs
Normal file
24
como_core/src/projects/mod.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use como_domain::{
|
||||||
|
projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto},
|
||||||
|
Context,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type DynProjectService = Arc<dyn ProjectService + Send + Sync>;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait ProjectService {
|
||||||
|
async fn get_project(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<ProjectDto>;
|
||||||
|
async fn get_projects(&self, context: &Context) -> anyhow::Result<Vec<ProjectDto>>;
|
||||||
|
async fn create_project(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
name: CreateProjectMutation,
|
||||||
|
) -> anyhow::Result<ProjectDto>;
|
||||||
|
}
|
22
como_core/src/users/mod.rs
Normal file
22
como_core/src/users/mod.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use como_domain::Context;
|
||||||
|
|
||||||
|
pub type DynUserService = Arc<dyn UserService + Send + Sync>;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait UserService {
|
||||||
|
async fn add_user(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
) -> anyhow::Result<String>;
|
||||||
|
async fn validate_user(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
) -> anyhow::Result<Option<String>>;
|
||||||
|
}
|
12
como_domain/Cargo.toml
Normal file
12
como_domain/Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "como_domain"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
async-graphql.workspace = true
|
||||||
|
serde.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
uuid.workspace = true
|
37
como_domain/src/common/mod.rs
Normal file
37
como_domain/src/common/mod.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
pub mod user;
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Context {
|
||||||
|
values: BTreeMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Context {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
values: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_value(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
||||||
|
let mut values = self.values.clone();
|
||||||
|
|
||||||
|
let _ = values.insert(key.into(), value.into());
|
||||||
|
|
||||||
|
Self { values }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_value_mut(
|
||||||
|
&mut self,
|
||||||
|
key: impl Into<String>,
|
||||||
|
value: impl Into<String>,
|
||||||
|
) -> &mut Self {
|
||||||
|
self.values.insert(key.into(), value.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, key: impl AsRef<str>) -> Option<&str> {
|
||||||
|
self.values.get(key.as_ref()).map(|s| s.as_str())
|
||||||
|
}
|
||||||
|
}
|
23
como_domain/src/common/user.rs
Normal file
23
como_domain/src/common/user.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::Context;
|
||||||
|
|
||||||
|
pub trait ContextUserExt {
|
||||||
|
fn set_user_id(&self, user_id: impl Into<String>) -> Context;
|
||||||
|
fn set_user_id_mut(&mut self, user_id: impl Into<String>) -> &mut Context;
|
||||||
|
fn get_user_id(&self) -> Option<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const USER_ID_KEY: &str = "user_id";
|
||||||
|
|
||||||
|
impl ContextUserExt for Context {
|
||||||
|
fn set_user_id(&self, user_id: impl Into<String>) -> Context {
|
||||||
|
self.with_value(USER_ID_KEY, user_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_user_id_mut(&mut self, user_id: impl Into<String>) -> &mut Context {
|
||||||
|
self.with_value_mut(USER_ID_KEY, user_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_user_id(&self) -> Option<String> {
|
||||||
|
self.get(USER_ID_KEY).map(|s| s.to_string())
|
||||||
|
}
|
||||||
|
}
|
24
como_domain/src/item/mod.rs
Normal file
24
como_domain/src/item/mod.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
pub mod queries;
|
||||||
|
pub mod requests;
|
||||||
|
pub mod responses;
|
||||||
|
|
||||||
|
use async_graphql::{Enum, InputObject};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Enum, Copy)]
|
||||||
|
pub enum ItemState {
|
||||||
|
Created,
|
||||||
|
Done,
|
||||||
|
Archived,
|
||||||
|
Deleted,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct ItemDto {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub title: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub state: ItemState,
|
||||||
|
pub project_id: Uuid,
|
||||||
|
}
|
13
como_domain/src/item/queries.rs
Normal file
13
como_domain/src/item/queries.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use async_graphql::InputObject;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct GetItemQuery {
|
||||||
|
pub item_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct GetItemsQuery {
|
||||||
|
pub project_id: Uuid,
|
||||||
|
}
|
21
como_domain/src/item/requests.rs
Normal file
21
como_domain/src/item/requests.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use async_graphql::InputObject;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::ItemState;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct CreateItemDto {
|
||||||
|
pub title: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub project_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct UpdateItemDto {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub title: Option<String>,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub state: Option<ItemState>,
|
||||||
|
pub project_id: Option<Uuid>,
|
||||||
|
}
|
3
como_domain/src/item/responses.rs
Normal file
3
como_domain/src/item/responses.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
use super::ItemDto;
|
||||||
|
|
||||||
|
pub type CreatedItemDto = ItemDto;
|
6
como_domain/src/lib.rs
Normal file
6
como_domain/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
pub mod common;
|
||||||
|
pub mod item;
|
||||||
|
pub mod projects;
|
||||||
|
pub mod users;
|
||||||
|
|
||||||
|
pub use common::*;
|
17
como_domain/src/projects/mod.rs
Normal file
17
como_domain/src/projects/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
pub mod mutation;
|
||||||
|
pub mod queries;
|
||||||
|
pub mod requests;
|
||||||
|
pub mod responses;
|
||||||
|
|
||||||
|
use async_graphql::{InputObject, SimpleObject};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject, SimpleObject)]
|
||||||
|
pub struct ProjectDto {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
|
||||||
|
pub user_id: String,
|
||||||
|
}
|
8
como_domain/src/projects/mutation.rs
Normal file
8
como_domain/src/projects/mutation.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use async_graphql::InputObject;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct CreateProjectMutation {
|
||||||
|
pub name: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
}
|
8
como_domain/src/projects/queries.rs
Normal file
8
como_domain/src/projects/queries.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use async_graphql::InputObject;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, InputObject)]
|
||||||
|
pub struct GetProjectQuery {
|
||||||
|
pub project_id: Uuid,
|
||||||
|
}
|
6
como_domain/src/projects/requests.rs
Normal file
6
como_domain/src/projects/requests.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct CreateProjectDto {
|
||||||
|
pub name: String,
|
||||||
|
}
|
3
como_domain/src/projects/responses.rs
Normal file
3
como_domain/src/projects/responses.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
use super::ProjectDto;
|
||||||
|
|
||||||
|
pub type CreatedProjectDto = ProjectDto;
|
17
como_domain/src/users/mod.rs
Normal file
17
como_domain/src/users/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
pub mod requests;
|
||||||
|
pub mod responses;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct UserDto {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub username: String,
|
||||||
|
pub email: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: String,
|
||||||
|
}
|
8
como_domain/src/users/requests.rs
Normal file
8
como_domain/src/users/requests.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct CreateUserDto {
|
||||||
|
pub username: String,
|
||||||
|
pub email: String,
|
||||||
|
pub password: String,
|
||||||
|
}
|
3
como_domain/src/users/responses.rs
Normal file
3
como_domain/src/users/responses.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
use super::UserDto;
|
||||||
|
|
||||||
|
pub type UserCreatedDto = UserDto;
|
16
como_gql/Cargo.toml
Normal file
16
como_gql/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "como_gql"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
como_core.workspace = true
|
||||||
|
como_domain.workspace = true
|
||||||
|
como_infrastructure.workspace = true
|
||||||
|
|
||||||
|
anyhow.workspace = true
|
||||||
|
async-trait.workspace = true
|
||||||
|
async-graphql.workspace = true
|
||||||
|
uuid.workspace = true
|
94
como_gql/src/graphql.rs
Normal file
94
como_gql/src/graphql.rs
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
use crate::common::*;
|
||||||
|
use crate::items::{CreatedItem, Item};
|
||||||
|
use crate::projects::Project;
|
||||||
|
use async_graphql::{Context, EmptySubscription, Object, Schema};
|
||||||
|
use como_domain::item::queries::{GetItemQuery, GetItemsQuery};
|
||||||
|
use como_domain::item::requests::{CreateItemDto, UpdateItemDto};
|
||||||
|
use como_domain::projects::mutation::CreateProjectMutation;
|
||||||
|
use como_domain::projects::queries::GetProjectQuery;
|
||||||
|
use como_domain::projects::ProjectDto;
|
||||||
|
|
||||||
|
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
|
||||||
|
|
||||||
|
pub struct MutationRoot;
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl MutationRoot {
|
||||||
|
async fn create_item(
|
||||||
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
item: CreateItemDto,
|
||||||
|
) -> anyhow::Result<CreatedItem> {
|
||||||
|
let created_item = item_service(ctx)
|
||||||
|
.add_item(get_domain_context(ctx), item)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(CreatedItem {
|
||||||
|
id: created_item.id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_project(
|
||||||
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
request: CreateProjectMutation,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let project = project_service(ctx)
|
||||||
|
.create_project(get_domain_context(ctx), request)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(project)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_item(&self, ctx: &Context<'_>, item: UpdateItemDto) -> anyhow::Result<Item> {
|
||||||
|
let updated_item = item_service(ctx)
|
||||||
|
.update_item(get_domain_context(ctx), item)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(updated_item.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct QueryRoot;
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl QueryRoot {
|
||||||
|
async fn get_item(&self, ctx: &Context<'_>, query: GetItemQuery) -> anyhow::Result<Item> {
|
||||||
|
let item = item_service(ctx)
|
||||||
|
.get_item(get_domain_context(ctx), query)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(Item::from(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_items(
|
||||||
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<Item>> {
|
||||||
|
let items = item_service(ctx)
|
||||||
|
.get_items(get_domain_context(ctx), query)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(items.iter().map(|i| Item::from(i.clone())).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Projects
|
||||||
|
async fn get_project(
|
||||||
|
&self,
|
||||||
|
ctx: &Context<'_>,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<Project> {
|
||||||
|
project_service(ctx)
|
||||||
|
.get_project(get_domain_context(ctx), query)
|
||||||
|
.await
|
||||||
|
.map(|p| p.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_projects(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Project>> {
|
||||||
|
project_service(ctx)
|
||||||
|
.get_projects(get_domain_context(ctx))
|
||||||
|
.await
|
||||||
|
.map(|p| p.into_iter().map(|p| p.into()).collect())
|
||||||
|
}
|
||||||
|
}
|
80
como_gql/src/items.rs
Normal file
80
como_gql/src/items.rs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
use crate::common::*;
|
||||||
|
use async_graphql::{Context, Object};
|
||||||
|
use como_domain::{
|
||||||
|
item::{queries::GetItemQuery, ItemDto, ItemState},
|
||||||
|
projects::queries::GetProjectQuery,
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::projects::Project;
|
||||||
|
|
||||||
|
pub struct CreatedItem {
|
||||||
|
pub id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl CreatedItem {
|
||||||
|
pub async fn item(&self, ctx: &Context<'_>) -> anyhow::Result<Item> {
|
||||||
|
let item = item_service(ctx)
|
||||||
|
.get_item(get_domain_context(ctx), GetItemQuery { item_id: self.id })
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(item.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Item {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub title: String,
|
||||||
|
pub description: Option<String>,
|
||||||
|
pub state: ItemState,
|
||||||
|
pub project_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl Item {
|
||||||
|
pub async fn id(&self, _ctx: &Context<'_>) -> anyhow::Result<Uuid> {
|
||||||
|
return Ok(self.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn title(&self, _ctx: &Context<'_>) -> anyhow::Result<String> {
|
||||||
|
return Ok(self.title.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn description(&self, _ctx: &Context<'_>) -> anyhow::Result<Option<String>> {
|
||||||
|
return Ok(self.description.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn state(&self, _ctx: &Context<'_>) -> anyhow::Result<ItemState> {
|
||||||
|
return Ok(self.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn project(&self, ctx: &Context<'_>) -> anyhow::Result<Project> {
|
||||||
|
let project = project_service(ctx)
|
||||||
|
.get_project(
|
||||||
|
get_domain_context(ctx),
|
||||||
|
GetProjectQuery {
|
||||||
|
project_id: self.project_id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(project.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn project_id(&self, _ctx: &Context<'_>) -> anyhow::Result<Uuid> {
|
||||||
|
return Ok(self.project_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ItemDto> for Item {
|
||||||
|
fn from(dto: ItemDto) -> Self {
|
||||||
|
Self {
|
||||||
|
id: dto.id,
|
||||||
|
title: dto.title,
|
||||||
|
description: dto.description,
|
||||||
|
state: dto.state,
|
||||||
|
project_id: dto.project_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
como_gql/src/lib.rs
Normal file
33
como_gql/src/lib.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
pub mod graphql;
|
||||||
|
mod items;
|
||||||
|
mod projects;
|
||||||
|
|
||||||
|
pub mod common {
|
||||||
|
use async_graphql::Context;
|
||||||
|
use como_core::items::DynItemService;
|
||||||
|
use como_core::projects::DynProjectService;
|
||||||
|
use como_infrastructure::register::ServiceRegister;
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn get_domain_context<'a>(ctx: &Context<'a>) -> &'a como_domain::Context {
|
||||||
|
ctx.data_unchecked::<como_domain::Context>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn get_service_register<'a>(ctx: &Context<'a>) -> &'a ServiceRegister {
|
||||||
|
ctx.data_unchecked::<ServiceRegister>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn project_service<'a>(ctx: &Context<'a>) -> DynProjectService {
|
||||||
|
ctx.data_unchecked::<ServiceRegister>()
|
||||||
|
.project_service
|
||||||
|
.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn item_service<'a>(ctx: &Context<'a>) -> DynItemService {
|
||||||
|
ctx.data_unchecked::<ServiceRegister>().item_service.clone()
|
||||||
|
}
|
||||||
|
}
|
48
como_gql/src/projects.rs
Normal file
48
como_gql/src/projects.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use crate::common::*;
|
||||||
|
use async_graphql::{Context, Object};
|
||||||
|
use como_domain::projects::ProjectDto;
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::items::Item;
|
||||||
|
|
||||||
|
pub struct Project {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Object]
|
||||||
|
impl Project {
|
||||||
|
async fn id(&self) -> &Uuid {
|
||||||
|
&self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn name(&self) -> &String {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn items(&self, ctx: &Context<'_>) -> anyhow::Result<Vec<Item>> {
|
||||||
|
let items = item_service(ctx)
|
||||||
|
.get_items(
|
||||||
|
get_domain_context(ctx),
|
||||||
|
como_domain::item::queries::GetItemsQuery {
|
||||||
|
project_id: self.id,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
.iter()
|
||||||
|
.map(|i| Item::from(i.clone()))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
Ok(items)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ProjectDto> for Project {
|
||||||
|
fn from(dto: ProjectDto) -> Self {
|
||||||
|
Self {
|
||||||
|
id: dto.id,
|
||||||
|
name: dto.name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
0
como_infrastructure/.sqlx/.gitkeep
Normal file
0
como_infrastructure/.sqlx/.gitkeep
Normal file
27
como_infrastructure/Cargo.toml
Normal file
27
como_infrastructure/Cargo.toml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
[package]
|
||||||
|
name = "como_infrastructure"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
como_core.workspace = true
|
||||||
|
como_domain.workspace = true
|
||||||
|
|
||||||
|
|
||||||
|
axum.workspace = true
|
||||||
|
async-trait.workspace = true
|
||||||
|
uuid.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
sqlx.workspace = true
|
||||||
|
chrono.workspace = true
|
||||||
|
serde_json.workspace = true
|
||||||
|
|
||||||
|
async-sqlx-session.workspace = true
|
||||||
|
|
||||||
|
tokio.workspace = true
|
||||||
|
clap.workspace = true
|
||||||
|
tracing.workspace = true
|
||||||
|
argon2.workspace = true
|
||||||
|
rand_core.workspace = true
|
9
como_infrastructure/build.rs
Normal file
9
como_infrastructure/build.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("cargo:rustc-env=SQLX_OFFLINE_DIR='./.sqlx'");
|
||||||
|
// When building in docs.rs, we want to set SQLX_OFFLINE mode to true
|
||||||
|
if std::env::var_os("DOCS_RS").is_some() {
|
||||||
|
println!("cargo:rustc-env=SQLX_OFFLINE=true");
|
||||||
|
} else if std::env::var_os("DOCKER_BUILD").is_some() {
|
||||||
|
println!("cargo:rustc-env=SQLX_OFFLINE=true");
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
username varchar not null,
|
||||||
|
password_hash varchar not null
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE unique index users_username_idx on users(username)
|
@@ -0,0 +1,10 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS projects (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
name varchar not null,
|
||||||
|
description varchar default null,
|
||||||
|
user_id varchar not null,
|
||||||
|
created_at timestamp not null,
|
||||||
|
updated_at timestamp not null
|
||||||
|
);
|
17
como_infrastructure/migrations/20230603152353_items.sql
Normal file
17
como_infrastructure/migrations/20230603152353_items.sql
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
create table if not exists items (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
title varchar not null,
|
||||||
|
description varchar default null,
|
||||||
|
state integer not null,
|
||||||
|
user_id varchar not null,
|
||||||
|
project_id UUID not null,
|
||||||
|
created_at timestamp not null,
|
||||||
|
updated_at timestamp not null,
|
||||||
|
CONSTRAINT fk_project
|
||||||
|
FOREIGN KEY(project_id)
|
||||||
|
REFERENCES projects(id)
|
||||||
|
ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
|
@@ -0,0 +1,4 @@
|
|||||||
|
-- Add migration script here
|
||||||
|
|
||||||
|
ALTER TABLE items ALTER COLUMN state TYPE varchar(255);
|
||||||
|
|
346
como_infrastructure/sqlx-data.json
Normal file
346
como_infrastructure/sqlx-data.json
Normal file
@@ -0,0 +1,346 @@
|
|||||||
|
{
|
||||||
|
"db": "PostgreSQL",
|
||||||
|
"05d0a7901f0481d7443f125655df26eeacd63f2b023723a0c09c662617e0baf5": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "title",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "description",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "project_id",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT id, title, description, state, project_id\n FROM items\n WHERE id = $1 AND user_id = $2\n "
|
||||||
|
},
|
||||||
|
"3b4484c5ccfd4dcb887c4e978fe6e45d4c9ecc2a73909be207dced79ddf17d87": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Varchar",
|
||||||
|
"Varchar"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n INSERT INTO users (username, password_hash) \n VALUES ( $1, $2 ) \n RETURNING id\n "
|
||||||
|
},
|
||||||
|
"4ec32ebd0ee991cec625d9de51de0d3e0ddfc8afda0568327fa9c818bde08e1f": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Timestamp",
|
||||||
|
"Timestamp"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n INSERT INTO projects (id, name, description, user_id, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6)\n RETURNING id\n "
|
||||||
|
},
|
||||||
|
"7901e81b1f1f08f0c7e72a967a8116efb62f40d99f80900f1e56cd13ad4f6bb2": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "title",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "description",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "project_id",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Uuid",
|
||||||
|
"Varchar"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n INSERT INTO items (id, title, description, state, project_id, user_id, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, now(), now())\n RETURNING id, title, description, state, project_id\n "
|
||||||
|
},
|
||||||
|
"a188dc748025cf3311820d16002b111a75f571d18f44f54b730ac14e9b2e10ea": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "name",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "description",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "user_id",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT id, name, description, user_id\n FROM projects\n WHERE id = $1 and user_id = $2\n "
|
||||||
|
},
|
||||||
|
"b930a7123d22d543e4d8ed70a1bc10477362127969ceca9653e445f26670003a": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "name",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "description",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "user_id",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT id, name, description, user_id\n FROM projects\n WHERE user_id = $1\n LIMIT 500\n "
|
||||||
|
},
|
||||||
|
"bacf3c8a2f302d50991483fa36a06965c3536c2ef3837c19c6e6361eff312848": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "title",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "description",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "project_id",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n UPDATE items\n SET \n title = COALESCE($1, title), \n description = COALESCE($2, description), \n state = COALESCE($3, state), \n project_id = COALESCE($4, project_id), \n updated_at = now()\n WHERE id = $5 AND user_id = $6\n RETURNING id, title, description, state, project_id\n "
|
||||||
|
},
|
||||||
|
"bd2407ffb9637afcff3ffe1101e7c1920b8cf0be423ab0313d14acc9c76e0f93": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "title",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "description",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"ordinal": 3,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "project_id",
|
||||||
|
"ordinal": 4,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text",
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT id, title, description, state, project_id\n FROM items\n WHERE user_id = $1 and project_id = $2\n LIMIT 500\n "
|
||||||
|
},
|
||||||
|
"d3f222cf6c3d9816705426fdbed3b13cb575bb432eb1f33676c0b414e67aecaf": {
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"ordinal": 0,
|
||||||
|
"type_info": "Uuid"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "username",
|
||||||
|
"ordinal": 1,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "password_hash",
|
||||||
|
"ordinal": 2,
|
||||||
|
"type_info": "Varchar"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"query": "\n SELECT * from users\n where username=$1\n "
|
||||||
|
}
|
||||||
|
}
|
27
como_infrastructure/src/configs/mod.rs
Normal file
27
como_infrastructure/src/configs/mod.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
use clap::ValueEnum;
|
||||||
|
|
||||||
|
#[derive(clap::Parser)]
|
||||||
|
pub struct AppConfig {
|
||||||
|
#[clap(long, env)]
|
||||||
|
pub database_url: String,
|
||||||
|
#[clap(long, env, default_value = "postgres")]
|
||||||
|
pub database_type: DatabaseType,
|
||||||
|
#[clap(long, env)]
|
||||||
|
pub rust_log: String,
|
||||||
|
#[clap(long, env)]
|
||||||
|
pub token_secret: String,
|
||||||
|
#[clap(long, env, default_value = "3001")]
|
||||||
|
pub api_port: u32,
|
||||||
|
#[clap(long, env, default_value = "true")]
|
||||||
|
pub run_migrations: bool,
|
||||||
|
#[clap(long, env, default_value = "false")]
|
||||||
|
pub seed: bool,
|
||||||
|
#[clap(long, env)]
|
||||||
|
pub cors_origin: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, ValueEnum)]
|
||||||
|
pub enum DatabaseType {
|
||||||
|
Postgres,
|
||||||
|
InMemory,
|
||||||
|
}
|
33
como_infrastructure/src/database/mod.rs
Normal file
33
como_infrastructure/src/database/mod.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use anyhow::Context;
|
||||||
|
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
|
||||||
|
use tracing::log::info;
|
||||||
|
|
||||||
|
pub type ConnectionPool = Pool<Postgres>;
|
||||||
|
|
||||||
|
pub struct ConnectionPoolManager;
|
||||||
|
|
||||||
|
impl ConnectionPoolManager {
|
||||||
|
pub async fn new_pool(
|
||||||
|
connection_string: &str,
|
||||||
|
run_migrations: bool,
|
||||||
|
) -> anyhow::Result<ConnectionPool> {
|
||||||
|
info!("initializing the database connection pool");
|
||||||
|
let pool = PgPoolOptions::new()
|
||||||
|
.max_connections(5)
|
||||||
|
.connect(connection_string)
|
||||||
|
.await
|
||||||
|
.context("error while initializing the database connection pool")?;
|
||||||
|
|
||||||
|
if run_migrations {
|
||||||
|
info!("migrations enabled");
|
||||||
|
info!("migrating database");
|
||||||
|
|
||||||
|
sqlx::migrate!()
|
||||||
|
.run(&pool)
|
||||||
|
.await
|
||||||
|
.context("error while running database migrations")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(pool)
|
||||||
|
}
|
||||||
|
}
|
5
como_infrastructure/src/lib.rs
Normal file
5
como_infrastructure/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod configs;
|
||||||
|
pub mod database;
|
||||||
|
pub mod register;
|
||||||
|
pub mod repositories;
|
||||||
|
pub mod services;
|
67
como_infrastructure/src/register.rs
Normal file
67
como_infrastructure/src/register.rs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use async_sqlx_session::PostgresSessionStore;
|
||||||
|
use como_core::{items::DynItemService, projects::DynProjectService, users::DynUserService};
|
||||||
|
use tracing::log::info;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
configs::{AppConfig, DatabaseType},
|
||||||
|
database::ConnectionPool,
|
||||||
|
services::{
|
||||||
|
item_service::{DefaultItemService, MemoryItemService},
|
||||||
|
project_service::{DefaultProjectService, MemoryProjectService},
|
||||||
|
user_service::DefaultUserService,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ServiceRegister {
|
||||||
|
pub item_service: DynItemService,
|
||||||
|
pub project_service: DynProjectService,
|
||||||
|
pub user_service: DynUserService,
|
||||||
|
pub session_store: PostgresSessionStore,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ServiceRegister {
|
||||||
|
pub async fn new(pool: ConnectionPool, config: Arc<AppConfig>) -> anyhow::Result<Self> {
|
||||||
|
info!("creating services");
|
||||||
|
|
||||||
|
let s = match config.database_type {
|
||||||
|
DatabaseType::Postgres => {
|
||||||
|
let item_service =
|
||||||
|
Arc::new(DefaultItemService::new(pool.clone())) as DynItemService;
|
||||||
|
let project_service =
|
||||||
|
Arc::new(DefaultProjectService::new(pool.clone())) as DynProjectService;
|
||||||
|
let user_service =
|
||||||
|
Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
|
||||||
|
let store = PostgresSessionStore::new(&config.database_url).await?;
|
||||||
|
store.migrate().await?;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
item_service,
|
||||||
|
user_service,
|
||||||
|
project_service,
|
||||||
|
session_store: store,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DatabaseType::InMemory => {
|
||||||
|
let item_service = Arc::new(MemoryItemService::new()) as DynItemService;
|
||||||
|
let project_service = Arc::new(MemoryProjectService::new()) as DynProjectService;
|
||||||
|
let user_service =
|
||||||
|
Arc::new(DefaultUserService::new(pool.clone())) as DynUserService;
|
||||||
|
let store = PostgresSessionStore::new(&config.database_url).await?;
|
||||||
|
store.migrate().await?;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
item_service,
|
||||||
|
user_service,
|
||||||
|
project_service,
|
||||||
|
session_store: store,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
info!("services created succesfully");
|
||||||
|
|
||||||
|
Ok(s)
|
||||||
|
}
|
||||||
|
}
|
1
como_infrastructure/src/repositories/mod.rs
Normal file
1
como_infrastructure/src/repositories/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
217
como_infrastructure/src/services/item_service.rs
Normal file
217
como_infrastructure/src/services/item_service.rs
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
use crate::database::ConnectionPool;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use como_core::items::ItemService;
|
||||||
|
use como_domain::{
|
||||||
|
item::{
|
||||||
|
queries::{GetItemQuery, GetItemsQuery},
|
||||||
|
requests::{CreateItemDto, UpdateItemDto},
|
||||||
|
responses::CreatedItemDto,
|
||||||
|
ItemDto,
|
||||||
|
},
|
||||||
|
user::ContextUserExt,
|
||||||
|
Context,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct DefaultItemService {
|
||||||
|
pool: ConnectionPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultItemService {
|
||||||
|
pub fn new(connection_pool: ConnectionPool) -> Self {
|
||||||
|
Self {
|
||||||
|
pool: connection_pool,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ItemService for DefaultItemService {
|
||||||
|
async fn add_item(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
item: CreateItemDto,
|
||||||
|
) -> anyhow::Result<CreatedItemDto> {
|
||||||
|
let state = serde_json::to_string(&como_domain::item::ItemState::Created {})?;
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO items (id, title, description, state, project_id, user_id, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, now(), now())
|
||||||
|
RETURNING id, title, description, state, project_id
|
||||||
|
"#,
|
||||||
|
Uuid::new_v4(),
|
||||||
|
item.title,
|
||||||
|
item.description,
|
||||||
|
state,
|
||||||
|
item.project_id,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(CreatedItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: como_domain::item::ItemState::Created {},
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_item(&self, context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, description, state, project_id
|
||||||
|
FROM items
|
||||||
|
WHERE id = $1 AND user_id = $2
|
||||||
|
"#,
|
||||||
|
query.item_id,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: serde_json::from_str(&rec.state)?,
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_items(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<ItemDto>> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let recs = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, title, description, state, project_id
|
||||||
|
FROM items
|
||||||
|
WHERE user_id = $1 and project_id = $2
|
||||||
|
LIMIT 500
|
||||||
|
"#,
|
||||||
|
user_id,
|
||||||
|
query.project_id,
|
||||||
|
)
|
||||||
|
.fetch_all(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(recs
|
||||||
|
.into_iter()
|
||||||
|
.map(|rec| ItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: serde_json::from_str(&rec.state).unwrap(),
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_item(&self, context: &Context, item: UpdateItemDto) -> anyhow::Result<ItemDto> {
|
||||||
|
let state = item.state.map(|s| serde_json::to_string(&s)).transpose()?;
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
UPDATE items
|
||||||
|
SET
|
||||||
|
title = COALESCE($1, title),
|
||||||
|
description = COALESCE($2, description),
|
||||||
|
state = COALESCE($3, state),
|
||||||
|
project_id = COALESCE($4, project_id),
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $5 AND user_id = $6
|
||||||
|
RETURNING id, title, description, state, project_id
|
||||||
|
"#,
|
||||||
|
item.title,
|
||||||
|
item.description,
|
||||||
|
state,
|
||||||
|
item.project_id,
|
||||||
|
item.id,
|
||||||
|
user_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ItemDto {
|
||||||
|
id: rec.id,
|
||||||
|
title: rec.title,
|
||||||
|
description: rec.description,
|
||||||
|
state: serde_json::from_str(&rec.state)?,
|
||||||
|
project_id: rec.project_id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MemoryItemService {
|
||||||
|
item_store: Arc<Mutex<HashMap<String, ItemDto>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MemoryItemService {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
item_store: Arc::new(Mutex::new(HashMap::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ItemService for MemoryItemService {
|
||||||
|
async fn add_item(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
create_item: CreateItemDto,
|
||||||
|
) -> anyhow::Result<CreatedItemDto> {
|
||||||
|
if let Ok(mut item_store) = self.item_store.lock() {
|
||||||
|
let item = ItemDto {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
title: create_item.title,
|
||||||
|
description: create_item.description,
|
||||||
|
state: como_domain::item::ItemState::Created,
|
||||||
|
project_id: create_item.project_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
item_store.insert(item.id.to_string(), item.clone());
|
||||||
|
|
||||||
|
return Ok(item);
|
||||||
|
} else {
|
||||||
|
Err(anyhow::anyhow!("could not unlock item_store"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_item(&self, _context: &Context, query: GetItemQuery) -> anyhow::Result<ItemDto> {
|
||||||
|
if let Ok(item_store) = self.item_store.lock() {
|
||||||
|
let item = item_store
|
||||||
|
.get(&query.item_id.to_string())
|
||||||
|
.ok_or(anyhow::anyhow!("could not find item"))?;
|
||||||
|
return Ok(item.clone());
|
||||||
|
} else {
|
||||||
|
Err(anyhow::anyhow!("could not unlock item_store"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_items(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
_query: GetItemsQuery,
|
||||||
|
) -> anyhow::Result<Vec<ItemDto>> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_item(&self, _context: &Context, _item: UpdateItemDto) -> anyhow::Result<ItemDto> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
3
como_infrastructure/src/services/mod.rs
Normal file
3
como_infrastructure/src/services/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod item_service;
|
||||||
|
pub mod project_service;
|
||||||
|
pub mod user_service;
|
167
como_infrastructure/src/services/project_service.rs
Normal file
167
como_infrastructure/src/services/project_service.rs
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
|
use axum::async_trait;
|
||||||
|
use como_core::projects::ProjectService;
|
||||||
|
use como_domain::{
|
||||||
|
projects::{mutation::CreateProjectMutation, queries::GetProjectQuery, ProjectDto},
|
||||||
|
user::ContextUserExt,
|
||||||
|
Context,
|
||||||
|
};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::database::ConnectionPool;
|
||||||
|
|
||||||
|
pub struct DefaultProjectService {
|
||||||
|
pool: ConnectionPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultProjectService {
|
||||||
|
pub fn new(connection_pool: ConnectionPool) -> Self {
|
||||||
|
Self {
|
||||||
|
pool: connection_pool,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ProjectService for DefaultProjectService {
|
||||||
|
async fn get_project(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, name, description, user_id
|
||||||
|
FROM projects
|
||||||
|
WHERE id = $1 and user_id = $2
|
||||||
|
"#,
|
||||||
|
query.project_id,
|
||||||
|
&user_id
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ProjectDto {
|
||||||
|
id: rec.id,
|
||||||
|
name: rec.name,
|
||||||
|
description: rec.description,
|
||||||
|
user_id: rec.user_id,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
async fn get_projects(&self, context: &Context) -> anyhow::Result<Vec<ProjectDto>> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let recs = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT id, name, description, user_id
|
||||||
|
FROM projects
|
||||||
|
WHERE user_id = $1
|
||||||
|
LIMIT 500
|
||||||
|
"#,
|
||||||
|
&user_id
|
||||||
|
)
|
||||||
|
.fetch_all(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(recs
|
||||||
|
.into_iter()
|
||||||
|
.map(|rec| ProjectDto {
|
||||||
|
id: rec.id,
|
||||||
|
name: rec.name,
|
||||||
|
description: rec.description,
|
||||||
|
user_id: rec.user_id,
|
||||||
|
})
|
||||||
|
.collect::<_>())
|
||||||
|
}
|
||||||
|
async fn create_project(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
request: CreateProjectMutation,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO projects (id, name, description, user_id, created_at, updated_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
RETURNING id
|
||||||
|
"#,
|
||||||
|
uuid::Uuid::new_v4(),
|
||||||
|
request.name,
|
||||||
|
request.description,
|
||||||
|
&user_id,
|
||||||
|
chrono::Utc::now().naive_utc(),
|
||||||
|
chrono::Utc::now().naive_utc(),
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(ProjectDto {
|
||||||
|
id: rec.id,
|
||||||
|
name: request.name,
|
||||||
|
description: request.description,
|
||||||
|
user_id: user_id.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MemoryProjectService {
|
||||||
|
project_store: Arc<Mutex<HashMap<String, ProjectDto>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MemoryProjectService {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
project_store: Arc::new(Mutex::new(HashMap::new())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ProjectService for MemoryProjectService {
|
||||||
|
async fn get_project(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
query: GetProjectQuery,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let ps = self.project_store.lock().await;
|
||||||
|
Ok(ps
|
||||||
|
.get(&query.project_id.to_string())
|
||||||
|
.ok_or(anyhow::anyhow!("could not find project"))?
|
||||||
|
.clone())
|
||||||
|
}
|
||||||
|
async fn get_projects(&self, context: &Context) -> anyhow::Result<Vec<ProjectDto>> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
Ok(self
|
||||||
|
.project_store
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.values()
|
||||||
|
.filter(|p| p.user_id == user_id)
|
||||||
|
.cloned()
|
||||||
|
.collect::<_>())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_project(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
mutation: CreateProjectMutation,
|
||||||
|
) -> anyhow::Result<ProjectDto> {
|
||||||
|
let user_id = context.get_user_id().ok_or(anyhow::anyhow!("no user id"))?;
|
||||||
|
|
||||||
|
let mut ps = self.project_store.lock().await;
|
||||||
|
let project = ProjectDto {
|
||||||
|
id: uuid::Uuid::new_v4(),
|
||||||
|
name: mutation.name,
|
||||||
|
description: None,
|
||||||
|
user_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
ps.insert(project.id.to_string(), project.clone());
|
||||||
|
Ok(project)
|
||||||
|
}
|
||||||
|
}
|
95
como_infrastructure/src/services/user_service.rs
Normal file
95
como_infrastructure/src/services/user_service.rs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
|
||||||
|
use axum::async_trait;
|
||||||
|
use como_core::users::UserService;
|
||||||
|
use como_domain::Context;
|
||||||
|
use rand_core::OsRng;
|
||||||
|
|
||||||
|
use crate::database::ConnectionPool;
|
||||||
|
|
||||||
|
pub struct DefaultUserService {
|
||||||
|
pool: ConnectionPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultUserService {
|
||||||
|
pub fn new(pool: ConnectionPool) -> Self {
|
||||||
|
Self { pool }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_password(&self, _context: &Context, password: String) -> anyhow::Result<String> {
|
||||||
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
|
let argon2 = Argon2::default();
|
||||||
|
|
||||||
|
let password_hash = argon2
|
||||||
|
.hash_password(password.as_bytes(), &salt)
|
||||||
|
.map_err(|e| anyhow::anyhow!(e))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok(password_hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_password(
|
||||||
|
&self,
|
||||||
|
_context: &Context,
|
||||||
|
password: String,
|
||||||
|
hashed_password: String,
|
||||||
|
) -> anyhow::Result<bool> {
|
||||||
|
let argon2 = Argon2::default();
|
||||||
|
|
||||||
|
let parsed_hash = PasswordHash::new(&hashed_password).map_err(|e| anyhow::anyhow!(e))?;
|
||||||
|
match argon2.verify_password(password.as_bytes(), &parsed_hash) {
|
||||||
|
Ok(..) => Ok(true),
|
||||||
|
Err(..) => Ok(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl UserService for DefaultUserService {
|
||||||
|
async fn add_user(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
) -> anyhow::Result<String> {
|
||||||
|
let hashed_password = self.hash_password(context, password)?;
|
||||||
|
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO users (username, password_hash)
|
||||||
|
VALUES ( $1, $2 )
|
||||||
|
RETURNING id
|
||||||
|
"#,
|
||||||
|
username,
|
||||||
|
hashed_password
|
||||||
|
)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(rec.id.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn validate_user(
|
||||||
|
&self,
|
||||||
|
context: &Context,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
) -> anyhow::Result<Option<String>> {
|
||||||
|
let rec = sqlx::query!(
|
||||||
|
r#"
|
||||||
|
SELECT * from users
|
||||||
|
where username=$1
|
||||||
|
"#,
|
||||||
|
username,
|
||||||
|
)
|
||||||
|
.fetch_optional(&self.pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
match rec {
|
||||||
|
Some(user) => match self.validate_password(context, password, user.password_hash)? {
|
||||||
|
true => Ok(Some(user.id.to_string())),
|
||||||
|
false => Ok(None),
|
||||||
|
},
|
||||||
|
None => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
crates/omnia/.gitignore
vendored
1
crates/omnia/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/target
|
|
@@ -1,15 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "omnia"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
clap = { version = "4.4.11", features = ["string"] }
|
|
||||||
color-eyre = "0.6.2"
|
|
||||||
eyre = "0.6.10"
|
|
||||||
tokio = { version = "1.35.0", features = ["full"] }
|
|
||||||
tracing = { version = "0.1.40", features = ["log"] }
|
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["tracing"] }
|
|
||||||
uuid = { version = "1.6.1", features = ["v4"] }
|
|
@@ -1,94 +0,0 @@
|
|||||||
use cli::CliCommand;
|
|
||||||
use inbox::services::Inbox;
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> eyre::Result<()> {
|
|
||||||
tracing_subscriber::fmt().init();
|
|
||||||
|
|
||||||
if let Err(e) = color_eyre::install() {
|
|
||||||
tracing::error!("failed to install color_eyre: {}", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
let cmd = clap::Command::new("omnia");
|
|
||||||
|
|
||||||
let inbox = Inbox::new();
|
|
||||||
let cmd = cmd.subcommand(inbox.get_command());
|
|
||||||
|
|
||||||
let matches = cmd.get_matches();
|
|
||||||
match matches.subcommand() {
|
|
||||||
Some((name, args)) => {
|
|
||||||
tracing::debug!("executing: {}", name);
|
|
||||||
if inbox.matches_command(name) {
|
|
||||||
inbox.execute_command(args).await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
tracing::info!("executing raw command")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod cli {
|
|
||||||
use clap::{ArgMatches, Command};
|
|
||||||
|
|
||||||
pub trait CliCommand {
|
|
||||||
fn get_name(&self) -> String;
|
|
||||||
fn get_command(&self) -> Command {
|
|
||||||
Command::new(self.get_name())
|
|
||||||
}
|
|
||||||
fn matches_command(&self, name: &str) -> bool {
|
|
||||||
name == self.get_name()
|
|
||||||
}
|
|
||||||
async fn execute_command(&self, args: &ArgMatches) -> eyre::Result<()>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod inbox {
|
|
||||||
pub mod cli {}
|
|
||||||
|
|
||||||
pub mod domain {
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct InboxItem {
|
|
||||||
pub title: String,
|
|
||||||
pub description: Option<String>,
|
|
||||||
pub project: Option<String>,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod services {
|
|
||||||
use crate::cli::CliCommand;
|
|
||||||
|
|
||||||
use super::domain::*;
|
|
||||||
|
|
||||||
pub struct Inbox {}
|
|
||||||
|
|
||||||
impl Inbox {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn add_item(&self, inbox_item: InboxItem) -> eyre::Result<uuid::Uuid> {
|
|
||||||
Ok(uuid::Uuid::new_v4())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CliCommand for Inbox {
|
|
||||||
fn get_name(&self) -> String {
|
|
||||||
"inbox".to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn execute_command(&self, args: &clap::ArgMatches) -> eyre::Result<()> {
|
|
||||||
self.add_item(InboxItem {
|
|
||||||
title: "something".into(),
|
|
||||||
description: Some("description".into()),
|
|
||||||
project: Some("project".into()),
|
|
||||||
})
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
37
cuddle.yaml
Normal file
37
cuddle.yaml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
|
||||||
|
|
||||||
|
base: "git@git.front.kjuulh.io:kjuulh/cuddle-rust-plan.git"
|
||||||
|
|
||||||
|
vars:
|
||||||
|
service: "como-backend"
|
||||||
|
deployments: "git@git.front.kjuulh.io:kjuulh/como-deployments.git"
|
||||||
|
bin_name: como_bin
|
||||||
|
|
||||||
|
scripts:
|
||||||
|
render_como_templates:
|
||||||
|
type: shell
|
||||||
|
local_up:
|
||||||
|
type: shell
|
||||||
|
local_down:
|
||||||
|
type: shell
|
||||||
|
run_como:
|
||||||
|
type: shell
|
||||||
|
migrate_como:
|
||||||
|
type: shell
|
||||||
|
new_migration:
|
||||||
|
type: shell
|
||||||
|
description: "creates a new migration in `como_infrastructure` with the given name prefixed with a date"
|
||||||
|
args:
|
||||||
|
name:
|
||||||
|
type: "flag"
|
||||||
|
name: "name"
|
||||||
|
description: "the name of the migration"
|
||||||
|
required: true
|
||||||
|
"sqlx:prepare":
|
||||||
|
type: shell
|
||||||
|
|
||||||
|
"deploy":
|
||||||
|
type: shell
|
||||||
|
|
||||||
|
"test":
|
||||||
|
type: lua
|
8
scripts/deploy.sh
Executable file
8
scripts/deploy.sh
Executable file
@@ -0,0 +1,8 @@
|
|||||||
|
export $(cat .env | xargs)
|
||||||
|
|
||||||
|
cuddle x start_deployment
|
||||||
|
cuddle x render_templates
|
||||||
|
cuddle x render_como_templates
|
||||||
|
cuddle x build_release
|
||||||
|
cuddle x push_release
|
||||||
|
cuddle x deploy_release
|
7
scripts/local_down.sh
Executable file
7
scripts/local_down.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cuddle render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml
|
||||||
|
|
||||||
|
docker compose -f $TMP/docker-compose.local_up.yml down -v
|
7
scripts/local_up.sh
Executable file
7
scripts/local_up.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
cuddle render_template --template-file $TMP/docker-compose.local_up.yml.tmpl --dest $TMP/docker-compose.local_up.yml
|
||||||
|
|
||||||
|
docker compose -f $TMP/docker-compose.local_up.yml up -d --remove-orphans --build
|
11
scripts/lua/commands/init.lua
Normal file
11
scripts/lua/commands/init.lua
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
M = {}
|
||||||
|
|
||||||
|
function M.something()
|
||||||
|
local io = require("io")
|
||||||
|
local output = io.popen("ls"):read("a*")
|
||||||
|
print(output)
|
||||||
|
|
||||||
|
print("hello-world!")
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
6
scripts/migrate_como.sh
Executable file
6
scripts/migrate_como.sh
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
export $(cat .env | xargs)
|
||||||
|
|
||||||
|
cargo sqlx migrate run --source como_infrastructure/migrations --database-url=$DATABASE_URL
|
||||||
|
|
5
scripts/new_migration.sh
Executable file
5
scripts/new_migration.sh
Executable file
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
export $(cat .env | xargs)
|
||||||
|
|
||||||
|
cargo sqlx migrate add --source como_infrastructure/migrations $NAME
|
10
scripts/render_como_templates.sh
Executable file
10
scripts/render_como_templates.sh
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
deploymentrepo="$TMP/deployments"
|
||||||
|
|
||||||
|
CUDDLE_FETCH_POLICY=never cuddle render_template \
|
||||||
|
--template-file "$TMP/.env.example.tmpl" \
|
||||||
|
--dest "$deploymentrepo/$SERVICE/env.example"
|
||||||
|
|
5
scripts/run_como.sh
Executable file
5
scripts/run_como.sh
Executable file
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
(cd como_bin; cargo watch -x run)
|
7
scripts/sqlx:prepare.sh
Executable file
7
scripts/sqlx:prepare.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
export $(cat .env | xargs)
|
||||||
|
|
||||||
|
cd como_infrastructure || return
|
||||||
|
|
||||||
|
cargo sqlx prepare -- --all-targets --all-features
|
5
scripts/test.lua
Normal file
5
scripts/test.lua
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
print("from test")
|
||||||
|
|
||||||
|
local init = require("commands")
|
||||||
|
|
||||||
|
init.something()
|
4
templates/.env.example.tmpl
Normal file
4
templates/.env.example.tmpl
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
POSTGRES_DB=como
|
||||||
|
POSTGRES_USER=como
|
||||||
|
POSTGRES_PASSWORD=somenotverysecurepassword
|
||||||
|
DATABASE_URL="postgres://como:somenotverysecurepassword@localhost:5432/como"
|
7
templates/docker-compose.local_up.dockerignore
Normal file
7
templates/docker-compose.local_up.dockerignore
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
target/
|
||||||
|
.git/
|
||||||
|
.cuddle/
|
||||||
|
scripts/
|
||||||
|
cuddle.yaml
|
||||||
|
local.sh
|
||||||
|
README.md
|
17
templates/docker-compose.local_up.yml.tmpl
Normal file
17
templates/docker-compose.local_up.yml.tmpl
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
version: '3.7'
|
||||||
|
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: local_up.Dockerfile
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
- POSTGRES_PASSWORD=somenotverysecurepassword
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
volumes:
|
||||||
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pgdata:
|
8
templates/init-user-db.sh
Normal file
8
templates/init-user-db.sh
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#!bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
||||||
|
CREATE USER como WITH PASSWORD 'somenotverysecurepassword';
|
||||||
|
CREATE DATABASE como;
|
||||||
|
GRANT ALL PRIVILEGES ON DATABASE como TO como;
|
||||||
|
EOSQL
|
3
templates/local_up.Dockerfile
Normal file
3
templates/local_up.Dockerfile
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
FROM postgres:14-alpine
|
||||||
|
|
||||||
|
COPY *.sh /docker-entrypoint-initdb.d/
|
Reference in New Issue
Block a user