-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (35 loc) · 881 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
38
39
40
41
42
43
package main
import (
"fmt"
"net/http"
"strconv"
"github.com/MrM21632/snowball/snowball"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func prometheusHandler() gin.HandlerFunc {
h := promhttp.Handler()
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
// Change to false if you want to provide pre-assigned IDs for servers
node, err := snowball.InitNode(true)
if err != nil {
fmt.Printf("Encountered error while initializing node: %s", err)
return
}
r := gin.Default() // Request routing
p := gin.New() // Metrics routing
p.Use(prometheusHandler())
r.SetTrustedProxies(nil)
r.POST("/generate", func(c *gin.Context) {
id := node.GenerateID()
c.JSON(http.StatusOK, gin.H{"id": strconv.FormatUint(uint64(id), 10)})
})
go func() {
p.Run(":9100")
}()
r.Run(":8080")
}