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

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

View File

@ -1,5 +1,6 @@
//! NGINX-specific helper functions //! NGINX-specific helper functions
use crate::content_type::ContentType;
use crate::logging; use crate::logging;
use ngx::core::Buffer; use ngx::core::Buffer;
use ngx::ffi::ngx_chain_t; 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 /// Send HTML response
pub fn send_response(request: &mut Request, body: &str) -> Status { 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 /// Send JSON response
pub fn send_json_response(request: &mut Request, body: &str) -> Status { 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 /// 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 // Create output buffer
let mut buf = match request.pool().create_buffer_from_str(body) { let mut buf = match request.pool().create_buffer_from_str(body) {
Some(buf) => buf, Some(buf) => buf,
@ -54,9 +55,10 @@ fn send_response_with_content_type(request: &mut Request, body: &str, _content_t
request.discard_request_body(); request.discard_request_body();
request.set_status(http::HTTPStatus::OK); request.set_status(http::HTTPStatus::OK);
// Set content type (nginx will handle it based on add_header in config or auto-detection) // 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 // 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(); let rc = request.send_header();
if rc == Status::NGX_ERROR || rc > Status::NGX_OK || request.header_only() { if rc == Status::NGX_ERROR || rc > Status::NGX_OK || request.header_only() {