22mod client;
33mod render;
44mod routes;
5+ mod settings;
56
67use std:: collections:: HashMap ;
78
@@ -12,7 +13,9 @@ use routes::{
1213 article:: render_article,
1314 internet_news:: render_legacy_article,
1415 markets:: render_market,
16+ proxy:: image_proxy,
1517 search:: { render_search, render_section, render_topic} ,
18+ settings:: handle_settings,
1619} ;
1720
1821const CSS : & str = include_str ! ( concat!( env!( "OUT_DIR" ) , "/main.css" ) ) ;
@@ -24,7 +27,7 @@ macro_rules! document {
2427 html lang="en" {
2528 head {
2629 title { ( $title) }
27- link rel="stylesheet" href="/main.css?v=0 " ;
30+ link rel="stylesheet" href="/main.css?v=1 " ;
2831 meta name="viewport" content="width=device-width, initial-scale=1" ;
2932 $( ( $head) ) ?
3033 }
@@ -35,13 +38,16 @@ macro_rules! document {
3538 " - "
3639 a href="/search" { "Search" }
3740 " - "
41+ a href="/settings" { "Settings" }
42+ " - "
3843 a href="/about" { "About" } } }
3944 }
4045 }
4146 }
4247 } ;
4348}
4449pub ( crate ) use document;
50+ use settings:: Settings ;
4551
4652pub struct Section {
4753 id : String ,
@@ -133,12 +139,15 @@ fn main() {
133139 . as_deref ( )
134140 . unwrap_or_default ( )
135141 . iter ( )
136- . map ( |s| SectionChild { id : s. id . clone ( ) , name : s. name . clone ( ) } )
142+ . map ( |s| SectionChild {
143+ id : s. id . clone ( ) ,
144+ name : s. name . clone ( ) ,
145+ } )
137146 . collect ( ) ;
138147 for child in section. children . unwrap_or_default ( ) {
139148 queue. push ( child) ;
140149 }
141- let node= Section {
150+ let node = Section {
142151 id : section. id . clone ( ) ,
143152 name : section. name . clone ( ) ,
144153 children,
@@ -151,6 +160,8 @@ fn main() {
151160 println ! ( "Listening on http://{}" , list_address) ;
152161 rouille:: start_server ( list_address, move |request| {
153162 let path = request. url ( ) ;
163+ let settings = Settings :: from_request ( request) ;
164+
154165 let response = match path. as_str ( ) {
155166 "/" | "/home" | "/world/" => {
156167 let offset = request
@@ -159,10 +170,11 @@ fn main() {
159170 let section = sections_by_id
160171 . get ( "/world/" )
161172 . unwrap_or_else ( || panic ! ( "Section 'world' not found" ) ) ;
162-
173+
163174 render_section ( & client, section, offset, 8 )
164175 }
165176 "/about" => render_about ( ) ,
177+ "/settings" => return handle_settings ( request, & settings) ,
166178 "/search" | "/search/" => render_search ( & client, request) ,
167179 "/main.css" => {
168180 return rouille:: Response {
@@ -213,23 +225,26 @@ fn main() {
213225 render_market ( & client, path)
214226 } else if let Some ( path) = path. strip_prefix ( "/markets/companies/" ) {
215227 render_market ( & client, path)
228+ } else if let Some ( path) = request. raw_url ( ) . strip_prefix ( "/proxy/" ) {
229+ return image_proxy ( & client, request, path) ;
216230 } else {
217- render_article ( & client, & path)
231+ render_article ( & client, & path, & settings )
218232 }
219233 }
220234 } ;
221235
222236 match response {
223237 Ok ( body) => rouille:: Response :: html ( body) ,
224- Err ( err) => render_api_error ( & err, & path) ,
238+ Err ( err) => render_api_error ( & err, & path, & settings ) ,
225239 }
226240 } ) ;
227241}
228242
229- fn render_api_error ( err : & ApiError , path : & str ) -> rouille:: Response {
243+ fn render_api_error ( err : & ApiError , path : & str , settings : & Settings ) -> rouille:: Response {
230244 let ( status, title) = match err {
231- ApiError :: Empty |
232- ApiError :: External ( 404 , _) => ( 404 , "404 - Content not found" . to_string ( ) ) ,
245+ ApiError :: Empty | ApiError :: External ( 404 , _) => {
246+ ( 404 , "404 - Content not found" . to_string ( ) )
247+ }
233248 ApiError :: Redirect ( _, _) => ( 200 , format ! ( "Redirect found" ) ) ,
234249 ApiError :: External ( code, _) => ( * code, format ! ( "{code} - External error" ) ) ,
235250 ApiError :: Internal ( message) => ( 500 , format ! ( "500 - Internal server error {message}" ) ) ,
@@ -241,21 +256,23 @@ fn render_api_error(err: &ApiError, path: &str) -> rouille::Response {
241256 let location = strip_prefix ( location) ;
242257 (
243258 maud:: html! {
244- meta http-equiv="refresh" content=( format!( "10 ; url={}" , location) ) ;
259+ meta http-equiv="refresh" content=( format!( "{} ; url={}" , settings . redirect_timer , location) ) ;
245260 link rel="canonical" href=( location) ;
246261 } ,
247262 maud:: html! {
248- p { "Redirecting to " ( location) " in 10 seconds. Or click " a href=( location) { "here" } " to follow the link directly." }
249- }
250- ) } ,
263+ p { "Redirecting to " ( location) " in " ( settings. redirect_timer) " seconds. Or click " a href=( location) { "here" } " to follow the link directly." }
264+ } ,
265+ )
266+ }
251267 ApiError :: External ( _, message) => (
252268 maud:: html!( ) ,
253269 maud:: html! {
254270 details {
255271 summary { "Server response" }
256272 p { ( message) }
257273 }
258- } ) ,
274+ } ,
275+ ) ,
259276 ApiError :: Internal ( _) => ( maud:: html!( ) , maud:: html!( ) ) ,
260277 } ;
261278
0 commit comments