Skip to content

Instantly share code, notes, and snippets.

View RhysC's full-sized avatar

Rhys Campbell RhysC

  • Perth; Australia
View GitHub Profile
@RhysC
RhysC / PerfCounterSample.cs
Created April 4, 2011 10:37
Linq Pad CPU usage C# program sample
void Main()
{
var instanceName = "LINQPad";
var counterCollectionInterval = TimeSpan.FromSeconds(1);
var continueCollectingTimeFrame = TimeSpan.FromSeconds(10);
var counters = from performanceCounterDefinition in PerformanceCounterRepository.GetPerfCounters
from counterName in performanceCounterDefinition.Counters
select new {
@RhysC
RhysC / RedisRx.linq
Created October 21, 2014 12:02
LinqPad sample of Redis Pub sub using StackExchange.Redis and RX
<Query Kind="Program">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<NuGetReference>StackExchange.Redis</NuGetReference>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Reactive.Disposables</Namespace>
<Namespace>StackExchange.Redis</Namespace>
</Query>
void Main()
@RhysC
RhysC / Guard.cs
Created September 3, 2015 08:00
Showing a proposed use of Guard with expression for static checks and correct name evaluation in exception messages
public static class Guard
{
public static void NotNull<T>(Expression<Func<T>> notNullableExpression) where T : class
{
var compliedExpression = notNullableExpression.Compile();
if (compliedExpression() == null)
{
var paramName = notNullableExpression.GetObjectNameGraph();
throw new ArgumentNullException(paramName);
@RhysC
RhysC / CSharpObjectDefinition.cs
Created October 31, 2014 02:55
Code generator from JSON Schema - swap out the impl of ObjectDef to write, view model, dtos etc
using System;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Schema;
namespace JsonSchemaToCode
{
public class CSharpObjectDefinition : ObjectDefinition
{
public override string ToString()
@RhysC
RhysC / build_and_run.sh
Created July 29, 2018 05:40
Cron jobs in docker (using node base to show we can use that image)
docker build -f ./cron.docker --tag cronsample:local .
docker run -d cronsample:local --name cronsample_rhysc
# Wait a bit, a minute in fact
docker container cp cronsample_rhysc:/var/log/cron.log ./cron.log
cat ./cron.log
# Should see "Hello world" printed on a new line for every minute the container has been running
@RhysC
RhysC / generate_select.sql
Created July 6, 2017 08:20
Generate Select * (with column names defined ) in postgres
SELECT 'SELECT ' || string_agg(column_name, ', ') || ' FROM ' || table_name
FROM information_schema.columns
WHERE table_schema='my_schema'
GROUP BY table_name
@RhysC
RhysC / RequireApiKey
Created August 28, 2014 08:18
RequireApiKey require an api key for MVC controllers assumes SSL
public class RequireApiKey : ActionFilterAttribute
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(RequireApiKey));
public override void OnActionExecuting(HttpActionContext context)
{
var ipAddress = GetIpAddress(context);
Logger.InfoFormat("API attempt. Uri {0} - IP {1} - Headers {2} ", context.Request.RequestUri, ipAddress, context.Request.Headers);
IEnumerable<string> values;
if (context.Request.Headers.TryGetValues("ApiKey", out values) && GetApiKeys().Any (x => x ==values.First())
@RhysC
RhysC / InlineFileAttribute.cs
Created December 8, 2014 05:33
Show file as web page, not an automatic download | MVC Asp.net filter
public class InlineFileAttribute : ActionFilterAttribute
{
private const string ContentDisposition = "Content-Disposition";
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var headers = filterContext.HttpContext.Response.Headers;
if (!string.IsNullOrWhiteSpace(headers[ContentDisposition]))
headers[ContentDisposition] = headers[ContentDisposition].Replace("attachment", "inline");
base.OnResultExecuted(filterContext);
@RhysC
RhysC / ActiveDirectorySnippets.txt
Created November 17, 2016 03:05
Active directory snippets
# opens a GUI to manage users under the given account - fine for adding/upodate one or two accounts
runas /user:myDomain\myusername "C:\Windows\System32\rundll32.exe dsquery, OpenQueryWindow"
##POWERSHELL – to load the users, done from a server so it has the AD PS modules
import-module activedirectory
# basic search using ps filters
Get-ADUser -filter {GivenName -like "rickie"}
@RhysC
RhysC / Calendar.html
Last active November 11, 2016 12:17
Schedule or calendar html page using Knockout bindings, vanilla js and bootstrap shell. View here https://gistpreview.github.io/?640b2b2a8fb038c6f1b62bbb3059628d
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Calendar Demo</title>
<!-- BEGIN CALENDAR CSS - ideally these would be in a separate file - eg /calendar-template.css -->
<style>