-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
37 lines (35 loc) · 922 Bytes
/
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
package main
import (
"fmt"
"github.com/donetkit/contrib/discovery"
"github.com/donetkit/contrib/discovery/consul"
"github.com/gin-gonic/gin"
"log"
"net/http"
"time"
)
func main() {
r := gin.New()
consulClient, _ := consul.New(
discovery.WithRegisterAddr("127.0.0.1"),
discovery.WithRegisterPort(8500),
discovery.WithCheckHTTP(func(response *discovery.CheckResponse) {
r.GET(response.Url, func(c *gin.Context) { c.String(200, response.Result()) })
}))
// Example ping request.
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})
r.GET("/register", func(c *gin.Context) {
consulClient.Register()
c.String(http.StatusOK, "ok")
})
r.GET("/deregister", func(c *gin.Context) {
consulClient.Deregister()
c.String(http.StatusOK, "ok")
})
// Listen and Server in 0.0.0.0:8080
if err := r.Run(":8080"); err != nil {
log.Fatal(err)
}
}