Update schema, add status endpoint

This commit is contained in:
Tyler Hallada 2020-09-07 16:51:01 -04:00
parent 170a3a8b02
commit 8d5fe7f75d
13 changed files with 249 additions and 246 deletions

View File

@ -8,7 +8,7 @@ pub fn migration() -> String {
t.add_column("name", types::varchar(255));
t.add_column("api_key", types::uuid().indexed(true));
t.add_column("ip_address", types::custom("inet").nullable(true));
t.add_column("mod_version", types::varchar(25));
t.add_column("mod_version", types::integer());
t.add_column("created_at", types::custom("timestamp(3)"));
t.add_column("updated_at", types::custom("timestamp(3)"));
t.add_index(
@ -22,10 +22,11 @@ pub fn migration() -> String {
t.add_column("name", types::varchar(255));
t.add_column("owner_id", types::foreign("owners", "id").indexed(true));
t.add_column("description", types::text().nullable(true));
t.add_column("is_not_sell_buy", types::boolean().default(true));
t.add_column("sell_buy_list_id", types::integer().default(0));
t.add_column("vendor_id", types::integer());
t.add_column("vendor_gold", types::integer());
// removing these until I figure out the plan for buying and selling
// t.add_column("is_not_sell_buy", types::boolean().default(true));
// t.add_column("sell_buy_list_id", types::integer().default(0));
// t.add_column("vendor_id", types::integer());
// t.add_column("vendor_gold", types::integer());
t.add_column("created_at", types::custom("timestamp(3)"));
t.add_column("updated_at", types::custom("timestamp(3)"));
t.add_index(

View File

@ -1,3 +1,4 @@
use http::StatusCode;
use serde::de::DeserializeOwned;
use std::convert::Infallible;
use warp::{Filter, Rejection, Reply};
@ -6,6 +7,12 @@ use super::handlers;
use super::models::{InteriorRefList, ListParams, Owner, Shop};
use super::Environment;
pub fn status() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path::path("status")
.and(warp::get())
.map(|| StatusCode::OK) // TODO: return what api versions this server supports instead
}
pub fn shops(env: Environment) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::path("shops").and(
get_shop(env.clone())
@ -88,6 +95,7 @@ pub fn create_owner(
.and(json_body::<Owner>())
.and(warp::addr::remote())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("x-real-ip"))
.and(with_env(env))
.and_then(handlers::create_owner)
}

View File

@ -1,9 +1,9 @@
use anyhow::{anyhow, Result};
use http::StatusCode;
use ipnetwork::IpNetwork;
use std::net::SocketAddr;
use tracing::instrument;
use uuid::Uuid;
use warp::http::StatusCode;
use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply};
@ -134,6 +134,7 @@ pub async fn create_owner(
owner: Owner,
remote_addr: Option<SocketAddr>,
api_key: Option<Uuid>,
real_ip: Option<IpNetwork>,
env: Environment,
) -> Result<impl Reply, Rejection> {
if let Some(api_key) = api_key {
@ -145,6 +146,7 @@ pub async fn create_owner(
},
None => Owner {
api_key: Some(api_key),
ip_address: real_ip,
..owner
},
};
@ -186,6 +188,7 @@ pub async fn delete_owner(
Ok(StatusCode::NO_CONTENT)
}
// TODO: probably need a way to get by shop id instead
pub async fn get_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
.interior_ref_list

View File

@ -73,16 +73,16 @@ async fn main() -> Result<()> {
let host = env::var("HOST").expect("`HOST` environment variable not defined");
let host_url = Url::parse(&host).expect("Cannot parse URL from `HOST` environment variable");
let api_url = host_url.join("/api/v1/")?;
let api_url = host_url.join("/v1/")?;
let env = Environment::new(api_url).await?;
let base = warp::path("api").and(warp::path("v1"));
let routes = base
.and(
let base = warp::path("v1");
let routes = filters::status()
.or(base.and(
filters::shops(env.clone())
.or(filters::owners(env.clone()))
.or(filters::interior_ref_lists(env.clone())),
)
))
.recover(problem::unpack_problem)
.with(warp::compression::gzip())
.with(warp::trace::request());
@ -97,7 +97,7 @@ async fn main() -> Result<()> {
let server = if let Some(l) = listenfd.take_tcp_listener(0)? {
Server::from_tcp(l)?
} else {
Server::bind(&([0, 0, 0, 0], 3030).into())
Server::bind(&([127, 0, 0, 1], 3030).into())
};
// warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;

View File

@ -17,15 +17,17 @@ use crate::problem::forbidden_permission;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct InteriorRef {
pub mod_name: String,
pub local_form_id: i32,
pub position_x: f64,
pub position_y: f64,
pub position_z: f64,
pub angle_x: f64,
pub angle_y: f64,
pub angle_z: f64,
pub scale: f64,
pub base_mod_name: String,
pub base_local_form_id: i32,
pub ref_mod_name: Option<String>,
pub ref_local_form_id: i32,
pub position_x: f32,
pub position_y: f32,
pub position_z: f32,
pub angle_x: f32,
pub angle_y: f32,
pub angle_z: f32,
pub scale: u16,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View File

@ -19,7 +19,7 @@ pub struct Owner {
pub api_key: Option<Uuid>,
#[serde(skip_serializing)]
pub ip_address: Option<IpNetwork>,
pub mod_version: String,
pub mod_version: i32,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}

View File

@ -15,10 +15,11 @@ pub struct Shop {
pub name: String,
pub owner_id: Option<i32>,
pub description: String,
pub is_not_sell_buy: bool,
pub sell_buy_list_id: i32,
pub vendor_id: i32,
pub vendor_gold: i32,
// removing these until I figure out the plan for buying and selling
// pub is_not_sell_buy: bool,
// pub sell_buy_list_id: i32,
// pub vendor_id: i32,
// pub vendor_gold: i32,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
@ -46,17 +47,12 @@ impl Model for Shop {
Ok(sqlx::query_as!(
Self,
"INSERT INTO shops
(name, owner_id, description, is_not_sell_buy, sell_buy_list_id, vendor_id,
vendor_gold, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, now(), now())
(name, owner_id, description, created_at, updated_at)
VALUES ($1, $2, $3, now(), now())
RETURNING *",
self.name,
self.owner_id,
self.description,
self.is_not_sell_buy,
self.sell_buy_list_id,
self.vendor_id,
self.vendor_gold,
)
.fetch_one(db)
.await?)

View File

@ -1,7 +1,7 @@
use anyhow::{anyhow, Error};
use http::StatusCode;
use http_api_problem::HttpApiProblem;
use tracing::error;
use warp::http::StatusCode;
use warp::{reject, Rejection, Reply};
pub fn forbidden_permission() -> Error {

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
{
"name": "Test Owner",
"mod_version": "0.0.1"
"mod_version": 1
}

View File

@ -1,4 +1,5 @@
{
"id": null,
"name": "Test Owner 2",
"mod_version": "0.0.1"
"mod_version": 1
}

View File

@ -1,8 +1,4 @@
{
"name": "Test Shop",
"description": "for testing",
"is_not_sell_buy": true,
"sell_buy_list_id": 1,
"vendor_id": 1,
"vendor_gold": 0
"description": "for testing"
}

View File

@ -1,8 +1,4 @@
{
"name": "Test Shop 2",
"description": "for testing",
"is_not_sell_buy": true,
"sell_buy_list_id": 1,
"vendor_id": 1,
"vendor_gold": 0
"description": "for testing"
}