forked from rcarmo/go-http-mdns
/
web.go
84 lines (70 loc) · 1.95 KB
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"github.com/hashicorp/mdns"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"html/template"
"net/http"
"os"
"strings"
)
var templates = template.Must(template.ParseFiles("views/index.html"))
type EnvVar struct {
Name string
Value string
}
type Context struct {
Hostname string
Env []EnvVar
Peers []string
}
var c Context
var port int
func renderIndex(wc web.C, w http.ResponseWriter, r *http.Request) {
_ = templates.ExecuteTemplate(w, "index.html", &c)
}
func main() {
c.Hostname, _ = os.Hostname()
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
c.Env = append(c.Env, EnvVar{pair[0], pair[1]})
}
fmt.Println(c)
static := web.New()
static.Get("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.Handle("/static/", static)
goji.Get("/", renderIndex)
// Setup our service export
host, _ := os.Hostname()
info := []string{"simple peer discovery test"}
service, _ := mdns.NewMDNSService(host, "_http._tcp", "", "", 8000, nil, info)
// Create the mDNS server, defer shutdown
fmt.Println("Creating mDNS server")
server, _ := mdns.NewServer(&mdns.Config{Zone: service})
defer server.Shutdown()
// Make a channel for results and start listening
peers := make(chan *mdns.ServiceEntry, 4)
go func() {
fmt.Println("Waiting for peers")
for entry := range peers {
known := false
for _, e := range c.Peers {
if entry.Name == e {
known = true
}
}
if known == false {
fmt.Println("Got new peer", entry.Name)
c.Peers = append(c.Peers, entry.Name)
}
}
}()
fmt.Println("Creating mDNS listener")
//mdns.Lookup("_googlecast._tcp", peers)
// this will panic without IF_MULTICAST, causing the program to exit
mdns.Lookup("_http._tcp", peers)
fmt.Println("Creating HTTP server")
goji.Serve() // port 8000 by default
close(peers)
}