Skip to content

Instantly share code, notes, and snippets.

@abdusco
abdusco / server.py
Created March 2, 2024 04:51
Python mini HTTP server to serve a single request and shutdown
import http.server
import socketserver
import threading
import urllib.parse
def wait_for_oauth_callback(port: int = 9090) -> str:
code: str | None = None
class CallbackHandler(http.server.SimpleHTTPRequestHandler):
@abdusco
abdusco / hub.go
Created January 23, 2022 14:43
Hub implementation in Go
type Subscriber struct {
send chan string
}
type Hub struct {
clients map[*Subscriber]bool
broadcast chan string
register chan *Subscriber
unregister chan *Subscriber
}
@abdusco
abdusco / JwtCache.cs
Last active June 19, 2021 12:55
JWT Cache
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
namespace JwtUtils
{
public interface IJwtCache
{
void Set(string key, JwtSecurityToken token);
@abdusco
abdusco / realdebrid.py
Created May 22, 2021 10:02
RealDebrid + aria2 downloader script
#!/usr/bin/env python3
import httpx
import subprocess
import logging
import typer
from pathlib import Path
logging.basicConfig(
level=logging.DEBUG,
format="%(levelname)s %(asctime)s: %(message)s",
@abdusco
abdusco / Startup.cs
Created May 4, 2021 08:40
SwashBuckle configuration for bearer tokens
services.AddSwaggerGen(c =>
{
// ...
c.AddSecurityDefinition("bearer", new OpenApiSecurityScheme
{
Description =
"Authenticate with an existing JWT token. **Prefix the token with `Bearer`, i.e. `Bearer eyJ...`**",
In = ParameterLocation.Header,
Name = HeaderNames.Authorization,
import dataclasses
import functools
import io
import pathlib
from datetime import timedelta, datetime
from io import FileIO
from typing import Callable
import httpx
@abdusco
abdusco / JsonQuery.cs
Created March 30, 2021 18:32
ASP.NET Core json query string with Swagger support
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Http;
using System.Net.Mime;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
@abdusco
abdusco / LoginType.cs
Last active March 22, 2021 04:29
ASP.NET Core custom model binder
public sealed class LoginType
{
public string Name { get; set; }
public static ICollection<LoginType> All { get; set; } = new List<LoginType>();
public static LoginType Internal = new LoginType(nameof(Internal));
public static LoginType Customer = new LoginType(nameof(Customer));
public static LoginType Impersonation = new LoginType(nameof(Impersonation));
@abdusco
abdusco / EfCustomIdGenerator.csproj
Created March 14, 2021 11:23
EF Core use custom Guid generator for ids
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.4" />
<PackageReference Include="NewId" Version="3.0.3" />
@abdusco
abdusco / HostBuilderExtensions.cs
Last active March 12, 2021 12:52
Execute ConfigureServices without IWebHostBuilder
public static class HostBuilderExtensions
{
public static IHostBuilder UseStartup<TStartup>(this IHostBuilder hostBuilder)
where TStartup : class =>
hostBuilder.ConfigureServices((ctx, sc) =>
{
var startupType = typeof(TStartup);
var startupArgs = startupType
.GetConstructors().First()
.GetParameters()