/
example_app_test.go
53 lines (44 loc) · 1 KB
/
example_app_test.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
package app_test
import (
"errors"
"net"
"net/http"
"os"
"time"
"gopkg.in/crast/app.v0"
)
func Example() {
// Set up an HTTP server
listener, err := net.Listen("tcp", ":80")
if err != nil {
os.Exit(1)
}
// app.Serve is a shortcut for using the listener as a closer and calling Serve()
app.Serve(listener, &http.Server{
Handler: http.NotFoundHandler(), //example, use real handler
})
// You can run one-time tasks too with app.Go
app.Go(func() error {
if !registerMyselfSomewhere() {
return errors.New("Registration failed. Shutting down...")
}
return nil
})
setupHeartbeat()
// main will wait for all goroutines to complete and then run closers.
app.Main()
}
// ping some URL every 10 seconds to let them know we're up
func setupHeartbeat() {
app.Go(func() {
tick := time.NewTicker(10 * time.Second)
app.AddCloser(tick.Stop)
for _ = range tick.C {
http.Get("http://example.com/ping")
}
})
}
func registerMyselfSomewhere() bool {
// do some monitoring thing.
return false
}