Initial commit
This commit is contained in:
commit
51384cacbb
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
/target/
|
||||
**/*.rs.bk
|
1236
Cargo.lock
generated
Normal file
1236
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
authors = ["Sakuhl <2012collector@gmail.com>"]
|
||||
name = "wynnapi"
|
||||
version = "0.1.0"
|
||||
|
||||
[dependencies]
|
||||
reqwest = "0.8.2"
|
||||
rocket = "0.3.5"
|
||||
rocket_codegen = "0.3.5"
|
||||
wynncraft = { git = "https://gitlab.com/Sakuhl/wynncraft" }
|
||||
diesel = { version = "1.0", features = ["postgres"] }
|
1
RustConfig
Normal file
1
RustConfig
Normal file
@ -0,0 +1 @@
|
||||
VERSION=nightly-2017-12-20
|
0
migrations/.gitkeep
Normal file
0
migrations/.gitkeep
Normal file
2
migrations/00000000000000_diesel_initial_setup/down.sql
Normal file
2
migrations/00000000000000_diesel_initial_setup/down.sql
Normal file
@ -0,0 +1,2 @@
|
||||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
36
migrations/00000000000000_diesel_initial_setup/up.sql
Normal file
36
migrations/00000000000000_diesel_initial_setup/up.sql
Normal file
@ -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;
|
1
migrations/2018-01-15-123353_create_guild_list/down.sql
Normal file
1
migrations/2018-01-15-123353_create_guild_list/down.sql
Normal file
@ -0,0 +1 @@
|
||||
DROP TABLE guilds
|
4
migrations/2018-01-15-123353_create_guild_list/up.sql
Normal file
4
migrations/2018-01-15-123353_create_guild_list/up.sql
Normal file
@ -0,0 +1,4 @@
|
||||
CREATE TABLE guilds (
|
||||
prefix CHAR(3) PRIMARY KEY NOT NULL,
|
||||
name VARCHAR(32) NOT NULL
|
||||
)
|
65
src/main.rs
Normal file
65
src/main.rs
Normal file
@ -0,0 +1,65 @@
|
||||
#![feature(plugin)]
|
||||
#![plugin(rocket_codegen)]
|
||||
|
||||
extern crate rocket;
|
||||
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
use diesel::prelude::*;
|
||||
use diesel::pg::PgConnection;
|
||||
|
||||
extern crate wynncraft;
|
||||
|
||||
use std::env;
|
||||
use std::thread;
|
||||
|
||||
mod models;
|
||||
use models::*;
|
||||
mod schema;
|
||||
use schema::*;
|
||||
|
||||
#[get("/list")]
|
||||
fn list() -> String {
|
||||
let mut json = "{\"guilds\":[".to_owned();
|
||||
for guild in guilds::table.load::<Guild>(&establish_connection()).unwrap() {
|
||||
json.push_str("{");
|
||||
json.push_str(&format!(r#""name":{:?},"prefix":{:?}"#, guild.name, guild.prefix));
|
||||
json.push_str("},");
|
||||
}
|
||||
json.push_str("]}");
|
||||
json
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let server_handle = thread::Builder::new()
|
||||
.spawn(|| {
|
||||
rocket::ignite().mount("/", routes![list]).launch()
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let guild_list = wynncraft::guild_list().unwrap();
|
||||
let conn = establish_connection();
|
||||
|
||||
let values = guild_list.into_iter().map(|name| {
|
||||
thread::sleep_ms(2500);
|
||||
Guild { prefix: wynncraft::guild(&name).unwrap().unwrap().prefix, name }
|
||||
}).collect::<Vec<_>>();
|
||||
for value in values.into_iter() {
|
||||
diesel::insert_into(guilds::table)
|
||||
.values(&value)
|
||||
.on_conflict(guilds::prefix)
|
||||
.do_nothing()
|
||||
.execute(&conn).unwrap();
|
||||
}
|
||||
|
||||
println!("Guilds updated!");
|
||||
|
||||
server_handle.join().unwrap();
|
||||
}
|
||||
|
||||
fn establish_connection() -> PgConnection {
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("DATABASE_URL must be set");
|
||||
PgConnection::establish(&database_url)
|
||||
.expect(&format!("Error connecting to {}", database_url))
|
||||
}
|
8
src/models.rs
Normal file
8
src/models.rs
Normal file
@ -0,0 +1,8 @@
|
||||
use ::schema::*;
|
||||
|
||||
#[derive(Queryable, Insertable)]
|
||||
#[table_name="guilds"]
|
||||
pub struct Guild {
|
||||
pub prefix: String,
|
||||
pub name: String
|
||||
}
|
6
src/schema.rs
Normal file
6
src/schema.rs
Normal file
@ -0,0 +1,6 @@
|
||||
table! {
|
||||
guilds (prefix) {
|
||||
prefix -> Bpchar,
|
||||
name -> Varchar,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user