Skip to content

Instantly share code, notes, and snippets.

@soheilhy
soheilhy / nginxproxy.md
Last active May 8, 2024 09:56
How to proxy web apps using nginx?

Virtual Hosts on nginx (CSC309)

When hosting our web applications, we often have one public IP address (i.e., an IP address visible to the outside world) using which we want to host multiple web apps. For example, one may wants to host three different web apps respectively for example1.com, example2.com, and example1.com/images on the same machine using a single IP address.

How can we do that? Well, the good news is Internet browsers

@soheilhy
soheilhy / mochatutorial.md
Last active March 18, 2022 05:23
Mocha Tutorial

Testing Node.JS applications using Mocha

Mocha is a unittest framework for Node. In this document, we explain how you can test your javascript code and also your HTTP servers.

Installing Mocha

Use npm to install Mocha:

npm install mocha
@soheilhy
soheilhy / HttpServer.scala
Created June 14, 2012 02:45
Routing based on HTTP method in Finagle
import com.twitter.finagle.http.path._
import com.twitter.finagle.http.service.RoutingService
import com.twitter.finagle.http.{Request, Response, RichHttp, Http}
import com.twitter.finagle.{Service, SimpleFilter}
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1
import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
import org.jboss.netty.util.CharsetUtil.UTF_8
import com.twitter.util.Future
@soheilhy
soheilhy / server.go
Created August 1, 2015 16:41
grpc route guide server + cmux
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright

MongoDB + NodeJS Tutorial

Prerequisites

  • You have node.js and npm installed on your machine.
  • If you use CDF:
    • Use bash or zsh as your shell.
    • Make sure npm installs packages locally as you don't have root permissions. To do so, you can edit ~/.npmrc and add the following line:
      • prefix=$NODE_HOME
    • $NODE_HOME is the directory in which you have installed node.js.
var http = require('http');
var hello = function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello! <br />');
res.write('<br />' +
'URL:' + req.url + '<br />' +
'Method:' + req.method + '<br />');
};
@soheilhy
soheilhy / gist:3077008
Created July 9, 2012 14:56
A generic routing service
class RoutingService[REQUEST <: Request, RESPONSE](
val routes: PartialFunction[Request, Service[REQUEST, RESPONSE]])
extends Service[REQUEST, RESPONSE] {
protected[this] val requestToService = routes
def apply(request: REQUEST): Future[RESPONSE] = {
val service = requestToService(request)
service(request)
}
@soheilhy
soheilhy / CakePattern.scala
Created June 20, 2012 01:40
Cake Pattern
// You create the trait.
trait JavaScriptCompilerComponent {
def compiler: JavaScriptCompiler
// You add the abstract method to get all javascript files.
def jsFiles: Seq[Source]
def getCompiledJavaScript = compiler.compile(jsFiles)
trait JavaScriptCompiler {
@soheilhy
soheilhy / CakePatternIm.scala
Created June 20, 2012 01:25
Cake Pattern Imitated
// You create the trait.
trait JavaScriptCompilerComponent {
def compiler: JavaScriptCompiler
trait JavaScriptCompiler {
def compile(files: Seq[Source]): String
}
}
// You implement it.
@soheilhy
soheilhy / cmux_example.go
Last active August 29, 2015 14:26
cmux example
// Create the main listener.
lis, err := net.Listen("tcp", ":23456")
if err != nil {
log.Fatal(err)
}
// Create a cmux.
mux := cmux.New(lis)
// Match connections in order: