1+ #[ global_allocator]
2+ static GLOBAL : mimalloc:: MiMalloc = mimalloc:: MiMalloc ;
3+
14use khttp:: { Headers , Method :: * , RequestContext , ResponseHandle , Server , Status } ;
2- use std:: { ffi:: CStr , io, ptr} ;
5+ use pq_sys:: {
6+ ConnStatusType , ExecStatusType , PGconn , PQclear , PQconnectdb , PQerrorMessage , PQexecPrepared ,
7+ PQfinish , PQgetlength , PQgetvalue , PQntuples , PQprepare , PQresultStatus , PQstatus ,
8+ } ;
9+ use std:: { ffi:: CStr , io, ptr, sync:: LazyLock } ;
310use yarte:: { Serialize , ywrite_html} ;
411
512#[ derive( Serialize ) ]
613struct HelloMessage {
714 message : & ' static str ,
815}
916
17+ static JSON_HEADERS : LazyLock < Headers < ' static > > = LazyLock :: new ( || {
18+ let mut headers = Headers :: new ( ) ;
19+ headers. add ( Headers :: CONTENT_TYPE , b"application/json" ) ;
20+ headers. add ( "server" , b"khttp" ) ;
21+ headers
22+ } ) ;
23+
1024fn main ( ) {
1125 let mut app = Server :: builder ( "0.0.0.0:8080" ) . unwrap ( ) ;
1226
13- app. route ( Get , "/plaintext" , |_ctx, res| {
14- // headers
15- let mut headers = Headers :: new ( ) ;
16- headers. add ( Headers :: CONTENT_TYPE , b"text/plain" ) ;
17- headers. add ( "server" , b"khttp" ) ;
18-
19- // response
20- res. ok ( & headers, "Hello, World!" )
21- } ) ;
22-
2327 app. route ( Get , "/json" , |_ctx, res| {
24- // headers
25- let mut headers = Headers :: new ( ) ;
26- headers. add ( Headers :: CONTENT_TYPE , b"application/json" ) ;
27- headers. add ( "server" , b"khttp" ) ;
28-
2928 // body
3029 let msg = HelloMessage {
3130 message : "Hello, World!" ,
@@ -34,7 +33,7 @@ fn main() {
3433 msg. to_bytes_mut ( & mut buf) ;
3534
3635 // response
37- res. ok ( & headers , buf)
36+ res. ok ( & JSON_HEADERS , buf)
3837 } ) ;
3938
4039 app. route ( Get , "/fortunes" , handle_fortunes) ;
@@ -63,11 +62,6 @@ fn handle_fortunes(_ctx: RequestContext, res: &mut ResponseHandle) -> io::Result
6362// /fortunes query implementation using postgres (libpq)
6463// ---------------------------------------------------------------------
6564
66- use pq_sys:: {
67- ConnStatusType , ExecStatusType , PGconn , PQclear , PQconnectdb , PQerrorMessage , PQexecPrepared ,
68- PQfinish , PQgetlength , PQgetvalue , PQntuples , PQprepare , PQresultStatus , PQstatus ,
69- } ;
70-
7165const DB_CONNINFO : & CStr = c"postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world" ;
7266const PG_FORTUNES_SQL : & CStr = c"SELECT id, message FROM fortune" ;
7367const PG_FORTUNES_PREPARED_STMT : & CStr = c"s_fortunes" ;
@@ -78,7 +72,7 @@ struct Fortune<'a> {
7872 message : & ' a str ,
7973}
8074
81- fn fetch_fortunes_html ( ) -> Result < Vec < u8 > , String > {
75+ fn fetch_fortunes_html ( ) -> Result < Vec < u8 > , & ' static str > {
8276 PG_CONN . with ( |pg| unsafe {
8377 let res = PQexecPrepared (
8478 pg. conn ,
@@ -90,11 +84,11 @@ fn fetch_fortunes_html() -> Result<Vec<u8>, String> {
9084 1 , // resultFormat = 1 (binary)
9185 ) ;
9286 if res. is_null ( ) {
93- return Err ( "PQexecPrepared returned null" . to_owned ( ) ) ;
87+ return Err ( "PQexecPrepared returned null" ) ;
9488 }
9589 if PQresultStatus ( res) != ExecStatusType :: PGRES_TUPLES_OK {
9690 PQclear ( res) ;
97- return Err ( "PQexecPrepared non-ok result status" . to_owned ( ) ) ;
91+ return Err ( "PQexecPrepared non-ok result status" ) ;
9892 }
9993
10094 let rows = PQntuples ( res) ;
@@ -164,7 +158,6 @@ impl PgConnection {
164158 PQfinish ( conn) ;
165159 panic ! ( "PQprepare returned null" ) ;
166160 }
167-
168161 let st = PQresultStatus ( res) ;
169162 PQclear ( res) ;
170163 if st != ExecStatusType :: PGRES_COMMAND_OK {
0 commit comments