gopher/examples/hello/main.go

84 lines
1.4 KiB
Go
Raw Normal View History

2016-09-14 03:45:49 +00:00
package main
import (
"log"
2017-02-13 08:18:27 +00:00
"sync"
2016-09-14 03:45:49 +00:00
"github.com/prologic/go-gopher"
)
func index(w gopher.ResponseWriter, r *gopher.Request) {
w.WriteItem(
gopher.Item{
Type: gopher.DIRECTORY,
2016-09-14 03:45:49 +00:00
Selector: "/hello",
Description: "hello",
2017-02-13 08:18:27 +00:00
// TLS Resource
Host: "localhost",
Port: 73,
Extras: []string{"TLS"},
2016-09-14 03:45:49 +00:00
},
)
w.WriteItem(
gopher.Item{
Type: gopher.FILE,
Selector: "/foo",
Description: "foo",
},
)
w.WriteItem(
gopher.Item{
Type: gopher.DIRECTORY,
Selector: "/",
Description: "Floodgap",
Host: "gopher.floodgap.com",
Port: 70,
},
)
2016-09-14 03:45:49 +00:00
}
func hello(w gopher.ResponseWriter, r *gopher.Request) {
w.WriteInfo("Hello World!")
}
func foo(w gopher.ResponseWriter, r *gopher.Request) {
w.Write([]byte("Foo!"))
}
2016-09-14 03:45:49 +00:00
func main() {
2017-02-13 08:18:27 +00:00
wg := &sync.WaitGroup{}
// Standard Server
wg.Add(1)
go func() {
mux := gopher.NewServeMux()
mux.HandleFunc("/", index)
mux.HandleFunc("/foo", foo)
mux.HandleFunc("/hello", hello)
log.Fatal(gopher.ListenAndServe("localhost:70", mux))
wg.Done()
}()
// TLS server
wg.Add(1)
go func() {
mux := gopher.NewServeMux()
mux.HandleFunc("/", index)
mux.HandleFunc("/foo", foo)
mux.HandleFunc("/hello", hello)
log.Fatal(
gopher.ListenAndServeTLS(
"localhost:73", "cert.pem", "key.pem", mux,
),
)
wg.Done()
}()
wg.Wait()
2016-09-14 03:45:49 +00:00
}