Auto-indexing service and GraphQL API for AT Protocol Records
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix priv serving for client

+18 -42
+18 -42
server/src/server.gleam
··· 276 276 ) { 277 277 wisp.configure_logger() 278 278 279 + // Get priv directory for serving static files 280 + let assert Ok(priv_directory) = wisp.priv_directory("server") 281 + let static_directory = priv_directory <> "/static" 282 + 279 283 // Get secret_key_base from environment or generate one 280 284 let secret_key_base = case envoy.get("SECRET_KEY_BASE") { 281 285 Ok(key) -> { ··· 472 476 jetstream_consumer: jetstream_subject, 473 477 ) 474 478 475 - let handler = fn(req) { handle_request(req, ctx) } 479 + let handler = fn(req) { handle_request(req, ctx, static_directory) } 476 480 477 481 logging.log( 478 482 logging.Info, ··· 532 536 process.sleep_forever() 533 537 } 534 538 535 - fn handle_request(req: wisp.Request, ctx: Context) -> wisp.Response { 536 - use _req <- middleware(req) 539 + fn handle_request( 540 + req: wisp.Request, 541 + ctx: Context, 542 + static_directory: String, 543 + ) -> wisp.Response { 544 + use _req <- middleware(req, static_directory) 537 545 538 546 let segments = wisp.path_segments(req) 539 547 540 548 case segments { 541 549 [] -> index_route(req, ctx) 542 550 ["health"] -> handle_health_check(ctx) 543 - // Serve client static files (bundled by lustre dev tools) 544 - ["quickslice_client.js"] -> serve_static_file(["quickslice_client.js"]) 545 - ["styles.css"] -> serve_static_file(["styles.css"]) 546 551 ["oauth", "authorize"] -> 547 552 handlers.handle_oauth_authorize(req, ctx.db, ctx.oauth_config) 548 553 ["oauth", "callback"] -> ··· 727 732 728 733 fn index_route(_req: wisp.Request, _ctx: Context) -> wisp.Response { 729 734 // Serve the client SPA's index.html (bundled by lustre dev tools) 730 - case simplifile.read("priv/static/index.html") { 735 + // The priv directory is resolved at startup and passed through the handler 736 + let assert Ok(priv_dir) = wisp.priv_directory("server") 737 + let index_path = priv_dir <> "/static/index.html" 738 + 739 + case simplifile.read(index_path) { 731 740 Ok(contents) -> wisp.html_response(contents, 200) 732 741 Error(_) -> 733 742 wisp.html_response( ··· 737 746 } 738 747 } 739 748 740 - fn serve_static_file(path_segments: List(String)) -> wisp.Response { 741 - let file_path = "priv/static/" <> string.join(path_segments, "/") 742 - 743 - case simplifile.read(file_path) { 744 - Ok(contents) -> { 745 - // Determine content type based on file extension 746 - let content_type = case list.last(path_segments) { 747 - Ok(filename) -> { 748 - case 749 - string.ends_with(filename, ".mjs") 750 - || string.ends_with(filename, ".js") 751 - { 752 - True -> "application/javascript" 753 - False -> 754 - case string.ends_with(filename, ".css") { 755 - True -> "text/css" 756 - False -> 757 - case string.ends_with(filename, ".html") { 758 - True -> "text/html" 759 - False -> "application/octet-stream" 760 - } 761 - } 762 - } 763 - } 764 - Error(_) -> "application/octet-stream" 765 - } 766 - 767 - wisp.response(200) 768 - |> wisp.set_header("content-type", content_type) 769 - |> wisp.set_body(wisp.Text(contents)) 770 - } 771 - Error(_) -> wisp.response(404) |> wisp.set_body(wisp.Text("Not found")) 772 - } 773 - } 774 - 775 749 fn middleware( 776 750 req: wisp.Request, 751 + static_directory: String, 777 752 handle_request: fn(wisp.Request) -> wisp.Response, 778 753 ) -> wisp.Response { 779 754 use <- wisp.rescue_crashes 780 755 use <- wisp.log_request(req) 781 756 use req <- wisp.handle_head(req) 757 + use <- wisp.serve_static(req, under: "/", from: static_directory) 782 758 783 759 // Get origin from request headers 784 760 let origin = case request.get_header(req, "origin") {