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

View File

@ -2,7 +2,7 @@ use std::marker::PhantomData;
use std::str::FromStr;
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_api_problem::HttpApiProblem;
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::Environment;
pub static SERVER_STRING: &str = "BazaarRealmAPI/0.1.0";
#[instrument(level = "debug", skip(env, api_key))]
pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i32> {
if let Some(api_key) = api_key {
@ -75,6 +77,8 @@ impl Reply for ETagReply<Json> {
let mut res = Response::new(self.body.into());
res.headers_mut()
.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) {
res.headers_mut().insert(ETAG, val);
} else {
@ -113,6 +117,8 @@ impl Reply for ETagReply<Bincode> {
CONTENT_TYPE,
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) {
res.headers_mut().insert(ETAG, val);
} else {

View File

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