-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (78 loc) · 2.29 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
flagd "github.com/open-feature/go-sdk-contrib/providers/flagd/pkg"
"github.com/open-feature/go-sdk/pkg/openfeature"
)
const defaultMessage = "Hello hello!"
const newWelcomeMessage = "Hello, welcome to this OpenFeature-enabled website!"
func init() {
viper.AutomaticEnv()
viper.SetDefault("API_PORT", 7357)
viper.SetDefault("API_HOST", "")
viper.SetDefault("FLAGD_HOST", "localhost")
viper.SetDefault("FLAGD_PORT", 8013)
}
func main() {
cancelChan := make(chan os.Signal, 1)
stop := make(chan struct{})
// catch SIGETRM or SIGINTERRUPT
signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
_, _ = fmt.Fprintln(os.Stdout, "🚀 Starting server...")
// Use flagd as the OpenFeature provider
flagdHost := viper.GetString("FLAGD_HOST")
flagdPort := viper.GetUint16("FLAGD_PORT")
provider := flagd.NewProvider(
flagd.WithHost(flagdHost),
flagd.WithPort(flagdPort))
err := openfeature.SetProvider(provider)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error setting OpenFeature provider: %s\n", err)
os.Exit(1)
}
// Initialize OpenFeature openFeatureClient
openFeatureClient := openfeature.NewClient("GoStartApp")
// Initialize Go Gin
engine := gin.Default()
// Setup a simple endpoint
engine.GET("", func(c *gin.Context) {
c.Data(http.StatusOK, "text/plain; charset=utf-8", []byte("Test server 😊"))
return
})
engine.GET("/hello", func(c *gin.Context) {
// Evaluate welcome-message feature flag
welcomeMessage, _ := openFeatureClient.BooleanValue(
context.Background(), "welcome-message", false, openfeature.EvaluationContext{},
)
if welcomeMessage {
c.JSON(http.StatusOK, newWelcomeMessage)
return
} else {
c.JSON(http.StatusOK, defaultMessage)
return
}
})
go func() {
apiHost := viper.GetString("API_HOST")
apiPort := viper.GetString("API_PORT")
if apiHost != "" && apiPort != "" {
apiAddress := fmt.Sprintf("%s:%s", apiHost, apiPort)
_ = engine.Run(apiAddress)
} else {
_ = engine.Run()
}
sig := <-cancelChan
_, _ = fmt.Println()
_, _ = fmt.Println(sig)
stop <- struct{}{}
}()
<-cancelChan
_, _ = fmt.Fprintln(os.Stdout, "\n⛔ Abort signal detected\nShutting down gracefully 😎")
}