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

View File

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

View File

@ -1,9 +1,9 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use http::StatusCode;
use ipnetwork::IpNetwork; use ipnetwork::IpNetwork;
use std::net::SocketAddr; use std::net::SocketAddr;
use tracing::instrument; use tracing::instrument;
use uuid::Uuid; use uuid::Uuid;
use warp::http::StatusCode;
use warp::reply::{json, with_header, with_status}; use warp::reply::{json, with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
@ -134,6 +134,7 @@ pub async fn create_owner(
owner: Owner, owner: Owner,
remote_addr: Option<SocketAddr>, remote_addr: Option<SocketAddr>,
api_key: Option<Uuid>, api_key: Option<Uuid>,
real_ip: Option<IpNetwork>,
env: Environment, env: Environment,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
if let Some(api_key) = api_key { if let Some(api_key) = api_key {
@ -145,6 +146,7 @@ pub async fn create_owner(
}, },
None => Owner { None => Owner {
api_key: Some(api_key), api_key: Some(api_key),
ip_address: real_ip,
..owner ..owner
}, },
}; };
@ -186,6 +188,7 @@ pub async fn delete_owner(
Ok(StatusCode::NO_CONTENT) 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> { pub async fn get_interior_ref_list(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches env.caches
.interior_ref_list .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 = env::var("HOST").expect("`HOST` environment variable not defined");
let host_url = Url::parse(&host).expect("Cannot parse URL from `HOST` environment variable"); 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 env = Environment::new(api_url).await?;
let base = warp::path("api").and(warp::path("v1")); let base = warp::path("v1");
let routes = base let routes = filters::status()
.and( .or(base.and(
filters::shops(env.clone()) filters::shops(env.clone())
.or(filters::owners(env.clone())) .or(filters::owners(env.clone()))
.or(filters::interior_ref_lists(env.clone())), .or(filters::interior_ref_lists(env.clone())),
) ))
.recover(problem::unpack_problem) .recover(problem::unpack_problem)
.with(warp::compression::gzip()) .with(warp::compression::gzip())
.with(warp::trace::request()); .with(warp::trace::request());
@ -97,7 +97,7 @@ async fn main() -> Result<()> {
let server = if let Some(l) = listenfd.take_tcp_listener(0)? { let server = if let Some(l) = listenfd.take_tcp_listener(0)? {
Server::from_tcp(l)? Server::from_tcp(l)?
} else { } 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; // 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)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct InteriorRef { pub struct InteriorRef {
pub mod_name: String, pub base_mod_name: String,
pub local_form_id: i32, pub base_local_form_id: i32,
pub position_x: f64, pub ref_mod_name: Option<String>,
pub position_y: f64, pub ref_local_form_id: i32,
pub position_z: f64, pub position_x: f32,
pub angle_x: f64, pub position_y: f32,
pub angle_y: f64, pub position_z: f32,
pub angle_z: f64, pub angle_x: f32,
pub scale: f64, pub angle_y: f32,
pub angle_z: f32,
pub scale: u16,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

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