-
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.
- Loading branch information
0 parents
commit 85caf44
Showing
13 changed files
with
2,054 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
/target | ||
**/*.rs.bk |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "rustfest_wundergraph_workshop" | ||
version = "0.1.0" | ||
authors = ["Georg Semmler <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
actix-web = "1" | ||
serde = {version = "1", features = ["derive"]} | ||
serde_json = "1" | ||
diesel = {version = "1", features = ["postgres", "r2d2", "chrono"]} | ||
diesel_migrations = "1" | ||
structopt = "0.3" | ||
env_logger = "0.7" | ||
chrono = {version = "0.4", features = ["serde"]} | ||
failure = "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,5 @@ | ||
# For documentation on how to configure this file, | ||
# see diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/schema.rs" |
Empty file.
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,6 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
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,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
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,54 @@ | ||
#[macro_use] | ||
extern crate diesel; | ||
|
||
use actix_web::{middleware, App, HttpServer}; | ||
use diesel::pg::PgConnection; | ||
use diesel::r2d2::{ConnectionManager, Pool}; | ||
use structopt::StructOpt; | ||
|
||
mod model; | ||
mod schema; | ||
|
||
#[derive(Debug, StructOpt)] | ||
#[structopt(name = "rustfest")] | ||
struct Opt { | ||
#[structopt(short = "u", long = "db-url")] | ||
database_url: String, | ||
#[structopt(short = "s", long = "socket", default_value = "127.0.0.1:8000")] | ||
socket: String, | ||
} | ||
|
||
#[derive(Clone)] | ||
struct AppState { | ||
pool: Pool<ConnectionManager<PgConnection>>, | ||
} | ||
|
||
fn main() { | ||
let opt = Opt::from_args(); | ||
::std::env::set_var("RUST_LOG", "actix_web=info"); | ||
env_logger::init(); | ||
let manager = ConnectionManager::<PgConnection>::new(opt.database_url); | ||
let pool = Pool::builder().build(manager).expect("Failed to init pool"); | ||
|
||
diesel_migrations::run_pending_migrations(&pool.get().expect("Failed to get db connection")) | ||
.expect("Failed to run migrations"); | ||
|
||
let data = AppState { pool }; | ||
|
||
let url = opt.socket; | ||
|
||
println!("Started http server: http://{}", url); | ||
|
||
HttpServer::new(move || { | ||
App::new() | ||
.configure(model::posts::config) | ||
.configure(model::users::config) | ||
.configure(model::comments::config) | ||
.data(data.clone()) | ||
.wrap(middleware::Logger::default()) | ||
}) | ||
.bind(&url) | ||
.expect("Failed to start server") | ||
.run() | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use actix_web::web; | ||
|
||
pub fn config(cfg: &mut web::ServiceConfig) {} |
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 posts; | ||
pub mod users; | ||
pub mod comments; |
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 @@ | ||
use actix_web::web; | ||
|
||
pub fn config(cfg: &mut web::ServiceConfig) {} |
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 @@ | ||
use actix_web::web; | ||
|
||
pub fn config(cfg: &mut web::ServiceConfig) {} |
Empty file.