This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var yamlExample = []byte(` | |
| {"DB":"SOME_URL","PASSWORD":"pass"} | |
| `) | |
| viper.ReadConfig(bytes.NewBuffer(yamlExample)) | |
| viper.SetConfigType("json") | |
| viper.Get("DB") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
| } | |
| }() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| doc, err := client.Collection("configs").Doc("config").Get(ctx) | |
| if err != nil { | |
| log.Fatalln(err) | |
| } | |
| data := doc.Data() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
NewerOlder