Created
October 14, 2018 01:08
-
-
Save antzucaro/af01b40ef71adf3043ad2d45436a6dfb to your computer and use it in GitHub Desktop.
RTMP XML parser and HTTP stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"os" | |
) | |
type LiveStreams struct { | |
Applications []struct { | |
Name string `xml:"name"` | |
Live []struct { | |
Stream struct { | |
Name string `xml:"name"` | |
BWIn int `xml:"bw_in"` | |
} `xml:"stream"` | |
} `xml:"live"` | |
} `xml:"server>application"` | |
} | |
func openXMLFile(filename string) ([]byte, error) { | |
f, err := os.Open(filename) | |
if err != nil { | |
return []byte{}, err | |
} | |
xmlBytes, err := ioutil.ReadAll(f) | |
if err != nil { | |
return []byte{}, err | |
} | |
return xmlBytes, nil | |
} | |
func parseXMLFromFile(filename string) { | |
xmlBytes, err := openXMLFile(filename) | |
if err != nil { | |
panic(err) | |
} | |
var streams LiveStreams | |
err = xml.Unmarshal(xmlBytes, &streams) | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Printf("%+v\n", streams) | |
} | |
func serveXML(filename string) { | |
xmlBytes, err := openXMLFile(filename) | |
if err != nil { | |
panic(err) | |
} | |
h := func(w http.ResponseWriter, req *http.Request) { | |
io.WriteString(w, string(xmlBytes)) | |
} | |
http.HandleFunc("/", h) | |
http.ListenAndServe(":8080", nil) | |
} | |
func main() { | |
if len(os.Args) < 3 { | |
fmt.Println("Usage: xml <parse|serve> <filename>") | |
os.Exit(1) | |
} | |
action := os.Args[1] | |
filename := os.Args[2] | |
if action == "parse" { | |
parseXMLFromFile(filename) | |
} else if action == "serve" { | |
serveXML(filename) | |
} else { | |
fmt.Println("Unsupported action. Must be 'parse' or 'serve'.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment