22static GLOBAL : mimalloc:: MiMalloc = mimalloc:: MiMalloc ;
33
44use khttp:: { Headers , Method :: * , RequestContext , ResponseHandle , Server , Status } ;
5- use std:: { ffi:: CStr , io, ptr} ;
5+ use std:: { ffi:: CStr , io, ptr, sync :: LazyLock } ;
66use yarte:: { Serialize , ywrite_html} ;
77
88#[ derive( Serialize ) ]
99struct HelloMessage {
1010 message : & ' static str ,
1111}
1212
13+ static JSON_HEADERS : LazyLock < Headers < ' static > > = LazyLock :: new ( || {
14+ let mut headers = Headers :: new ( ) ;
15+ headers. add ( Headers :: CONTENT_TYPE , b"application/json" ) ;
16+ headers. add ( "server" , b"khttp" ) ;
17+ headers
18+ } ) ;
19+
1320fn main ( ) {
1421 let mut app = Server :: builder ( "0.0.0.0:8080" ) . unwrap ( ) ;
1522
1623 app. route ( Get , "/json" , |_ctx, res| {
17- // headers
18- let mut headers = Headers :: new ( ) ;
19- headers. add ( Headers :: CONTENT_TYPE , b"application/json" ) ;
20- headers. add ( "server" , b"khttp" ) ;
21-
2224 // body
2325 let msg = HelloMessage {
2426 message : "Hello, World!" ,
@@ -27,7 +29,7 @@ fn main() {
2729 msg. to_bytes_mut ( & mut buf) ;
2830
2931 // response
30- res. ok ( & headers , buf)
32+ res. ok ( & JSON_HEADERS , buf)
3133 } ) ;
3234
3335 app. route ( Get , "/fortunes" , handle_fortunes) ;
0 commit comments