웹프레임워크 뻔한 기능들 정리

웹서비스를 하는데 필요한 백엔드 기능은 약 20가지 정도로 정리될 수 있다.
각 언어별로 이 기능들을 정리해 놓으면 거의 모든 언어를 이용하여 개발할 수 있다.

1. 개발환경 및 배포환경

build(cargo)

빌드툴이라고해야하나? 번들러라고 해야하나? 모두하는 기능을 갖춘 cargo 라는 프로그램을 이용한다.

// 프로젝트 생성
  cargo new hello_world --bin 

// 빌드하기
  cargo build
  
// 실행하기 (서버 실행하기느 등)
  cargo run

// 변경후 간략하게 변경된 사항의 에러만 검사하기
// 프로젝트가 커지면 check 만해서 모든 파일 빌드하지 않고 변경된 사항만 채크해서 
// 빠른 개발 지원
  cargo check
  
// node 번들러들이 실시간 라이브 상태확인 기능처럼
// 파일 수정후 저장하면 변경사항 실시간 반영
  cargo watch -x run

CI/CD

CI 통합은 gitlab 이나 github 등 외부 프로그램 사용함.

CD 배포는 docker + jenkins 이용하며 빌드 및 실행함.

무중단 배포

무중단 및 스케일은 쿠버네티스 이용함.
nodejs 는 무중단을 위해 forever 이나 pm2 등을 이용하는데 rust 는 이를 쿠버네티스로 이용할 수 있음.
rust 는 빌드를 하면 하나의 바이너리 파일이 생성된다. 그래서 그것을 실행하면 된다.

2. 호출방식 ROUTE 및 디렉토리 구조

route

use axum::{
    extract::Extension,
    http::{header, Method},
    routing::{get, post},
    Router,
};


let app = Router::new()
        .route("/", get(controllers::info::route_info))
        .route("/login", post(controllers::auth::login))
        .route("/register", post(controllers::auth::register))
        .route("/", post(controllers::auth::refresh_token))
        .route("/user_profile", get(controllers::user::user_profile))
        .layer(cors)
        .layer(Extension(pool));
        
        

디렉토리 구조 구성하기

디렉토리를 만들고 같은 위치에 디렉토리명과 같은 rs 파일을 만들고
그안에 정의할 스크립트를 명시해준다.
controllers 디렉토리를 만들고 같은 위치에 controllers.rs

pub mod auth;
pub mod user;
pab mod info;

파일을 만들고 해당 폴더안에 들어갈 rust 파일명을 명시함으로

폴더단위 접근이 가능하게함

3. DB 연결 방식

  dotenv().ok();
   let durl:String = env::var("DATABASE_URL").expect("set Database_URL2 env variable");
   
    //initialize tracing
    tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::new(
            std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
        ))
        .with(tracing_subscriber::fmt::layer())
        .init();

db 실행

4. 사용자 인증 방식

JWT 인증

cors & target allow origin 처리

use axum::{
    extract::Extension,
    http::{header, Method},
    routing::{get, post},
    Router,
};
use tower_http::cors::{AllowOrigin, CorsLayer};


let cors = CorsLayer::new()
    .allow_credentials(true)
    .allow_headers(vec![
        header::ACCEPT,
        header::ACCEPT_LANGUAGE,
        header::AUTHORIZATION,
        header::CONTENT_LANGUAGE,
        header::CONTENT_TYPE,
    ])
    .allow_methods(vec![
        Method::GET,
        Method::POST,
        Method::PUT,
        Method::DELETE,
        Method::HEAD,
        Method::OPTIONS,
        Method::CONNECT,
        Method::PATCH,
        Method::TRACE,
    ])
    .allow_origin(AllowOrigin::exact(
        "http://localhost:5173".parse().unwrap(), // Make sure this matches your frontend url
    ))
    .max_age(Duration::from_secs(60 * 60));

5. 파일업로드 방식

6. 파일다운로드 방식

7. 외부 api연결 방식

8. 민감정보 연결 .Env or yuml

9. 미들웨어 사용방법

10. 테스트 방법

profile
개발은 쉽고 간단하게

0개의 댓글

Powered by GraphCDN, the GraphQL CDN