-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from JonasCir/fix-new
Rewrite the new module such that nothing needs to be downloaded
- Loading branch information
Showing
58 changed files
with
2,976 additions
and
420 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATABASE_URL='postgres://postgres:postgres@localhost:5432/postgres' | ||
PORT=3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target | ||
.env | ||
keyfile.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[package] | ||
name = "{{ app_name }}" | ||
version = "0.1.0" | ||
edition = "2021" | ||
rust-version = "1.64" | ||
|
||
[workspace] | ||
members = [".", "entity", "migration"] | ||
|
||
[workspace.dependencies] | ||
# graphql base library | ||
async-graphql = { version = "4.0.13", features = ["chrono"] } | ||
# graphql integration for axum web framework | ||
async-graphql-axum = "4.0.13" | ||
# async std lib: used by migration crate | ||
async-std = { version = "1.12.0", features = ["attributes", "tokio1"] } | ||
# our general web framework | ||
axum = "^0.5.1" | ||
# read dotenv files in main.rs | ||
dotenv = "0.15.0" | ||
# generated local entities | ||
entity = { path = "entity" } | ||
# generated local migrations | ||
migration = { path = "migration" } | ||
# database connection and orm | ||
sea-orm = { version = "0.9.2", features = ["runtime-tokio-native-tls", "sqlx-postgres"] } | ||
# migrate database before startup | ||
sea-orm-migration = "0.9.2" | ||
# asnyc runtime: used by main.rs | ||
tokio = { version = "1.0", features = ["full"] } | ||
|
||
[dependencies] | ||
async-graphql = { workspace = true } | ||
async-graphql-axum = { workspace = true } | ||
async-std = { workspace = true } | ||
axum = {workspace = true} | ||
dotenv = { workspace = true } | ||
entity = { workspace = true } | ||
migration = { workspace = true } | ||
sea-orm = { workspace = true } | ||
sea-orm-migration = { workspace = true } | ||
tokio = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM rust:1.64 as builder | ||
|
||
# create a new empty bin project such that caching dependencies works | ||
RUN cargo new --bin /usr/src/{{ app_name }} | ||
|
||
WORKDIR /usr/src/{{ app_name }} | ||
|
||
# cache dependencies | ||
COPY entity entity | ||
COPY migration migration | ||
COPY Cargo.toml Cargo.toml | ||
RUN cargo build --release | ||
|
||
# copy source code | ||
COPY src src | ||
|
||
# release build | ||
RUN cargo build --release | ||
|
||
FROM debian:bullseye-slim | ||
|
||
RUN apt-get update \ | ||
&& apt-get upgrade \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
COPY --from=builder /usr/src/{{ app_name }}/target/release/{{ app_name }} . | ||
|
||
CMD ["./{{ app_name }}"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# {{ app_name }} | ||
|
||
Rust Serverless Framework | ||
|
||
## Usage | ||
|
||
Run PostgreSQL Docker | ||
```bash | ||
zapp docker psql | ||
``` | ||
|
||
Run Server | ||
```bash | ||
cargo run | ||
``` | ||
|
||
Access to GraphQL Playground | ||
|
||
`http://localshost:3000/api/graphql` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "entity" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
name = "entity" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
sea-orm = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use dotenv::dotenv; | ||
use sea_orm::DatabaseConnection; | ||
|
||
pub struct Database { | ||
pub connection: DatabaseConnection, | ||
} | ||
|
||
impl Database { | ||
pub async fn new() -> Self { | ||
dotenv().ok(); | ||
let connection = sea_orm::Database::connect(std::env::var("DATABASE_URL").unwrap()) | ||
.await | ||
.expect("Could not connect to database"); | ||
|
||
Database { connection } | ||
} | ||
|
||
pub fn get_connection(&self) -> &DatabaseConnection { | ||
&self.connection | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod mutation; | ||
pub mod query; | ||
pub mod schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use async_graphql::SimpleObject; | ||
|
||
#[derive(SimpleObject)] | ||
pub struct DeleteResult { | ||
pub success: bool, | ||
pub rows_affected: u64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod common; | ||
|
||
#[derive(async_graphql::MergedObject, Default)] | ||
pub struct Mutation(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[derive(async_graphql::MergedObject, Default)] | ||
pub struct Query(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use async_graphql::{EmptySubscription, Schema}; | ||
use migration::{Migrator, MigratorTrait}; | ||
|
||
use crate::{ | ||
db::Database, | ||
graphql::{mutation::Mutation, query::Query}, | ||
}; | ||
|
||
pub type ZappSchema = Schema<Query, Mutation, EmptySubscription>; | ||
|
||
/// Builds the GraphQL Schema, attaching the Database to the context | ||
pub async fn build_schema() -> ZappSchema { | ||
let db = Database::new().await; | ||
|
||
Migrator::up(db.get_connection(), None).await.unwrap(); | ||
|
||
Schema::build(Query::default(), Mutation::default(), EmptySubscription) | ||
.data(db) | ||
.finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
mod db; | ||
mod graphql; | ||
|
||
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; | ||
use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; | ||
use axum::{ | ||
extract::Extension, | ||
response::{Html, IntoResponse}, | ||
routing::get, | ||
Router, | ||
}; | ||
use graphql::schema::{build_schema, ZappSchema}; | ||
use std::env; | ||
|
||
#[cfg(debug_assertions)] | ||
use dotenv::dotenv; | ||
|
||
async fn graphql_handler(schema: Extension<ZappSchema>, req: GraphQLRequest) -> GraphQLResponse { | ||
schema.execute(req.into_inner()).await.into() | ||
} | ||
|
||
async fn graphql_playground() -> impl IntoResponse { | ||
Html(playground_source(GraphQLPlaygroundConfig::new( | ||
"/api/graphql", | ||
))) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
#[cfg(debug_assertions)] | ||
dotenv().ok(); | ||
let port = env::var("PORT").expect("PORT must be set."); | ||
let schema = build_schema().await; | ||
|
||
let app = Router::new() | ||
.route( | ||
"/api/graphql", | ||
get(graphql_playground).post(graphql_handler), | ||
) | ||
.layer(Extension(schema)); | ||
|
||
println!("Playground: http://localhost:{}/api/graphql", port); | ||
|
||
axum::Server::bind(&format!("0.0.0.0:{}", port).parse().unwrap()) | ||
.serve(app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.