Last active
November 7, 2021 16:46
-
-
Save jayhuang75/56006ea06601d5f39705f6f1d6b13cb5 to your computer and use it in GitHub Desktop.
go train delay rust api appstate init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| impl AppState { | |
| pub async fn new() -> Result<Self, AppError> { | |
| let database = Database::new().await?; | |
| Ok(AppState { | |
| db: database.pool, | |
| }) | |
| } | |
| pub async fn get_srv(self) -> Result<Server, AppError> { | |
| let mut listenfd = ListenFd::from_env(); | |
| let address = format!("{}:{}", "0.0.0.0", env::var("PORT").expect("missing PORT env")); | |
| let mut server = HttpServer::new(move || { | |
| App::new() | |
| .wrap(HttpAuthentication::bearer(ok_validator)) | |
| .wrap( | |
| Cors::permissive() | |
| .allow_any_origin() // this will set in the config. | |
| .allowed_methods(vec!["GET", "POST"]) | |
| .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) | |
| .allowed_header(header::CONTENT_TYPE) | |
| .max_age(3600), | |
| ) | |
| .wrap(middleware::Logger::new( | |
| "%a %r %s %b %{Referer}i %{User-Agent}i %D", | |
| )) | |
| .app_data(web::Data::new(self.clone())) | |
| .configure(routes_conf) | |
| }); | |
| server = match listenfd.take_tcp_listener(0)? { | |
| Some(listener) => server.listen(listener)?, | |
| None => server.bind(&address)?, | |
| }; | |
| Ok(server.run()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment