Skip to content

Instantly share code, notes, and snippets.

View 0x49D1's full-sized avatar
💭
💾

Dmitry Pursanov 0x49D1

💭
💾
View GitHub Profile
@0x49D1
0x49D1 / run_projects_by_pattern.sh
Last active July 8, 2025 11:50
Script is a sample to run .NET projects from specific directory and subdirectories by pattern (for example run all "API" services from "sharedservices" directory) at once without need of any tools on any OS, just need a terminal with bash support.
#!/usr/bin/env bash
# Generic script to build and run all .NET projects from a given directory recursively by pattern
set -e
if [ $# -lt 2 ]; then
echo "Usage: $0 <directory> <pattern>"
echo "Example: $0 ../sharedservices '*.Api.csproj'"
exit 1
fi
@0x49D1
0x49D1 / RateLimitAttribute.cs
Created December 18, 2023 07:59
Simple attribute used to rate limit requests based on IP address and path.
/* Sample usage in controller action
[HttpPost]
[Route("in/batch-update")]
[RateLimit]
public virtual async Task<IActionResult> BatchUpdate(SomeModel model)
{...}
*/
/// <summary>
@0x49D1
0x49D1 / DotEnv.cs
Last active March 30, 2022 12:41
.env Loader class for >.NET 5 or .NET Core
// credits to: https://dusted.codes/dotenv-in-dotnet
// To read add something like:
// public static class Program
// {
// public static async Task Main(string[] args)
// {
// var root = Directory.GetCurrentDirectory();
// var dotenv = Path.Combine(root, ".env");
// DotEnv.Load(dotenv);
@0x49D1
0x49D1 / SemaphoreLocker.cs
Last active January 21, 2022 10:36
Locking mechanics with named locks for async operations. After finishing lock code block this code releases semaphore itself. Standard "lock" statement can't be used with async operations: https://stackoverflow.com/a/7612714/47672
// Usage example:
// static readonly SemaphoreLocker _semaphoreLocker = new SemaphoreLocker();
// var values = await _semaphoreLocker.LockAsync(valuesKey, async () => { return await SomeActionAsync(); });
public class SemaphoreLocker
{
static readonly ConcurrentDictionary<string, SemaphoreSlim> lockDictionary = new ConcurrentDictionary<string, SemaphoreSlim>();
public async Task LockAsync(string key, Func<Task> worker)
@0x49D1
0x49D1 / NamedLocker.cs
Created January 21, 2022 10:26
Named locker example. Not to use same lock object for all locking constructs, because sometimes it will lock different procedures in same code block. For example get/set cache with cache key argument.
public class NamedLocker
{
private readonly ConcurrentDictionary<string, object> _lockDict = new ConcurrentDictionary<string, object>();
// Get a lock for use with a lock(){} block
public object GetLock(string name)
{
return _lockDict.GetOrAdd(name, s => new object());
}
@0x49D1
0x49D1 / grants_on_all_procedures_and_functions_mysql.sql
Last active April 23, 2021 07:53
Grant all procedures that were changed TODAY (or other date as parameter) in any database (as parameter) for any user (as parameter). Works for MariaDB too.
---------------------------------------------
-- Creates procedure that takes schema/user/modificationFromDate as parameters and calls it for some schema/user/today in the end. This works for MariaDB too.
-- Example of grant as concat select
-- SELECT CONCAT('GRANT EXECUTE ON PROCEDURE YOUR_SCHEMA.', routine_name, ' TO user@`10.1.%`;') FROM information_schema.routines where routine_schema = 'YOUR_SCHEMA' AND ROUTINE_TYPE = 'PROCEDURE';
-- SELECT CONCAT('GRANT EXECUTE ON FUNCTION YOUR_SCHEMA.', routine_name, ' TO user@`10.1.%`;') FROM information_schema.routines where routine_schema = 'YOUR_SCHEMA' AND ROUTINE_TYPE = 'FUNCTION';
---- Grant all procedures that were changed TODAY (or other date as parameter) in any database (as parameter) for any user (as parameter)
DROP PROCEDURE IF EXISTS SysExecuteGrantsForModifiedProcedures;
CREATE PROCEDURE SysExecuteGrantsForModifiedProcedures(IN p_schema VARCHAR(120), IN p_mysqlUser VARCHAR(120),
@0x49D1
0x49D1 / NLog.config
Created January 28, 2021 07:48
Using NLog WebService target for Telegram example. Send logs to Telegram via https://core.telegram.org/bots/api#sendmessage Telegram Bot API.
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="telegramBotToken" value="YOUR_TELEGRAM_BOT_TOKEN"/>
<variable name="telegramChatId" value="YOUR_BOT_CHAT_ID"/>
@0x49D1
0x49D1 / gcd_several_numbers.cs
Last active November 18, 2020 11:57
Find GCD for several numbers at once. Just a common snippet for math problems.
// gcd for n numbers
static int gcd(int[] numbers)
{
return numbers.Aggregate(gcd);
}
// gcd for 2
static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
@0x49D1
0x49D1 / deploy_angular_example.sh
Created June 15, 2020 11:48
Deploy angular app using SCP with automatic password without interaction
ng build --prod && sshpass -f ~/test_pass scp -r ./dist/project/* REMOTE_USER@REMOTE_SERVER:/var/www/html
# Where:
# test_pass is the file, containing the password for remote server access
# the project will be deployed to /var/www/html
@0x49D1
0x49D1 / update_git_repos.sh
Last active April 13, 2020 09:00
Script to update and publish (pull&push) all git repositories in sub-directories at once.
#!/bin/bash
eval $(keychain --eval id_github_rsa) # in case you use keychain to store keys. https://linux.die.net/man/1/keychain
find . -maxdepth 1 -type d -exec sh -c '(cd {} && echo $PWD && git pull && git push)' ';'