diff options
| author | Kevin J Hoerr <kjhoerr@submelon.tech> | 2018-08-26 01:38:28 -0400 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@submelon.tech> | 2018-08-31 23:24:45 -0400 |
| commit | 0965d62be00a7820f97284704dc71f37e661b412 (patch) | |
| tree | 5a5c9d69062e24aa926eb30447c5ff27e0a65492 /src | |
| parent | 1e3946f04b5b602d3869a285d897acb0ba2b3c35 (diff) | |
| download | august-offensive-0965d62be00a7820f97284704dc71f37e661b412.tar.gz august-offensive-0965d62be00a7820f97284704dc71f37e661b412.tar.bz2 august-offensive-0965d62be00a7820f97284704dc71f37e661b412.zip | |
Begin migration to Rust; Add actix-web, diesel as main dependencies
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/august-offensive.rs | 45 | ||||
| -rw-r--r-- | src/util/fresh.sql | 103 |
2 files changed, 148 insertions, 0 deletions
diff --git a/src/bin/august-offensive.rs b/src/bin/august-offensive.rs new file mode 100644 index 0000000..77ca2f6 --- /dev/null +++ b/src/bin/august-offensive.rs @@ -0,0 +1,45 @@ +extern crate actix; +extern crate actix_web; +extern crate env_logger; +extern crate dotenv; +extern crate diesel; + +use actix_web::{middleware, server, App, HttpRequest}; +use diesel::prelude::*; +use diesel::pg::PgConnection; +use dotenv::dotenv; +use std::env; + +fn index(_req: &HttpRequest) -> &'static str { + "Hello, world!" +} + +// TODO describe change of direction in README +// TODO implement error-chain +// TODO match 0.1.0 functionality +fn main() { + dotenv().ok(); + let db_url = env::var("DATABASE_URL") + .expect("DATABASE_URL must be set"); + let bind_address = env::var("BIND_ADDRESS") + .expect("BIND_ADDRESS must be set"); + + env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); + let sys = actix::System::new("hello-world"); + + PgConnection::establish(&db_url) + .expect(&format!("Error connecting to {}", db_url)); + + server::new(|| { + App::new() + .middleware(middleware::Logger::default()) + .resource("/index.html", |r| r.f(index)) + .resource("/", |r| r.f(index)) + }).bind(&bind_address) + .unwrap() + .start(); + + println!("Started http server: {}", bind_address); + let _ = sys.run(); +} diff --git a/src/util/fresh.sql b/src/util/fresh.sql new file mode 100644 index 0000000..f39b360 --- /dev/null +++ b/src/util/fresh.sql @@ -0,0 +1,103 @@ + -- fresh.sql: SQL script that creates the tables used by AO + -- please drop all tables in db before initiating unless told otherwise + + -- uncomment the following two lines to automatically drop all tables +--DROP SCHEMA "public" CASCADE; +--CREATE SCHEMA "public"; + +CREATE OR REPLACE FUNCTION public.GETDATE() RETURNS TIMESTAMPTZ + STABLE LANGUAGE SQL AS 'SELECT NOW()'; + + -- base table for users +CREATE TABLE users ( + userid SERIAL, + email VARCHAR(40) UNIQUE NOT NULL, + firstname VARCHAR(20) NOT NULL, + lastname VARCHAR(20) NOT NULL, + password VARCHAR(255) NOT NULL, -- intend to use bcrypt hash through PHP + games INTEGER DEFAULT 0, -- simple tracking statistics + wins INTEGER DEFAULT 0, -- " + joindate DATE DEFAULT GETDATE(), + activated BIT(1) DEFAULT B'0', -- see activation_keys table + PRIMARY KEY (userid) +); + + -- when activation is completed, key gets deleted and activated bit for user gets flipped to 1. +CREATE TABLE activation_keys ( + code VARCHAR(128), + userid SERIAL, + PRIMARY KEY (code), + CONSTRAINT activation_keys_userid + FOREIGN KEY (userid) REFERENCES users(userid) +); + + -- base table for games +CREATE TABLE games ( + gameid SERIAL, + title VARCHAR(140) NOT NULL, + gametypeid SERIAL NOT NULL, -- future proofing: suppose different game types + players SMALLINT NOT NULL, -- number of players in a game + waitfor INTEGER NOT NULL, -- time in seconds to wait for player to complete a turn + lastturn DATE DEFAULT GETDATE(), + gamestate INTEGER DEFAULT 1, + PRIMARY KEY (gameid) +); + + -- associative table: many users, many games +CREATE TABLE allegiances ( + gameid SERIAL, + userid SERIAL, + allegiance VARCHAR(140), -- important/unimportant role for user (depends on gametype) + ordernum SMALLINT NOT NULL, -- user # spot in game + playing SMALLINT DEFAULT 1, -- default is binary: 0 means player OOP + PRIMARY KEY (gameid, userid), + CONSTRAINT users_games_gameid + FOREIGN KEY (gameid) REFERENCES games(gameid), + CONSTRAINT users_games_userid + FOREIGN KEY (userid) REFERENCES users(userid) +); + + -- Regions are composed of nationstates +CREATE TABLE regions ( + regionid SERIAL, + name VARCHAR(64) NOT NULL, + abbreviation CHAR(2), + bonus INTEGER NOT NULL, + PRIMARY KEY (regionid) +); + + -- base table for nationstates +CREATE TABLE nationstates ( + nationid SERIAL, + regionid SERIAL, + name VARCHAR(64) NOT NULL, + abbreviation CHAR(4), + PRIMARY KEY (nationid), + CONSTRAINT nationstates_regionid + FOREIGN KEY (regionid) REFERENCES regions(regionid) +); + + -- nationstates border nationstates +CREATE TABLE borders ( + nationid SERIAL, + borderid SERIAL, + PRIMARY KEY (nationid, borderid), + CONSTRAINT borders_nationid + FOREIGN KEY (nationid) REFERENCES nationstates(nationid), + CONSTRAINT borders_borderid + FOREIGN KEY (borderid) REFERENCES nationstates(nationid) +); + + -- associative table: many games, many nationstates. Users control them. +CREATE TABLE games_nationstates ( + gameid SERIAL, + nationid SERIAL, + userid SERIAL, -- owner of nationstate for particular game + PRIMARY KEY (gameid, nationid), + CONSTRAINT games_nationstates_gameid + FOREIGN KEY (gameid) REFERENCES games(gameid), + CONSTRAINT games_nationstates_nationid + FOREIGN KEY (nationid) REFERENCES nationstates(nationid), + CONSTRAINT games_nationstates_userid + FOREIGN KEY (userid) REFERENCES users(userid) +); |
