はじめに
現在個人利用するためのTRPGのサポートwebアプリを開発中。
その中で、試しにログイン機能を付けようと試みた。
github-Copilotのチャット機能を活用しながら進めていたが、actix-webのバージョン更新に伴い作り方が変わっていたので覚書として残しておく。
各種バージョン
rustc 1.78.0
cargo 1.38
actix-web 4.5.1
変更点
Transformのジェネリクス
-> Requestをwhere節ではなくジェネリクスに入れる。
実際の実装
まず入口のTransform。
これがCopilotに提案された実装
use actix_web::{HttpRequest, Error, middleware, dev::ServiceRequest, dev::ServiceResponse, dev::Service};
use futures::future::{Ready, ok};
use futures::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct JwtValidator;
impl<S, B> middleware::Transform<S> for JwtValidator
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = JwtValidatorMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(JwtValidatorMiddleware { service })
}
}
過去のバージョンの記事などを参照するとこれで問題ない。
このまま実装するとTransformとtype Requestでコンパイルエラーが発生する。
公式のドキュメントを参照すると、少し記法が変わっている。
use crate::servicies::auth_service::verify_token;
use actix_web::{dev::Service, dev::ServiceRequest, dev::ServiceResponse, dev::Transform, Error};
use futures::future::{ok, Ready};
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct JwtValidator;
impl<S, B> Transform<S, ServiceRequest> for JwtValidator
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = JwtValidatorMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(JwtValidatorMiddleware { service })
}
}
これまでRequest = ServiceRequestとしていたところをServiceRequest直書きでよくなったらしい。
これはServiceの方でも同様の変更がなされている。
終わりに
github-Copilotのおかげでだいぶスムーズに個人開発で勉強を進められている。
しかし、バージョン変更に対して弱いのでまだまだ自力で調べる能力は必要と感じた。