forked from richardbowden/poc_bk_rannumgen
/
main.go
57 lines (43 loc) · 897 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"crypto/rand"
"encoding/base64"
"fmt"
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
"os"
)
const servePortEnv string = "RANNUM_PORT"
const defaultServePort string = "9090"
func genRndNumber(w rest.ResponseWriter, r *rest.Request) {
c := 10
b := make([]byte, c)
rand.Read(b)
rndNum := base64.URLEncoding.EncodeToString(b)
d := struct {
RndNum string
}{
rndNum,
}
w.WriteJson(d)
return
}
func main() {
servePort := os.Getenv(servePortEnv)
if servePort == "" {
servePort = defaultServePort
}
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
rest.Get("/rnd", genRndNumber),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
bindPort := fmt.Sprintf(":%s", servePort)
log.Printf("server listening on %s", bindPort)
log.Fatal(http.ListenAndServe(bindPort, api.MakeHandler()))
}