jazz

package module
v0.0.0-...-fc8cf6a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2016 License: MIT Imports: 3 Imported by: 0

README

GoDoc license

Handle HTTP requests with style (in Javascript).

Motivation

Javascript and Go belong together. I should be able to use npm modules inside my Go runtime.

Five Minutes Tutorial

// 1. Write your javascript modules. Example: jshandlers/index.js
// module.exports = {
// 	 handle: function(request, response) {
//	 	 ResponseUtil(response).WriteString("Hello World, from Javascript")
//	 }
// }

// 2. Write your Go project
package main

import (
	"fmt"
	"net/http"

	"github.com/didip/jazz"
	"github.com/robertkrimen/otto"
)

func gohello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World, from Go")
}

func main() {
	vm := otto.New()

	// Configure all the global settings
	jsutil.Configure(vm)

	// Expected output: "Hello World, from Go"
	http.HandleFunc("/", gohello)

	// Expected output: "Hello World, from Javascript"
	http.HandleFunc("/js", jazz.JSFuncHandler(vm, "jshandlers/index.js"))

	http.ListenAndServe(":8080", nil)
}

Features

  1. Write request handlers in Javascript.

  2. Javascript handlers are loaded during runtime, so you can refresh and see changes immediately. Just like PHP.

  3. Able to require npm modules. $NODE_PATH is used to find modules.

  4. The javascript VM can further be enhanced to provide more functionalities.

Why do I need this?

  1. You want to provide extensibility to your Go HTTP service.

  2. You have this very important npm module that does not have Go equivalent.

  3. You are just hacking for fun, wanting to see what's possible.

Otto Caveats

  • It complies to ECMAScript 5.1.

  • "use strict" will parse, but does nothing.

  • The regular expression engine (re2/regexp) is not fully compatible with the ECMA5 specification.

Documentation

Overview

Package jazz provides various request handling capabilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSFuncHandler

func JSFuncHandler(vm *otto.Otto, jsModule string) func(w http.ResponseWriter, r *http.Request)

JSFuncHandler loads and runs a javascript module that can process http request given request handler function.

Types

This section is empty.

Directories

Path Synopsis
Package httputil provides various convenience functions for request/response handling
Package httputil provides various convenience functions for request/response handling
Package jsutil provides various convenience functions for Javascript environment
Package jsutil provides various convenience functions for Javascript environment

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL