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 / 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> {
@DefectingCat
DefectingCat / resizable-table.tsx
Created November 30, 2023 05:28
Ant design resizable table
import { TableProps } from 'antd';
import Table, { ColumnsType } from 'antd/es/table';
import dynamic from 'next/dynamic';
import { memo, useMemo, useState } from 'react';
import { ResizeCallbackData } from 'react-resizable';
import styles from 'styles/index.module.scss';
const ResizableTitle = dynamic(
() => import('components/pages/resizable-title'),
);
@DefectingCat
DefectingCat / lspconfig.lua
Created November 17, 2023 03:58
organize imports for tsc server
local function organize_imports()
local params = {
command = "_typescript.organizeImports",
arguments = { vim.api.nvim_buf_get_name(0) },
}
vim.lsp.buf.execute_command(params)
end
lspconfig.tsserver.setup {
on_attach = on_attach,
@DefectingCat
DefectingCat / error.rs
Last active January 18, 2024 01:31
Axum custom app error.
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use log::error;
use serde_json::json;
#[derive(thiserror::Error, Debug)]
pub enum AppError {