@@ -2,17 +2,18 @@ use std::net::SocketAddr;
22use std:: time:: Duration ;
33use axum:: response:: { Html , IntoResponse , Response } ;
44use axum:: { Json , Router } ;
5+ use axum:: http:: { header, Request , StatusCode } ;
56use axum:: middleware:: Next ;
67use axum:: routing:: get;
7- use http:: { header, Request , StatusCode } ;
8+ // use http::{header, Request, StatusCode};
89use log:: { error, info} ;
910use serde_json:: json;
1011use sqlx:: postgres:: PgPoolOptions ;
1112use utoipa:: OpenApi ;
1213use utoipa_rapidoc:: RapiDoc ;
1314use crate :: errors:: ErrResponse ;
1415use crate :: conf:: Configuration ;
15- use crate :: example;
16+ use crate :: { example, services } ;
1617use crate :: redis:: { RedisHolder , RedisSession } ;
1718
1819#[ derive( Clone ) ]
@@ -34,7 +35,7 @@ pub async fn init(cfg: &Configuration) {
3435 ru_di:: Di :: register ( move |_| {
3536 pool_inner. clone ( )
3637 } ) ;
37- example :: init ( ) . await ;
38+ services :: init ( ) . await ;
3839}
3940
4041pub async fn start_serve ( cfg : & Configuration ) {
@@ -50,18 +51,13 @@ pub async fn start_serve(cfg: &Configuration) {
5051 . nest ( "/example" , example:: handlers:: router ( state. clone ( ) ) )
5152 . layer ( axum:: middleware:: from_fn ( log_request) ) ;
5253 info ! ( "server will start at 0.0.0.0:{}" , cfg. port) ;
53- let serv = axum:: Server :: bind ( & SocketAddr :: from ( ( [ 0 , 0 , 0 , 0 ] , cfg. port ) ) )
54- . serve ( app. into_make_service ( ) )
55- . await ;
56- match serv {
57- Ok ( _) =>{
54+ if let Ok ( listener) = tokio:: net:: TcpListener :: bind ( format ! ( "0.0.0.0:{}" , cfg. port) . as_str ( ) ) . await {
55+ if let Err ( e) = axum:: serve ( listener, app) . await {
56+ error ! ( "server fault with err:{}" , e) ;
57+ } else {
5858 info ! ( "server stopped normally" ) ;
5959 }
60- Err ( err) =>{
61- error ! ( "server fault with err:{}" , err) ;
62- }
6360 }
64-
6561}
6662
6763async fn not_found ( ) -> Html < String > {
@@ -71,16 +67,16 @@ async fn status()->impl IntoResponse {
7167 Json ( json ! ( { "status" : "it works!" } ) )
7268}
7369
74- async fn log_request < B > ( req : Request < B > , next : Next < B > ) -> impl IntoResponse {
70+ async fn log_request ( req : Request < axum :: body :: Body > , next : Next ) -> Response {
7571 info ! ( "{} {}" , req. method( ) , req. uri( ) ) ;
76- next. run ( req) . await . into_response ( )
72+ next. run ( req) . await
7773}
7874
7975async fn redoc_js ( ) -> Response {
8076 Response :: builder ( )
8177 . status ( StatusCode :: OK )
8278 . header ( header:: CONTENT_TYPE , "application/javascript" )
83- . body ( axum:: body:: boxed ( axum :: body :: Full :: from ( include_str ! ( "../templates/rapidoc-min.js" ) ) ) ) . unwrap ( )
79+ . body ( axum:: body:: Body :: new :: < String > ( include_str ! ( "../templates/rapidoc-min.js" ) . to_string ( ) ) ) . unwrap ( )
8480}
8581
8682async fn redoc_ui ( ) -> Html < String > {
0 commit comments