-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from husni-zuhdi/test-axum-framework
Test axum framework
- Loading branch information
Showing
11 changed files
with
201 additions
and
101 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM rust:1.73-buster AS builder | ||
FROM rust:1.74-buster AS builder | ||
COPY . . | ||
RUN cargo build --release | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
[package] | ||
name = "cmd" | ||
version = "0.1.2" | ||
version = "0.1.3" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
internal = { path = "../internal", version = "0.1.2"} | ||
actix-web = "4.4.0" | ||
internal = { path = "../internal", version = "0.1.3"} | ||
tokio = { version = "1.0", features = ["full"] } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
use internal::{self, config::Config, handler::handler}; | ||
|
||
#[actix_web::main] | ||
#[tokio::main] | ||
async fn main() -> std::io::Result<()> { | ||
let config = Config::from_envar(); | ||
handler(config).await | ||
handler(config).await; | ||
Ok(()) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
use crate::config::Config; | ||
use crate::model::data::BlogsData; | ||
use crate::model::data::{AppState, BlogsData}; | ||
use crate::router::*; | ||
use actix_web::{middleware, web, App, HttpServer}; | ||
use axum::{ | ||
routing::{get, get_service}, | ||
Router, | ||
}; | ||
use log::info; | ||
use tower_http::services::{ServeDir, ServeFile}; | ||
|
||
pub async fn handler(cfg: Config) -> std::io::Result<()> { | ||
pub async fn handler(cfg: Config) -> () { | ||
// Initialize Logger | ||
env_logger::init_from_env(env_logger::Env::new().default_filter_or(cfg.log_level.clone())); | ||
|
||
let endpoint = cfg.svc_endpoint.as_str(); | ||
let port = cfg | ||
.svc_port | ||
.parse::<u16>() | ||
.expect("Failed to get port number"); | ||
|
||
// Setup config and blogs_data states | ||
let config = cfg.clone(); | ||
let mut blogs_data = BlogsData::default(); | ||
if !config.gh_owner.is_empty() && !config.gh_repo.is_empty() && !config.gh_branch.is_empty() { | ||
blogs_data = BlogsData::with_gh(&config.gh_owner, &config.gh_repo, &config.gh_branch).await; | ||
} | ||
let app_state = AppState { config, blogs_data }; | ||
|
||
let endpoint = format!("{}:{}", cfg.svc_endpoint, cfg.svc_port); | ||
info!("Starting HTTP Server at http://{}", endpoint); | ||
|
||
info!( | ||
"Starting HTTP Server at http://{}:{}", | ||
cfg.svc_endpoint, cfg.svc_port | ||
); | ||
// Axum Application | ||
let app = Router::new() | ||
.route("/", get(get_profile)) | ||
.route("/not-found", get(get_404_not_found)) | ||
.route("/version", get(get_version)) | ||
.route("/blogs", get(get_blogs)) | ||
.route("/blogs/:blog_id", get(get_blog)) | ||
.nest_service("/statics", get_service(ServeDir::new("./statics/favicon/"))) | ||
.nest_service( | ||
"/statics/styles.css", | ||
get_service(ServeFile::new("./statics/styles.css")), | ||
) | ||
.with_state(app_state) | ||
.fallback(get(get_404_not_found)); | ||
|
||
HttpServer::new(move || { | ||
App::new() | ||
.wrap(middleware::Logger::default()) | ||
.app_data(web::Data::new(blogs_data.clone())) | ||
.app_data(web::Data::new(config.clone())) | ||
.service(web::resource("/").route(web::get().to(profile))) | ||
.service(web::resource("/statics/{static_file}").route(web::get().to(statics))) | ||
.service(web::resource("/blogs").route(web::get().to(get_blogs))) | ||
.service(web::resource("/blogs/{blogid}").route(web::get().to(get_blog))) | ||
.service(web::resource("/version").route(web::get().to(get_version))) | ||
.service(web::resource("/not-found").route(web::get().to(get_404_not_found))) | ||
}) | ||
.bind((endpoint, port)) | ||
.expect("Failed to start Http Server") | ||
.run() | ||
.await | ||
// Start Axum Application | ||
let listener = tokio::net::TcpListener::bind(endpoint).await.unwrap(); | ||
axum::serve(listener, app).await.unwrap(); | ||
} |
This file contains 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
This file contains 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
This file contains 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
File renamed without changes.
Oops, something went wrong.