Skip to content

Commit

Permalink
Examples: host examples locally
Browse files Browse the repository at this point in the history
Resolves #120
  • Loading branch information
backkem committed Sep 15, 2018
1 parent b489b33 commit b18b3a3
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .codacy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
exclude_paths:
- examples/examples.json
13 changes: 13 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Examples
This directory contains some example use-cases of Pions WebRTC.

## Instructions
You can host the examples locally on your machine as follows:
``` sh
go get github.com/pions/webrtc
cd $GOPATH/src/github.com/pions/webrtc/examples
go run examples.go
```
Note: you can change the port of the server using the ``--address`` flag.

Finally, browse to [localhost](http://localhost) to browse through the examples.
13 changes: 13 additions & 0 deletions examples/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="demo.css">
<script src="demo.js"></script>
</head>
<body>
<div>
{{ template "demo.html" }}
</div>
</body>
</html>
97 changes: 97 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)

// Examples represents the examples loaded from examples.json.
type Examples []Example

// Examples represents an example loaded from examples.json.
type Example struct {
Title string `json:"title"`
Link string `json:"link"`
Description string `json:"description"`
Type string `json:"type"`
}

func main() {
addr := flag.String("address", ":80", "Address to host the HTTP server on.")
flag.Parse()

log.Println("Listening on", *addr)
err := serve(*addr)
if err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}

func serve(addr string) error {
// Load the examples
examples, err := getExamples()
if err != nil {
return err
}

// Load the templates
homeTemplate := template.Must(template.ParseFiles("index.html"))

// Serve the required pages
// DIY 'mux' to avoid additional dependencies
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) > 3 && // 1 / example:2 / link:3 / [ static: 4 ]
parts[1] == "example" {
exampleLink := parts[2]
for _, example := range *examples {
if example.Link != exampleLink {
continue
}
fiddle := filepath.Join(exampleLink, "jsfiddle")
if len(parts[3]) != 0 {
http.StripPrefix("/example/"+exampleLink+"/", http.FileServer(http.Dir(fiddle))).ServeHTTP(w, r)
return
}

temp := template.Must(template.ParseFiles("example.html"))
_, err = temp.ParseFiles(filepath.Join(fiddle, "demo.html"))
if err != nil {
panic(err)
}

temp.Execute(w, example)
return
}
}

// Serve the main page
homeTemplate.Execute(w, examples)
})

// Start the server
return http.ListenAndServe(addr, nil)
}

// getExamples loads the examples from the examples.json file.
func getExamples() (*Examples, error) {
file, err := os.Open("./examples.json")
if err != nil {
return nil, fmt.Errorf("failed to list examples (please run in the examples folder): %v", err)
}

var examples Examples
err = json.NewDecoder(file).Decode(&examples)
if err != nil {
return nil, fmt.Errorf("failed to parse examples: %v", err)
}

return &examples, nil
}
32 changes: 32 additions & 0 deletions examples/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"title": "Data channels",
"link": "data-channels",
"description": "data-channels is a pion-WebRTC application that shows how you can send/recv DataChannel messages from a web browser.",
"type": "browser"
},
{
"title": "Data channels create",
"link": "data-channels-create",
"description": "data-channels-create is a pion-WebRTC application that shows how you can send/recv DataChannel messages from a web browser. The difference with the data-channels example is that the datachannel is initialized from the pion side in this example.",
"type": "browser"
},
{
"title": "Gstreamer receive",
"link": "gstreamer-receive",
"description": "gstreamer-receive is a simple application that shows how to receive media using pion-WebRTC and play live using GStreamer.",
"type": "browser"
},
{
"title": "Gstreamer send",
"link": "gstreamer-send",
"description": "gstreamer-send is a simple application that shows how to send video to your browser using pion-WebRTC and GStreamer.",
"type": "browser"
},
{
"title": "Save-to-disk",
"link": "save-to-disk",
"description": "save-to-disk is a simple application that shows how to record your webcam using pion-WebRTC and save to disk.",
"type": "browser"
}
]
15 changes: 15 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>pions/webrtc examples!</title>
</head>
<body>
<ol>
{{range .}}
<li><a href="/example/{{ .Link }}/">{{ .Title }}</a>: {{ .Description }}</li>
{{else}}
<li><strong>No examples found!</strong></li>
{{end}}
</ol>
</body>
</html>

0 comments on commit b18b3a3

Please sign in to comment.