Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
carlhoerberg / nginx.conf
Created January 19, 2012 21:49
Nginx config template
user www-data;
pid /var/run/nginx.pid;
worker_processes 1;
events {
worker_connections 1024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
use epoll; # enable for Linux 2.6+
}
$ jruby -e "require 'uglifier'; Uglifier.compile File.read 'jquery-1.7.1.js'"
java(25487,0x10f18a000) malloc: *** error for object 0x7f9604a05300: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
require 'rack'
require 'json'
require 'torquebox-messaging'
class EventStream
def self.call(env)
path = Rack::Utils.unescape env["PATH_INFO"]
[200, {
'Content-Type' => 'text/event-stream',
'X-Accel-Buffering' => 'no',
@carlhoerberg
carlhoerberg / gist:1146796
Created August 15, 2011 13:50
Optional validation
[HttpPost]
public ActionResult Create(int id, Projekt model)
{
var p = db.Get<Projekt>(id);
if (p.Typ1Validering)
if (string.IsNullOrEmpty(model.Field))
ModelState.AddModelError("Field", "Fältet får inte vara tomt");
if (ModelState.IsValid)
{
@carlhoerberg
carlhoerberg / Rakefile.rb
Created May 17, 2011 19:33
How to do automatic backup with Heroku PGBackups and Heroku Cron. http://carlhoerberg.com/automatic-backup-of-heroku-database-to-s3
require 'aws/s3'
require 'heroku'
require 'heroku/command'
require 'heroku/command/auth'
require 'heroku/command/pgbackups'
task :cron do
class Heroku::Auth
def self.client
Heroku::Client.new ENV['heroku_login'], ENV['heroku_passwd']
public class AutocompleteController : Controller
{
private readonly IRepository db;
public AutocompleteController(IRepository db)
{
this.db = db;
}
public JsonResult Users(string term)
{
@carlhoerberg
carlhoerberg / Global.asax.cs
Created April 5, 2011 07:12
Replace log4net replacement for Elmah
protected void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
var log = LogManager.GetLogger(GetType());
var msg = string.Format("\r\nPath: {0}\r\nMethod: {4}\r\nUser: {1}\r\nForm: {2}\r\nReferer: {3}",
Request.Url,
User.Identity.Name,
Request.Form,
Request.UrlReferrer,
Request.HttpMethod);
@carlhoerberg
carlhoerberg / global.asax.cs
Created March 15, 2011 10:38
How to get precompiled mvc 3 site working in Mono 2.10
public class MonoWebFormViewEngine : WebFormViewEngine
{
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
return base.FileExists(controllerContext, virtualPath.Replace("~", ""));
}
}
public class MonoRazorViewEngine : RazorViewEngine
{
@carlhoerberg
carlhoerberg / EncryptedColumnInEntity.cs
Created January 13, 2011 16:14
Make the encryption explicit in the domain model
public class EncryptedColumnInEntity : Entity
{
protected virtual byte[] EncryptedText { get; set; }
/// <summary>
/// Decryptes the text stored in the database
/// </summary>
/// <exception cref="CryptographicException">Throws CryptographicException when the password isn't valid</exception>
/// <param name="password">The password which the text was encrypted with</param>
/// <returns>Decrypted text</returns>
@carlhoerberg
carlhoerberg / AesCryptoProvider.cs
Created January 13, 2011 15:59
Implementation of an encrypted string user type
class AesCryptoProvider : ICryptoProvider
{
private string password;
public AesCryptoProvider(string password)
{
this.password = password;
}
public byte[] Encrypt(string inputText)