realm

command module
v0.0.24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

realm

release

go install github.com/steviebps/realm

example commands

server
start a local realm server
realm server --config ./configs/realm.json

example code snippets

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/steviebps/realm/client"
	realm "github.com/steviebps/realm/pkg"
)

type CustomStruct struct {
	Foo string `json:"foo,omitempty"`
}

func main() {
	var err error

	// create a realm client for retrieving your chamber from your local or remote host
	client, err := client.NewClient(&client.ClientConfig{Address: "http://localhost"})
	if err != nil {
		log.Fatal(err)
	}

	// initialize your realm 
	rlm, err := realm.NewRealm(realm.RealmOptions{Client: client, ApplicationVersion: "v1.0.0", Path: "root"})
	if err != nil {
		log.Fatal(err)
	}

	// start fetching your chamber from the local or remote host
	err = rlm.Start()
	if err != nil {
		log.Fatal(err)
	}

	// create a realm context
	bootCtx := rlm.NewContext(context.Background())
	// retrieve your first config value
	port, _ := 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 consistency
		message, _ := rlm.String(rlm.NewContext(r.Context()), "message", "DEFAULT")
		w.Write([]byte(message))
	})

	mux.HandleFunc("/custom", func(w http.ResponseWriter, r *http.Request) {
		var custom *CustomStruct
		// retrieve a custom value and unmarshal it
		if err := 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)
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Copyright © 2020

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Directories

Path Synopsis
examples
go
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL