Skip to content

Commit

Permalink
Initial template
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich committed Nov 8, 2019
0 parents commit 85caf44
Show file tree
Hide file tree
Showing 13 changed files with 2,054 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
1,920 changes: 1,920 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Cargo.toml
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"
5 changes: 5 additions & 0 deletions diesel.toml
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 added migrations/.gitkeep
Empty file.
6 changes: 6 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/down.sql
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();
36 changes: 36 additions & 0 deletions migrations/00000000000000_diesel_initial_setup/up.sql
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;
54 changes: 54 additions & 0 deletions src/main.rs
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();
}
3 changes: 3 additions & 0 deletions src/model/comments.rs
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) {}
4 changes: 4 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub mod posts;
pub mod users;
pub mod comments;
3 changes: 3 additions & 0 deletions src/model/posts.rs
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) {}
3 changes: 3 additions & 0 deletions src/model/users.rs
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 added src/schema.rs
Empty file.

0 comments on commit 85caf44

Please sign in to comment.