chore: set content-type in the module
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Has been cancelled

This commit is contained in:
Edward Langley
2025-11-15 19:08:44 -08:00
parent 4cae73faf6
commit a92a67eaa2
2 changed files with 15 additions and 18 deletions

View File

@ -36,11 +36,11 @@ http {
root "server_root";
charset utf-8;
# Serve static CSS file
location /static/ {
add_header "Content-Type" "text/css; charset=utf-8";
add_header "Cache-Control" "public, max-age=31536000";
}
# # Serve static CSS file
# location /static/ {
# add_header "Content-Type" "text/css; charset=utf-8";
# add_header "Cache-Control" "public, max-age=31536000";
# }
# Homepage - Redirect to catalog
location = / {
@ -49,7 +49,7 @@ http {
# Browse all books
location = /books {
add_header "Content-Type" "text/html; charset=utf-8";
# add_header "Content-Type" "text/html; charset=utf-8";
add_header "Cache-Control" "public, max-age=300";
sqlite_db "book_catalog.db";
sqlite_query "SELECT * FROM books ORDER BY rating DESC, title";
@ -58,7 +58,7 @@ http {
# Book detail by ID (named parameter)
location = /book {
add_header "Content-Type" "text/html; charset=utf-8";
# add_header "Content-Type" "text/html; charset=utf-8";
add_header "Cache-Control" "public, max-age=300";
sqlite_db "book_catalog.db";
sqlite_query "SELECT * FROM books WHERE id = :book_id";
@ -68,7 +68,6 @@ http {
# Filter by genre (named parameter)
location = /genre {
add_header "Content-Type" "text/html; charset=utf-8";
add_header "Cache-Control" "public, max-age=300";
sqlite_db "book_catalog.db";
sqlite_query "SELECT * FROM books WHERE genre = :genre ORDER BY rating DESC, title";
@ -78,7 +77,6 @@ http {
# Search by title (named parameter with LIKE)
location = /search {
add_header "Content-Type" "text/html; charset=utf-8";
sqlite_db "book_catalog.db";
sqlite_query "SELECT * FROM books WHERE title LIKE '%' || :q || '%' ORDER BY rating DESC";
sqlite_param :q $arg_q;
@ -87,7 +85,6 @@ http {
# Filter by minimum rating (named parameter)
location = /top {
add_header "Content-Type" "text/html; charset=utf-8";
sqlite_db "book_catalog.db";
sqlite_query "SELECT * FROM books WHERE rating >= :min_rating ORDER BY rating DESC, title";
sqlite_param :min_rating $arg_min;
@ -96,7 +93,6 @@ http {
# Year range filter (multiple named parameters)
location = /era {
add_header "Content-Type" "text/html; charset=utf-8";
sqlite_db "book_catalog.db";
sqlite_query "SELECT * FROM books WHERE year >= :from AND year <= :to ORDER BY year DESC, title";
sqlite_param :from $arg_from;
@ -111,4 +107,3 @@ http {
}
}
}

View File

@ -1,5 +1,6 @@
//! NGINX-specific helper functions
use crate::content_type::ContentType;
use crate::logging;
use ngx::core::Buffer;
use ngx::ffi::ngx_chain_t;
@ -28,16 +29,16 @@ pub fn get_doc_root_and_uri(request: &mut Request) -> Result<(String, String), S
/// Send HTML response
pub fn send_response(request: &mut Request, body: &str) -> Status {
send_response_with_content_type(request, body, "text/html; charset=utf-8")
send_response_with_content_type(request, body, &ContentType::Html)
}
/// Send JSON response
pub fn send_json_response(request: &mut Request, body: &str) -> Status {
send_response_with_content_type(request, body, "application/json; charset=utf-8")
send_response_with_content_type(request, body, &ContentType::Json)
}
/// Create and send nginx response buffer with specified content type
fn send_response_with_content_type(request: &mut Request, body: &str, _content_type: &str) -> Status {
fn send_response_with_content_type(request: &mut Request, body: &str, content_type: &ContentType) -> Status {
// Create output buffer
let mut buf = match request.pool().create_buffer_from_str(body) {
Some(buf) => buf,
@ -57,6 +58,7 @@ fn send_response_with_content_type(request: &mut Request, body: &str, _content_t
// Set content type (nginx will handle it based on add_header in config or auto-detection)
// For now, we rely on nginx config to set Content-Type via add_header directive
request.add_header_out("Content-Type", content_type.content_type_header());
let rc = request.send_header();
if rc == Status::NGX_ERROR || rc > Status::NGX_OK || request.header_only() {