https://github.com/araddon/m2go
Mongrel2 Handler for Go
https://github.com/araddon/m2go
Last synced: about 1 year ago
JSON representation
Mongrel2 Handler for Go
- Host: GitHub
- URL: https://github.com/araddon/m2go
- Owner: araddon
- Created: 2011-11-26T21:11:48.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2012-05-07T02:02:37.000Z (about 14 years ago)
- Last Synced: 2025-02-03T17:11:20.045Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 109 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
m2go, a `Mongrel2 http server `_ for go.
Includes adapter to run `Pat.go `_
Usage
===================
Example App::
import (
"github.com/araddon/m2go"
"github.com/bmizerany/pat"
"io"
"log"
"net/http"
)
func main() {
log.SetFlags(log.Ltime | log.Lshortfile)
m := pat.New()
m.Get("/hello/:name", http.HandlerFunc(hello))
m.Get("/stream", http.HandlerFunc(stream))
m2go.ListenAndServe("tcp://127.0.0.1:9055|tcp://127.0.0.1:9056|d9eae9a0-6bad-11e1-9cc3-5254004a61b5", m)
}
func hello(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
io.WriteString(w, "Hello, "+name)
}
// this will serve as a Streaming API, continuing to push out updates
// to connected client
func stream(w http.ResponseWriter, r *http.Request) {
// this line: is the key to make it streaming.
r.Header.Set("Transfer-Encoding", "chunked")
r.Header.Set("Content-Type", "application/json")
io.WriteString(w, "some content")
// lets simulate a zeromq type connection that recieves messages periocically
// and pushes to client
timer := time.NewTicker(time.Second * 1)
go func() {
for _ = range timer.C {
io.WriteString(w, `{"msg":"still alive","status":200}`)
}
}()
}