You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"context""encoding/json""fmt""log""net/http""github.com/steviebps/realm/client"
realm "github.com/steviebps/realm/pkg"
)
typeCustomStructstruct {
Foostring`json:"foo,omitempty"`
}
funcmain() {
varerrerror// create a realm client for retrieving your chamber from your local or remote hostclient, err:=client.NewClient(&client.ClientConfig{Address: "http://localhost"})
iferr!=nil {
log.Fatal(err)
}
// initialize your realm rlm, err:=realm.NewRealm(realm.RealmOptions{Client: client, ApplicationVersion: "v1.0.0", Path: "root"})
iferr!=nil {
log.Fatal(err)
}
// start fetching your chamber from the local or remote hosterr=rlm.Start()
iferr!=nil {
log.Fatal(err)
}
// create a realm contextbootCtx:=rlm.NewContext(context.Background())
// retrieve your first config valueport, _:=rlm.Float64(bootCtx, "port", 3000)
mux:=http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r*http.Request) {
// retrieve the message value with a new context// note: use the same context value throughout the request for consistencymessage, _:=rlm.String(rlm.NewContext(r.Context()), "message", "DEFAULT")
w.Write([]byte(message))
})
mux.HandleFunc("/custom", func(w http.ResponseWriter, r*http.Request) {
varcustom*CustomStruct// retrieve a custom value and unmarshal itiferr:=rlm.CustomValue(rlm.NewContext(r.Context()), "custom", &custom); err!=nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(custom)
})
log.Println("Listening on :", port)
err=http.ListenAndServe(fmt.Sprintf(":%d", int(port)), mux)
iferr!=nil {
log.Fatal(err)
}
}