Thursday, September 25, 2014

Unix & stdlog

I wish Unix had also added stdlog.  It seems to be a basic part of building software, but we have to cram it into stderr, which makes no sense.  Someone got lazy.  Fortran had the right idea.

http://www.gfdl.noaa.gov/~vb/mpp.html#stdunits

stdin, stdout, stderr, stdlog

function stdin()
  integer :: stdin
function stdout()
  integer :: stdout
function stderr()
  integer :: stderr
function stdlog()
  integer :: stdlog 
 

Monday, July 7, 2014

Gorilla Mux and multiple routers

Looking at Gorilla's Mux, I wanted to come up with a simple example of how to bind multiple routers to different base URLs.  For the first router, I'm not using the PathPrefix() method, so the handler function must contain the prefix that you're interested ('/v1').  The second doesn't require that as we're using the Subrouter() that utilizes the prefix for all handlers.


package main

import (
    "github.com/gorilla/mux"
    "log"
    "net/http"
)

func main() {
    rtr := mux.NewRouter()
    rtr.HandleFunc("/v1/user/{name:[a-z]+}/profile", profile).Methods("GET")

    rtrv2 := mux.NewRouter().PathPrefix("/v2").Subrouter()
    rtrv2.HandleFunc("/user/{name:[a-z]+}/profile", profilev2).Methods("GET")
 

    http.Handle("/v1/", rtr)    // need trailing slash     
    http.Handle("/v2/", rtrv2)  // need trailing slash

    log.Println("Listening...")
    http.ListenAndServe(":3000", nil)
}

func profile(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    name := params["name"]
    w.Write([]byte("Hello " + name))
}

func profilev2(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    name := params["name"]
    w.Write([]byte("Goodbye " + name))
}

Tuesday, April 22, 2014

Converting To and From UUIDs in MySQL

DELIMITER |

CREATE FUNCTION uuid_from_bin(b BINARY(16))
RETURNS CHAR(36) DETERMINISTIC
BEGIN
DECLARE hex CHAR(32);
SET hex = LOWER(HEX(b));
RETURN CONCAT(LEFT(hex, 8), '-', MID(hex, 9,4), '-', MID(hex, 13,4), '-', MID(hex, 17,4), '-', RIGHT(hex, 12));
END
|

CREATE FUNCTION uuid_to_bin(s CHAR(36))
RETURNS BINARY(16) DETERMINISTIC
RETURN UNHEX(CONCAT(LEFT(s, 8), MID(s, 10, 4), MID(s, 15, 4), MID(s, 20, 4), RIGHT(s, 12)))
|

DELIMITER ;


Source:  https://gist.github.com/159151/e66f0cc0a643c86041df3adb81d5758611e82096

Wednesday, March 19, 2014

NodeJS: How to return a JSON parsed object from v8

  string s = jvalue.toStyledString();
  v8::Handle<v8::Value> vs = cvv8::CastToJS(s);

    v8::Handle<v8::Context> context = v8::Context::GetCurrent();
    v8::Handle<v8::Object> global = context->Global();

    v8::Handle<v8::Object> JSON = global->Get(v8::String::New("JSON"))->ToObject();
    v8::Handle<v8::Function> JSON_parse = v8::Handle<v8::Function>::Cast(JSON->Get(v8::String::New("parse")));

    // return JSON.parse.apply(JSON, jsonString);
    return scope.Close(JSON_parse->Call(JSON, 1, &vs));