Example #1
0
func main() {
	stream = goose.NewEventStream()

	p := pat.New()
	p.Path("/events").Methods("GET").HandlerFunc(handler)
	p.Path("/notify").Methods("GET").HandlerFunc(someEvent)
	http.Handle("/", p)

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}
}
Example #2
0
func CreateAPIv1(conf *config.Config, r *pat.Router) *APIv1 {
	log.Println("Creating API v1")
	apiv1 := &APIv1{
		config: conf,
	}

	stream = goose.NewEventStream()

	r.Path("/api/v1/messages").Methods("GET").HandlerFunc(apiv1.messages)
	r.Path("/api/v1/messages").Methods("DELETE").HandlerFunc(apiv1.delete_all)
	r.Path("/api/v1/messages").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}").Methods("GET").HandlerFunc(apiv1.message)
	r.Path("/api/v1/messages/{id}").Methods("DELETE").HandlerFunc(apiv1.delete_one)
	r.Path("/api/v1/messages/{id}").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}/download").Methods("GET").HandlerFunc(apiv1.download)
	r.Path("/api/v1/messages/{id}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}/mime/part/{part}/download").Methods("GET").HandlerFunc(apiv1.download_part)
	r.Path("/api/v1/messages/{id}/mime/part/{part}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/messages/{id}/release").Methods("POST").HandlerFunc(apiv1.release_one)
	r.Path("/api/v1/messages/{id}/release").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	r.Path("/api/v1/events").Methods("GET").HandlerFunc(apiv1.eventstream)
	r.Path("/api/v1/events").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions)

	go func() {
		keepaliveTicker := time.Tick(time.Minute)
		for {
			select {
			case msg := <-apiv1.config.MessageChan:
				log.Println("Got message in APIv1 event stream")
				bytes, _ := json.MarshalIndent(msg, "", "  ")
				json := string(bytes)
				log.Printf("Sending content: %s\n", json)
				apiv1.broadcast(json)
			case <-keepaliveTicker:
				apiv1.keepalive()
			}
		}
	}()

	return apiv1
}