/
main.go
71 lines (60 loc) · 1.6 KB
/
main.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
package main
import (
"fmt"
"net/http"
"strconv"
"time"
// We can do this natively just as easily, but this framework makes
// the examples a bit more clear.
"github.com/gin-gonic/gin"
)
var (
beats, port int
heartbeat *time.Ticker
router *gin.Engine
)
// Do all of this stuff first.
func init() {
// In this example, we will hard code the port. Later the environment
// will dictate.
port = 7718
// Set up the heartbeat ticker.
heartbeat = time.NewTicker(60 * time.Second)
// Setup the service router.
gin.SetMode(gin.ReleaseMode)
router = gin.New()
// These are the services we will be listening for.
router.GET("/beats", GetHeartbeatCount)
router.GET("/ping", PingTheAPI)
} // func
// GetHeartbeatCount sends the number of times the heartbeat ticker has
// fired since the program started.
func GetHeartbeatCount(c *gin.Context) {
content := gin.H{"payload": beats}
c.JSON(http.StatusOK, content)
}
// PingTheAPI lets the caller know we are alive.
func PingTheAPI(c *gin.Context) {
content := gin.H{"payload": "pong"}
c.JSON(http.StatusOK, content)
}
// Manage the processes.
func main() {
// Dispatch a process into the background.
go func() {
// Now run it forever.
for {
// Watch for stuff to happen.
select {
// When the Heartbeat ticker is fired, execute this.
case <-heartbeat.C:
beats++
fmt.Printf("bump,Bump... @ %s\n", time.Now().UTC())
} // select
} // for
}() // go func
// After the 'go func' is dispatched, start the server and listen on the
// specified port.
fmt.Printf("ready on port %d\n", port)
router.Run(":" + strconv.Itoa(port))
} // func