lessmore/node/api.go

40 lines
639 B
Go
Raw Normal View History

2017-02-12 13:19:02 +03:00
package node
import (
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
// ListTXTHandler ...
func (es ESConf) ListTXTHandler(w http.ResponseWriter, r *http.Request) {
2017-02-13 10:24:21 +03:00
ch := make(chan []byte)
2017-02-12 13:19:02 +03:00
// Get echolist
2017-02-13 10:24:21 +03:00
go func() {
ch <- es.GetListTXT()
}()
echoes := <-ch
2017-02-12 13:19:02 +03:00
w.WriteHeader(200)
w.Write(echoes)
}
// Serve ...
func Serve(listen string, es ESConf) {
r := mux.NewRouter()
r.HandleFunc("/list.txt", es.ListTXTHandler)
http.Handle("/list.txt", r)
srv := http.Server{
Handler: r,
Addr: listen,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}