Skip to content

Commit a5d915a

Browse files
authored
Merge pull request #433 from namada-net/headers
added cache headers
2 parents 4bae2c8 + 735d71d commit a5d915a

7 files changed

Lines changed: 65 additions & 17 deletions

File tree

webserver/src/entity/governance.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ impl Proposal {
193193
.expect("Should be a number"),
194194
}
195195
}
196+
197+
pub fn activated(&self) -> bool {
198+
matches!(
199+
self.status,
200+
ProposalStatus::ExecutedPassed | ProposalStatus::ExecutedRejected
201+
)
202+
}
196203
}
197204

198205
impl From<GovernanceProposalVoteDb> for ProposalVote {

webserver/src/handler/block.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,50 @@ use axum_macros::debug_handler;
55

66
use crate::error::api::ApiError;
77
use crate::response::block::BlockResponse;
8+
use crate::response::headers;
89
use crate::state::common::CommonState;
910

1011
#[debug_handler]
1112
pub async fn get_block_by_height(
1213
_headers: HeaderMap,
1314
Path(value): Path<i32>,
1415
State(state): State<CommonState>,
15-
) -> Result<Json<BlockResponse>, ApiError> {
16+
) -> Result<(HeaderMap, Json<BlockResponse>), ApiError> {
1617
let (block, prev_block, transactions) =
1718
state.block_service.get_block_by_height(value).await?;
1819

1920
let response = BlockResponse::from(block, prev_block, transactions);
21+
let headers = headers::with_cache();
2022

21-
Ok(Json(response))
23+
Ok((headers, Json(response)))
2224
}
2325

2426
#[debug_handler]
2527
pub async fn get_block_by_timestamp(
2628
_headers: HeaderMap,
2729
Path(value): Path<i64>,
2830
State(state): State<CommonState>,
29-
) -> Result<Json<BlockResponse>, ApiError> {
31+
) -> Result<(HeaderMap, Json<BlockResponse>), ApiError> {
3032
let (block, prev_block, transactions) =
3133
state.block_service.get_block_by_timestamp(value).await?;
3234

3335
let response = BlockResponse::from(block, prev_block, transactions);
36+
let headers = headers::with_cache();
3437

35-
Ok(Json(response))
38+
Ok((headers, Json(response)))
3639
}
3740

3841
#[debug_handler]
3942
pub async fn get_block_by_hash(
4043
_headers: HeaderMap,
4144
Path(value): Path<String>,
4245
State(state): State<CommonState>,
43-
) -> Result<Json<BlockResponse>, ApiError> {
46+
) -> Result<(HeaderMap, Json<BlockResponse>), ApiError> {
4447
let (block, prev_block, transactions) =
4548
state.block_service.get_block_by_hash(value).await?;
4649

4750
let response = BlockResponse::from(block, prev_block, transactions);
51+
let headers = headers::with_cache();
4852

49-
Ok(Json(response))
53+
Ok((headers, Json(response)))
5054
}

webserver/src/handler/governance.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::error::governance::GovernanceError;
99
use crate::response::governance::{
1010
ProposalDataResponse, ProposalResponse, ProposalVoteResponse,
1111
};
12+
use crate::response::headers;
1213
use crate::response::utils::PaginatedResponse;
1314
use crate::state::common::CommonState;
1415

@@ -47,14 +48,19 @@ pub async fn get_governance_proposal_by_id(
4748
_headers: HeaderMap,
4849
Path(proposal_id): Path<u64>,
4950
State(state): State<CommonState>,
50-
) -> Result<Json<ProposalResponse>, ApiError> {
51+
) -> Result<(HeaderMap, Json<ProposalResponse>), ApiError> {
5152
let proposal = state
5253
.gov_service
5354
.find_governance_proposal_by_id(proposal_id)
5455
.await?;
5556

5657
if let Some(proposal) = proposal {
57-
Ok(Json(ProposalResponse::from(proposal)))
58+
let headers = if proposal.activated() {
59+
headers::with_cache()
60+
} else {
61+
headers::without_cache()
62+
};
63+
Ok((headers, Json(ProposalResponse::from(proposal))))
5864
} else {
5965
Err(GovernanceError::NotFound(proposal_id).into())
6066
}
@@ -65,12 +71,13 @@ pub async fn get_proposal_data_by_proposal_id(
6571
_headers: HeaderMap,
6672
Path(proposal_id): Path<u64>,
6773
State(state): State<CommonState>,
68-
) -> Result<Json<ProposalDataResponse>, ApiError> {
74+
) -> Result<(HeaderMap, Json<ProposalDataResponse>), ApiError> {
6975
let proposal = state.gov_service.find_proposal_data(proposal_id).await?;
7076

7177
let response = ProposalDataResponse::from(proposal);
78+
let headers = headers::with_cache();
7279

73-
Ok(Json(response))
80+
Ok((headers, Json(response)))
7481
}
7582

7683
#[debug_handler]

webserver/src/handler/pk.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use axum::http::HeaderMap;
44
use axum_macros::debug_handler;
55

66
use crate::error::api::ApiError;
7+
use crate::response::headers;
78
use crate::response::revealed_pk::RevealedPkResponse;
89
use crate::state::common::CommonState;
910

@@ -12,15 +13,20 @@ pub async fn get_revealed_pk(
1213
_headers: HeaderMap,
1314
Path(address): Path<String>,
1415
State(state): State<CommonState>,
15-
) -> Result<Json<RevealedPkResponse>, ApiError> {
16+
) -> Result<(HeaderMap, Json<RevealedPkResponse>), ApiError> {
1617
let revealed_pk = state
1718
.revealed_pk_service
1819
.get_revealed_pk_by_address(&state.client, address)
1920
.await?;
2021

2122
let response = RevealedPkResponse {
22-
public_key: revealed_pk.public_key,
23+
public_key: revealed_pk.public_key.clone(),
24+
};
25+
let headers = if revealed_pk.public_key.is_some() {
26+
headers::with_cache()
27+
} else {
28+
headers::without_cache()
2329
};
2430

25-
Ok(Json(response))
31+
Ok((headers, Json(response)))
2632
}

webserver/src/handler/transaction.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::dto::transaction::{
1010
};
1111
use crate::error::api::ApiError;
1212
use crate::error::transaction::TransactionError;
13+
use crate::response::headers;
1314
use crate::response::transaction::{
1415
InnerTransactionResponse, TransactionHistoryResponse,
1516
WrapperTransactionResponse,
@@ -22,7 +23,7 @@ pub async fn get_wrapper_tx(
2223
_headers: HeaderMap,
2324
Path(tx_id): Path<TransactionIdParam>,
2425
State(state): State<CommonState>,
25-
) -> Result<Json<Option<WrapperTransactionResponse>>, ApiError> {
26+
) -> Result<(HeaderMap, Json<Option<WrapperTransactionResponse>>), ApiError> {
2627
tx_id.is_valid_hash()?;
2728

2829
let tx_id = tx_id.get();
@@ -43,16 +44,17 @@ pub async fn get_wrapper_tx(
4344

4445
let response = wrapper_tx
4546
.map(|wrapper| WrapperTransactionResponse::new(wrapper, inner_txs));
47+
let headers = headers::with_cache();
4648

47-
Ok(Json(response))
49+
Ok((headers, Json(response)))
4850
}
4951

5052
#[debug_handler]
5153
pub async fn get_inner_tx(
5254
_headers: HeaderMap,
5355
Path(tx_id): Path<TransactionIdParam>,
5456
State(state): State<CommonState>,
55-
) -> Result<Json<Option<InnerTransactionResponse>>, ApiError> {
57+
) -> Result<(HeaderMap, Json<Option<InnerTransactionResponse>>), ApiError> {
5658
tx_id.is_valid_hash()?;
5759

5860
let tx_id = tx_id.get();
@@ -63,8 +65,9 @@ pub async fn get_inner_tx(
6365
.await?;
6466

6567
let response = inner_tx.map(InnerTransactionResponse::new);
68+
let headers = headers::with_cache();
6669

67-
Ok(Json(response))
70+
Ok((headers, Json(response)))
6871
}
6972

7073
#[debug_handler]

webserver/src/response/headers.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use axum::http::header::CACHE_CONTROL;
2+
use axum::http::{HeaderMap, HeaderValue};
3+
4+
pub fn with_cache() -> HeaderMap {
5+
let mut headers = HeaderMap::new();
6+
headers.insert(
7+
CACHE_CONTROL,
8+
HeaderValue::from_static("public, max-age=31536000, immutable"),
9+
);
10+
headers
11+
}
12+
13+
pub fn without_cache() -> HeaderMap {
14+
let mut headers = HeaderMap::new();
15+
headers.insert(
16+
CACHE_CONTROL,
17+
HeaderValue::from_static("no-cache, no-store, must-revalidate"),
18+
);
19+
headers
20+
}

webserver/src/response/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod chain;
55
pub mod crawler_state;
66
pub mod gas;
77
pub mod governance;
8+
pub mod headers;
89
pub mod ibc;
910
pub mod masp;
1011
pub mod pgf;

0 commit comments

Comments
 (0)