Esempio n. 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
}
Esempio n. 2
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)
}