Created
September 26, 2015 14:59
-
-
Save pkieltyka/049266c307bc8f14ea65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example server that embeds gnatsd | |
package main | |
import ( | |
"log" | |
"net/http" | |
"syscall" | |
"github.com/nats-io/gnatsd" | |
"github.com/nats-io/nats" | |
"github.com/zenazn/goji/graceful" | |
"github.com/zenazn/goji/web" | |
) | |
func main() { | |
log.Println("disko server") | |
// Gnatsd service running inside of our service. | |
// we are a NATS node and broker. | |
ns := gnatsd.New(&gnatsd.Options{}) | |
go ns.Start() | |
// setup handlers | |
nc, err := nats.Connect(nats.DefaultURL) | |
if err != nil { | |
log.Fatal(err) | |
} | |
nc.Subscribe("disko", func(m *nats.Msg) { | |
log.Println("got msg", m) | |
}) | |
// Basic web service | |
m := web.New() | |
m.Get("/", func(w http.ResponseWriter, r *http.Request) { | |
err := nc.Publish("disko", []byte("test")) | |
if err != nil { | |
http.Error(w, err.Error(), 500) | |
return | |
} | |
w.Write([]byte("hi")) | |
}) | |
graceful.AddSignal(syscall.SIGINT, syscall.SIGTERM) | |
graceful.PostHook(func() { | |
ns.Shutdown() | |
log.Println("shutting down..") | |
}) | |
err = graceful.ListenAndServe(":1111", m) | |
if err != nil { | |
log.Fatal(err) | |
} | |
graceful.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment