diff --git a/conf/sqlite_serve.conf b/conf/sqlite_serve.conf index 5ba9784..fc7b502 100644 --- a/conf/sqlite_serve.conf +++ b/conf/sqlite_serve.conf @@ -32,15 +32,15 @@ http { server { listen 8080; server_name localhost; - + 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 { } } } - diff --git a/src/nginx_helpers.rs b/src/nginx_helpers.rs index e674cf0..b141e59 100644 --- a/src/nginx_helpers.rs +++ b/src/nginx_helpers.rs @@ -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 +/// 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, @@ -54,9 +55,10 @@ fn send_response_with_content_type(request: &mut Request, body: &str, _content_t request.discard_request_body(); request.set_status(http::HTTPStatus::OK); - + // 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() {