Skip to content

Instantly share code, notes, and snippets.

View DefectingCat's full-sized avatar
🎮
ELDEN RING

Sonetto DefectingCat

🎮
ELDEN RING
View GitHub Profile
@DefectingCat
DefectingCat / .prettierignore
Created December 26, 2023 03:44
Common prettier configuration
node_modules
out
.next
public
dist
build
@DefectingCat
DefectingCat / react
Created December 26, 2023 03:42
Common editorconfig
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
@DefectingCat
DefectingCat / mod.rs
Created December 26, 2023 01:15
Axum fallback and hello world route
use axum::{
http::{StatusCode, Uri},
response::IntoResponse,
};
use tracing::info;
pub mod baidu;
pub async fn hello() -> &'static str {
"Hello World!"
@DefectingCat
DefectingCat / mod.rs
Created December 26, 2023 01:14
Axum custom add version for each response middleware
use crate::error::AppResult;
use anyhow::anyhow;
use axum::{
body::Body, extract::Request, http::HeaderValue, middleware::Next, response::IntoResponse,
};
/// Middleware for adding version information to each response's headers.
///
/// This middleware takes an incoming `Request` and a `Next` handler, which represents the
/// subsequent middleware or route in the chain. It then asynchronously runs the next handler,
@DefectingCat
DefectingCat / builder.test.ts
Created December 25, 2023 08:53
Vitest mock fn
import { existsSync, createReadStream, createWriteStream } from 'fs';
import { Readable, Writable } from 'stream';
import { afterEach, describe, it, vi } from 'vitest';
vi.mock('fs', async (importOriginal) => ({
...(await importOriginal<typeof import('fs')>()),
existsSync: vi.fn().mockImplementation(() => true),
createReadStream: vi.fn().mockImplementation(() => {
const stream = new Readable();
stream.push('test');
@DefectingCat
DefectingCat / reg.ts
Created December 20, 2023 06:55
WeChat and iOS WeChat regexp
export const isWeChat = () =>
/MicroMessenger/i.test(window.navigator.userAgent);
export const isWeChatSafari = navigator.userAgent.match(
/(?=iPhone|iPod)(?=.*(Safari|MicroMessenger))/i
);
@DefectingCat
DefectingCat / pre-commit
Last active January 18, 2024 01:42
Rust git pre commit hooks
#!/bin/sh
set -eu
if ! cargo fmt -- --check; then
echo "There are some code style issues."
echo "Run cargo fmt first."
exit 1
fi
@DefectingCat
DefectingCat / gitlab-ci.yml
Last active December 26, 2023 01:07
Gitlab CI for rust
stages:
- build
variables:
APP_NAME: "cymo"
image: "rust:latest"
before_script:
- apt-get update -y
@DefectingCat
DefectingCat / trace_layer.rs
Created December 12, 2023 03:09
Trace layer for axum
use tower_http::trace::{
DefaultMakeSpan, DefaultOnBodyChunk, DefaultOnEos, DefaultOnFailure, DefaultOnRequest,
DefaultOnResponse, TraceLayer,
};
use tracing::Level;
/// Constructs a TraceLayer for Axum applications to enable logging and tracing of HTTP requests and responses.
///
/// The TraceLayer is a middleware layer that integrates with Tower HTTP's tracing capabilities.
/// It provides detailed information about the execution of HTTP requests and responses, allowing for
@DefectingCat
DefectingCat / fold.rs
Created December 6, 2023 08:55
Return a closure fn
/// Find parents of all files
///
/// ## Arguments
///
/// `local_path`: local files path from params
///
/// ## Return
///
/// `Fn`: used for `fold()`'s callback
pub fn fold_parents(local_path: &String) -> impl Fn(Vec<PathBuf>, &PathBuf) -> Vec<PathBuf> {