-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathmain.go
65 lines (55 loc) · 1.63 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
package main
import (
"context"
"flag"
"fmt"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"github.com/thomaspoignant/go-feature-flag/retriever/fileretriever"
"html/template"
"io"
"net/http"
"time"
)
var users = make(map[string]ffcontext.EvaluationContext, 2500)
func main() {
configFile := flag.String("configFile", "./demo-flags.goff.yaml", "flags.goff.yaml")
flag.Parse()
_ = ffclient.Init(ffclient.Config{
PollingInterval: 1 * time.Second,
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: *configFile,
},
})
e := echo.New()
e.HideBanner = true
e.Static("/js", "js")
e.Static("/css", "css")
// Instantiate a template registry and register all html files inside the view folder
e.Renderer = &TemplateRegistry{templates: template.Must(template.ParseGlob("view/*.html"))}
// init users
for i := 0; i < 2500; i++ {
id := uuid.New()
u := ffcontext.NewEvaluationContext(id.String())
users[fmt.Sprintf("user%d", i)] = u
}
e.GET("/", apiHandler)
e.Logger.Fatal(e.Start(":8080"))
}
type TemplateRegistry struct {
templates *template.Template
}
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func apiHandler(c echo.Context) error {
mapToRender := make(map[string]string, 2500)
for k, user := range users {
color, _ := ffclient.StringVariation("color-box", user, "grey")
mapToRender[k] = color
}
return c.Render(http.StatusOK, "template.html", mapToRender)
}