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.go
Created February 11, 2025 03:25
Golang unmarshal from string and int
// StringInt create a type alias for type int
type StringInt int
// UnmarshalJSON create a custom unmarshal for the StringInt
/// this helps us check the type of our value before unmarshalling it
func (st *StringInt) UnmarshalJSON(b []byte) error {
//convert the bytes into an interface
//this will help us check the type of our value
//if it is a string that can be converted into an int we convert it
@DefectingCat
DefectingCat / jwt.rs
Created October 21, 2024 01:55
Rust jwt
use jsonwebtoken::{
decode, encode, Algorithm, DecodingKey, EncodingKey, Header, TokenData, Validation,
};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Claims {
// aud: String, // Optional. Audience
pub exp: usize, // Required (validate_exp defaults to true in validation). Expiration time (as UTC timestamp)
pub iat: usize, // Optional. Issued at (as UTC timestamp)
// compat module between xitca-http and axum.
mod tower_compat {
use core::{cell::RefCell, fmt, future::Future, marker::PhantomData};
use std::net::SocketAddr;
use http_body::Body;
use xitca_http::{
bytes::Bytes,
h1::RequestBody,
@DefectingCat
DefectingCat / mod.go
Created September 5, 2024 09:38
golang json parse date time
type Datetime struct {
time.Time
}
func (t *Datetime) UnmarshalJSON(input []byte) error {
strInput := strings.Trim(string(input), `"`)
newTime, err := time.Parse("2006-01-02", strInput)
if err != nil {
return err
}
@DefectingCat
DefectingCat / binding_arr.rs
Created July 4, 2024 05:11
rust at operator binding
fn main() {
let strs = ["hello world", "1", "hello", "world"];
let mut remaining: &[&str] = &strs;
while let [current, tail @ ..] = remaining {
println!("{} - {:?}", current, tail);
remaining = tail;
}
println!("The real strs {:?}", strs);
}
@DefectingCat
DefectingCat / .gitlab-ci.yml
Created June 6, 2024 03:19
gitlab ci docker runner use ssh private key
before_script:
- "command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )"
- eval $(ssh-agent -s)
- chmod 400 "$SSH_PRIVATE_KEY"
- ssh-add "$SSH_PRIVATE_KEY"
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
@DefectingCat
DefectingCat / daemon.json
Created June 6, 2024 03:04
docker daemon
{
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"],
"insecure-registries" : ["192.168.1.57:8004"],
"data-root": "/home/xfy/data/docker-var"
}
@DefectingCat
DefectingCat / validator.rs
Created June 5, 2024 14:44
axum json and form validator
use axum::{
async_trait,
extract::{rejection::FormRejection, FromRequest, Request},
Form, Json,
};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::de::DeserializeOwned;
use validator::Validate;
@DefectingCat
DefectingCat / jwt.rs
Created June 5, 2024 14:43
rust jsonwebtoken
use std::sync::OnceLock;
use jsonwebtoken::{DecodingKey, EncodingKey, Header};
use serde::{Deserialize, Serialize};
pub struct Keys {
pub encoding: EncodingKey,
pub decoding: DecodingKey,
}
impl Keys {
@DefectingCat
DefectingCat / password.rs
Created June 5, 2024 14:43
argon2 password hash
use anyhow::{anyhow, Context};
use tokio::task;
use argon2::password_hash::SaltString;
use argon2::{password_hash, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
/// 生成 hash
///
/// ## Arguments
///