Skip to content

Instantly share code, notes, and snippets.

View mainawycliffe's full-sized avatar

Maina Wycliffe mainawycliffe

View GitHub Profile
<html>
<head>
<meta charset="utf-8" />
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch('wasm.wasm'), go.importObject).then((result) => {
go.run(result.instance);
});
</script>
var yamlExample = []byte(`
{"DB":"SOME_URL","PASSWORD":"pass"}
`)
viper.ReadConfig(bytes.NewBuffer(yamlExample))
viper.SetConfigType("json")
viper.Get("DB")
func main() {
// set out config format type - we will use json, but you can use yaml, toml, etc.
viper.SetConfigType("json")
// this function will read the config from firestore
go ReadFirestoreConfigs()
// delay to allow the config to be read
time.Sleep(1 * time.Second)
func ReadFirestoreConfigs() {
ctx := context.Background()
// we will use a service account to authenticate and authorize our app
clientOptions := option.WithCredentialsFile("path/to/serviceAccountKey.json")
app, err := firebase.NewApp(ctx, nil, clientOptions)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
go func(){
for {
time.Sleep(time.Second * 5) // delay after each request for 5 seconds
// read config from source
// update viper
viper.ReadConfig(bytes.NewBuffer(jsonData))
}
}()
// listen for changes
streamChanges := client.Doc("configs/config").Snapshots(ctx)
defer streamChanges.Stop()
for {
snap, err := streamChanges.Next()
if err != nil {
log.Fatalln(err)
}
jsonData, err := json.Marshal(snap.Data())
if err != nil {
data := doc.Data()
jsonData, err := json.Marshal(data)
if err != nil {
log.Fatalln(err)
}
err = viper.ReadConfig(bytes.NewBuffer(jsonData))
if err != nil {
log.Fatalln(err)
}
doc, err := client.Collection("configs").Doc("config").Get(ctx)
if err != nil {
log.Fatalln(err)
}
data := doc.Data()
// we will use a service account to authenticate and authorize our app
clientOptions := option.WithCredentialsFile("path/to/serviceAccountKey.json")
app, err := firebase.NewApp(context.TODO(), nil, clientOptions)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
viper.SetEnvPrefix("APP_")
// binds "APP_PORT" or "APP_DB_PORT" to config key "port"
err := viper.BindEnv("port", "PORT", "DB_PORT" )
if err != nil {
log.Fatalf("Error binding env: %v", err)
}