Example #1
0
func parseRequest(w http.ResponseWriter, r *http.Request) (runMode string, s generator.Stream, err error) {
	urlBase := strings.Split(r.URL.Path, ".")[0]
	pathComps := strings.Split(urlBase, "/")
	runMode = "load"
	identifier := urlBase

	if len(pathComps) > 1 {
		runMode = pathComps[0]
		identifier = pathComps[1]
	}

	if runMode == "function" {
		functionId := identifier
		instructionString := functionId + "?" + r.URL.RawQuery
		si := generator.InstructionFromString(instructionString)
		s = generator.NewStream("temporary-stream", si)
		err = nil
	} else {
		s, err = generator.LoadStream(identifier)
		if err != nil && r.Method == "GET" {
			w.WriteHeader(404)
			w.Header().Add("Content-Type", "text/plain")
			fmt.Fprintln(w, "stream not found")
		}
	}

	w.Header().Add("Content-Type", "application/x-mpegurl")
	return
}
Example #2
0
func TestStreamController(t *testing.T) {
	stream := generator.NewStream("stream-controller-test-stream", generator.StreamInstruction{
		FunctionId: "aws",
		Args: map[string]string{
			"key": "justin-martin/lezgo",
		},
	})
	stream.ToChannel().Save()

	req, err := http.NewRequest("GET", "http://localhost:8080/stream-controller-test-stream", nil)
	if err != nil {
		t.Error("Failed to create test request:", err)
	}

	w := httptest.NewRecorder()
	gc := StreamController{}
	gc.ServeHTTP(w, req)

	if w.Code > 299 || w.Code < 200 {
		t.Error("Error: Expected 200 response but got", w.Code)
	}

	req, err = http.NewRequest("GET", "http://localhost:8080/this-stream-should-not-exist", nil)
	if err != nil {
		t.Error("Failed to create test request:", err)
	}

	w = httptest.NewRecorder()
	gc.ServeHTTP(w, req)

	if w.Code != 404 {
		t.Error("Expecting 404 response but got", w.Code)
	}
}
Example #3
0
func (sc StreamController) PostStream(w http.ResponseWriter, r *http.Request) {
	_, stream, err := parseRequest(w, r)
	body, bodyErr := ioutil.ReadAll(r.Body)
	if bodyErr != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	instructionString := string(body)
	if unescaped, err := url.QueryUnescape(instructionString); err != nil {
		instructionString = unescaped
	}
	instruction := generator.InstructionFromString(instructionString)

	if err != nil {
		ns := generator.NewStream(stream.Identifier, instruction)
		ns.Save()
	} else {
		stream.Instruction = instruction
		stream.Save()
	}

	sc.GetStream(w, r)
}