Add custom server header to responses

This commit is contained in:
Tyler Hallada 2020-11-12 21:05:04 -05:00
parent a1107b7100
commit 0bc94e4b7d
3 changed files with 18 additions and 9 deletions

13
Cargo.lock generated
View File

@ -2010,9 +2010,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-attributes" name = "tracing-attributes"
version = "0.1.9" version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0693bf8d6f2bf22c690fc61a9d21ac69efdbb894a17ed596b9af0f01e64b84b" checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -2051,9 +2051,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-serde" name = "tracing-serde"
version = "0.1.1" version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6ccba2f8f16e0ed268fc765d9b7ff22e965e7185d32f8f1ec8294fe17d86e79" checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b"
dependencies = [ dependencies = [
"serde", "serde",
"tracing-core", "tracing-core",
@ -2061,9 +2061,9 @@ dependencies = [
[[package]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.2.10" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7b33f8b2ef2ab0c3778c12646d9c42a24f7772bee4cdafc72199644a9f58fdc" checksum = "abd165311cc4d7a555ad11cc77a37756df836182db0d81aac908c8184c584f40"
dependencies = [ dependencies = [
"ansi_term", "ansi_term",
"chrono", "chrono",
@ -2074,6 +2074,7 @@ dependencies = [
"serde_json", "serde_json",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local",
"tracing-core", "tracing-core",
"tracing-log", "tracing-log",
"tracing-serde", "tracing-serde",

View File

@ -2,7 +2,7 @@ use std::marker::PhantomData;
use std::str::FromStr; use std::str::FromStr;
use anyhow::{anyhow, Error, Result}; use anyhow::{anyhow, Error, Result};
use http::header::{HeaderValue, CONTENT_TYPE, ETAG}; use http::header::{HeaderValue, CONTENT_TYPE, ETAG, SERVER};
use http::StatusCode; use http::StatusCode;
use http_api_problem::HttpApiProblem; use http_api_problem::HttpApiProblem;
use mime::{FromStrError, Mime}; use mime::{FromStrError, Mime};
@ -23,6 +23,8 @@ use super::caches::{CachedResponse, CACHES};
use super::problem::{unauthorized_no_api_key, unauthorized_no_owner}; use super::problem::{unauthorized_no_api_key, unauthorized_no_owner};
use super::Environment; use super::Environment;
pub static SERVER_STRING: &str = "BazaarRealmAPI/0.1.0";
#[instrument(level = "debug", skip(env, api_key))] #[instrument(level = "debug", skip(env, api_key))]
pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i32> { pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i32> {
if let Some(api_key) = api_key { if let Some(api_key) = api_key {
@ -75,6 +77,8 @@ impl Reply for ETagReply<Json> {
let mut res = Response::new(self.body.into()); let mut res = Response::new(self.body.into());
res.headers_mut() res.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
res.headers_mut()
.insert(SERVER, HeaderValue::from_static(SERVER_STRING));
if let Ok(val) = HeaderValue::from_str(&self.etag) { if let Ok(val) = HeaderValue::from_str(&self.etag) {
res.headers_mut().insert(ETAG, val); res.headers_mut().insert(ETAG, val);
} else { } else {
@ -113,6 +117,8 @@ impl Reply for ETagReply<Bincode> {
CONTENT_TYPE, CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"), HeaderValue::from_static("application/octet-stream"),
); );
res.headers_mut()
.insert(SERVER, HeaderValue::from_static(SERVER_STRING));
if let Ok(val) = HeaderValue::from_str(&self.etag) { if let Ok(val) = HeaderValue::from_str(&self.etag) {
res.headers_mut().insert(ETAG, val); res.headers_mut().insert(ETAG, val);
} else { } else {

View File

@ -3,7 +3,7 @@ extern crate lazy_static;
use anyhow::Result; use anyhow::Result;
use dotenv::dotenv; use dotenv::dotenv;
use http::StatusCode; use http::header::SERVER;
use hyper::server::Server; use hyper::server::Server;
use listenfd::ListenFd; use listenfd::ListenFd;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
@ -13,6 +13,7 @@ use std::convert::Infallible;
use std::env; use std::env;
use tracing_subscriber::fmt::format::FmtSpan; use tracing_subscriber::fmt::format::FmtSpan;
use url::Url; use url::Url;
use warp::http::Response;
use warp::Filter; use warp::Filter;
mod caches; mod caches;
@ -22,6 +23,7 @@ mod macros;
mod models; mod models;
mod problem; mod problem;
use handlers::SERVER_STRING;
use models::{ use models::{
ListParams, PostedInteriorRefList, PostedMerchandiseList, PostedOwner, PostedShop, ListParams, PostedInteriorRefList, PostedMerchandiseList, PostedOwner, PostedShop,
PostedTransaction, PostedTransaction,
@ -85,7 +87,7 @@ async fn main() -> Result<()> {
let status_handler = warp::path::path("status") let status_handler = warp::path::path("status")
.and(warp::path::end()) .and(warp::path::end())
.and(warp::get()) .and(warp::get())
.map(|| StatusCode::OK); // TODO: return what api versions this server supports instead .map(|| Response::builder().header(SERVER, SERVER_STRING).body("Ok"));
let get_owner_handler = warp::path("owners").and( let get_owner_handler = warp::path("owners").and(
warp::path::param() warp::path::param()
.and(warp::path::end()) .and(warp::path::end())