眠気.jl

投稿=不定期

備忘録: RustのCLIチュートリアルのExcercise1.3

First implementation - Command Line Applications in Rust

Exercise for the reader: This is not the best implementation: It will read the whole file into memory – however large the file may be. Find a way to optimize it! (One idea might be to use a BufReader instead of read_to_string().)
とのことだったのでBuffReader使いました

書いたもの

#![allow(unused)]

use clap::Parser;
use std::io::BufReader;
use std::fs::File;
use std::io::BufRead;

/// Search for a pattern in a file and display the lines that contain it.
#[derive(Parser)]
struct Cli {
    /// The pattern to look for
    pattern: String,
    /// The path to the file to read
    #[clap(parse(from_os_str))]
    path: std::path::PathBuf,
}

fn main() {
    let args = Cli::parse();

    let content = File::open(&args.path).expect("could not read file");
    let mut reader = BufReader::new(content);
    let mut line = String::new();

    for line in reader.lines() {
        if line.as_ref().unwrap().contains(&args.pattern) {
            println!("{}", line.unwrap());
        }
    }
    println!("path = {:?}", args.path);
    println!("pattern = {:?}", args.pattern);
}

その他

もっといいやり方あるかも
Rustに詳しい私のアンチの方がいたらご教示ください。

AWS App Runnerでactixをホスティングする手順

準備

aws configure 通したaws cli

examples/docker at master · actix/examples · GitHub

をクローンして、examples/dockerの中で作業する

目標

App Runnerのデフォルトドメインを叩いて、ローカルと同じ挙動にする

curl [デフォルトドメイン]; curl [デフォルトドメイン]/again
Hello world!Hello world again!

手順の概要

1 ECRにactixのDockerイメージを登録(CLIで済ませる)
2 AppRunnerでECRを指定(コンソールで設定する)

手順1

actixapprunnerという名前でECRレポジトリ作成

aws ecr create-repository --repository-name actixapprunner

この結果でJSONで表示される"repositoryUri"を使います
ECRにログイン

aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin [repositoryUri]

actixapprunnerというタグ付けでビルド

docker build -t actixapprunner .

タグの関連付け

docker tag actixapprunner:latest [repositoryUri]/:latest

push

docker push [repositoryUri]:latest

手順2

App Runnerコンソール
->リポジトリタイプ: コンテナレジストリ
->プロバイダー: Amazon ECR
->イメージURI: 作成したイメージを選択
->その他はデフォルトで作成

確認

curl [デフォルトドメイン]; curl [デフォルトドメイン]/again
Hello world!Hello world again!

その他

App Runnerもcliでやりたかったですがjsonを事前設定したりが面倒そうでコンソールでやってしまいました

日記

雷雨の中私用で外出てから何故か鼻水が止まりません、不思議ですね。

Rustでバックエンドやりたいになっていて、Dockerのexampleを見つけました。
明日これをawsにのせていい感じにしたい。
examples/docker at master · actix/examples · GitHub

Packtでずっと気になっていたBabylon.jsの本がEarly Access可能になったのでそれも読みたい。
jsとmaintanableってタイトルから矛盾してるじゃねーか!!と思うかも知れませんが, Babylon自体はTSで書かれてるという旨も説明ありました。

GitHub CIのAzure/static-web-apps-deploy@v1でNode16使うようにする

結論

ciのyaml

        with:
          node-version: 16

を追加するだけでなく package.jsonでも

  "engines": {
    "node": "^16.10.0"
  },

の指定が必要だった

経緯

こんなエラーが出て、ぐぐったらnode14やめてバージョン上げたら解決した、という投稿を見かけたため

> Build error occurred
SyntaxError: Unexpected token '||='
    at Loader.moduleStrategy (internal/modules/esm/translators.js:145:18) {
  type: 'SyntaxError'
}

おわりに

さすがマイクロソフトだなって僕の中のろいさんが言ってました

Stripe Webhookの設定メモ

背景

Nextで生やしたapi/にstripe-hooks生やしたメモ
stripe自体の設定は済んだ状態から、Webhookでイベント(契約、購入など)を受け取れるようにした

Stripeコンソールでやったこと

開発者->Webhook->エンドポイントを追加->URL打ち込む

.envにしたこと

SIGNING_SECRETを.envに追記

stripe-hooksに追記したこと

  const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
  const signature = req.headers["stripe-signature"];
  const signingSecret = process.env.STRIPE_SIGNING_SECRET;
  const reqBuffer = await buffer(req);
  let event;
  event = stripe.webhooks.constructEvent(reqBuffer, signature, signingSecret);

Azure Static Web AppsでNext.js(SSG)ホスティングするチュートリアルのメモ

基本的にリンクの手順通りで良かったです

.github/workflows/playwright-onDemand.yml
.github/workflows/playwright-scheduled.yml

がこけてたので消して動かしました。

チュートリ
チュートリアル:静的にレンダリングされた Next.js の Web サイトを Azure Static Web Apps にデプロイする | Microsoft Docs

公式でフォークを推奨されてるレポ(CIが動いたり動かなかったりしてる)
GitHub - staticwebdev/nextjs-starter: A Next.js starter application for deploying to Azure Static Web Apps

公式が動かないのは久々にマイクロソフトらしさを感じてよかったです。
最近TypeScriptやVSCodeGitHubで徳を積みすぎてるので、ちゃんと動かないチュートリアルやトラブルを一生シュートしてくれないトラブルシューティングガイドとか出してバランスをとって欲しいですね。