package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
)

var subscriptions map[string][]chan string

func main() {
	subscriptions = make(map[string][]chan string)

	r := mux.NewRouter()

	//r.HandleFunc("", handler)

	r.HandleFunc("/{hashstr}/plot.svg", PlotHandleFunc)
	r.HandleFunc("/{hashstr}/{width}x{height}.svg", EmptyHandleFunc)
	r.HandleFunc("/lock/{hashstr}", EmptyHandleFunc)
	r.HandleFunc("/{hashstr}", FeederHandleFunc)
	r.HandleFunc("/{hashstr}/stream", StreamHandleFunc)

	log.Fatal(http.ListenAndServe(":3001", r))
}

func EmptyHandleFunc(w http.ResponseWriter, r *http.Request) {

}

func PlotHandleFunc(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	hashStr := vars["hashstr"]

	log.Println(hashStr)
}

func FeederHandleFunc(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars("hashstr")

	hasStr := vars["hashstr"]
}

func StreamHandleFunc(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars("hashstr")

	hashStr := vars["hashstr"]

	sendProc := func(hashstr string, writer http.ResponseWriter) {
		q := make(chan string)
		if _, ok := subscriptions[hashstr]; !ok {
			subscriptions[hashstr] = []chan string{q}
		} else {
			subscriptions[hashstr] = append(subscriptions[hashstr], q)
		}

		for {
			writer.Write([]byte(<-q))
		}

		//TODO: handle interrupt and remove from subscriptions

		//delete(subscriptions[hashstr])
	}
}

Изменить пасту