gopher/examples/hello/main.go

78 lines
1.3 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
2019-06-09 00:57:17 +00:00
"within.website/gopher"
2016-09-14 03:45:49 +00:00
)
func index(w gopher.ResponseWriter, r *gopher.Request) {
w.WriteItem(&gopher.Item{
Type: gopher.DIRECTORY,
Selector: "/hello",
Description: "hello",
2017-02-13 08:18:27 +00:00
// TLS Resource
Host: "localhost",
Port: 73,
Extras: []string{"TLS"},
})
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
}