Skip to content

Commit

Permalink
Gen maintenance (#25)
Browse files Browse the repository at this point in the history
* updates and gcs fixes

* update test runner

* add dev mode and clean bigcache

* general maintenance

* general maintenance

* protect against negative

* remove logs
  • Loading branch information
steviebps committed Dec 22, 2023
1 parent c29a0f8 commit ca81599
Show file tree
Hide file tree
Showing 17 changed files with 503 additions and 209 deletions.
2 changes: 1 addition & 1 deletion api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type HTTPResponse struct {
Data json.RawMessage `json:"data"`
}

type HTTPErrorAndDataRespone struct {
type HTTPErrorAndDataResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors []string `json:"errors,omitempty"`
}
18 changes: 11 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/hashicorp/go-hclog"
"github.com/steviebps/realm/utils"
)

const DefaultClientTimeout = 15 * time.Second

type ClientConfig struct {
Logger hclog.Logger
Address string
Timeout time.Duration
}

type Client struct {
underlying *http.Client
logger hclog.Logger
address *url.URL
config *ClientConfig
underlying *http.Client
}

func NewClient(c *ClientConfig) (*Client, error) {
Expand All @@ -37,13 +40,14 @@ func NewClient(c *ClientConfig) (*Client, error) {
if logger == nil {
logger = hclog.Default().Named("client")
}
if c.Timeout <= 0 {
c.Timeout = DefaultClientTimeout
}

return &Client{
address: u,
config: c,
logger: logger,
// TODO: add internal client options
underlying: &http.Client{},
underlying: &http.Client{Timeout: c.Timeout},
address: u,
logger: logger,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/client_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var clientDelete = &cobra.Command{
}
defer res.Body.Close()

var httpRes api.HTTPErrorAndDataRespone
var httpRes api.HTTPErrorAndDataResponse
if err := utils.ReadInterfaceWith(res.Body, &httpRes); err != nil {
logger.Error(fmt.Sprintf("could not read response for deleting: %q", args[0]), "error", err.Error())
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/client_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var clientGet = &cobra.Command{
}
defer res.Body.Close()

var httpRes api.HTTPErrorAndDataRespone
var httpRes api.HTTPErrorAndDataResponse
if err := utils.ReadInterfaceWith(res.Body, &httpRes); err != nil {
logger.Error(fmt.Sprintf("could not read response for getting: %q", args[0]), "error", err.Error())
os.Exit(1)
Expand Down
10 changes: 10 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"strconv"
"time"

"github.com/steviebps/realm/utils"
)

Expand All @@ -9,6 +12,13 @@ type RealmConfig struct {
Server ServerConfig `json:"server,omitempty"`
}

func NewDefaultServerConfig() RealmConfig {
return RealmConfig{
Client: ClientConfig{},
Server: ServerConfig{StorageType: "bigcache", StorageOptions: map[string]string{"life_window": strconv.FormatInt(int64(time.Hour*24), 10)}, Port: "8080", Inheritable: true},
}
}

type ServerConfig struct {
StorageType string `json:"storage"`
StorageOptions map[string]string `json:"options"`
Expand Down
36 changes: 24 additions & 12 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var serverCmd = &cobra.Command{
Use: "server",
Short: "Starts realm server",
Long: "Starts realm server",
PreRun: func(cmd *cobra.Command, args []string) {
devMode, _ := cmd.Flags().GetBool("dev")
if !devMode {
cmd.MarkFlagRequired("config")
}
},
Run: func(cmd *cobra.Command, args []string) {
flags := cmd.Flags()
debug, _ := flags.GetBool("debug")
Expand All @@ -40,16 +46,24 @@ var serverCmd = &cobra.Command{
logger.Error(err.Error())
os.Exit(1)
}
if configPath == "" {

devMode, _ := flags.GetBool("dev")
if !devMode && configPath == "" {
logger.Error("config must be specified")
os.Exit(1)
}
var realmConfig RealmConfig

realmConfig, err := parseConfig(configPath)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
if devMode {
realmConfig = NewDefaultServerConfig()
} else {
realmConfig, err = parseConfig(configPath)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}

serverConfig := realmConfig.Server

portStr, err := flags.GetString("port")
Expand Down Expand Up @@ -90,7 +104,9 @@ var serverCmd = &cobra.Command{
for k, v := range serverConfig.StorageOptions {
options = append(options, k, v)
}
logger.Debug("Storage options", options...)
if len(options) > 0 {
logger.Debug("Storage options", options...)
}

stg, err := strgCreator(serverConfig.StorageOptions)
if err != nil {
Expand All @@ -106,12 +122,7 @@ var serverCmd = &cobra.Command{
}
}

if err != nil {
logger.Error(err.Error())
os.Exit(1)
}

handler, err := realmhttp.NewHandler(realmhttp.HandlerConfig{Storage: stg, Logger: logger, RequestTimeout: 5 * time.Second})
handler, err := realmhttp.NewHandler(realmhttp.HandlerConfig{Storage: stg, Logger: logger})
if err != nil {
logger.Error(err.Error())
os.Exit(1)
Expand All @@ -136,5 +147,6 @@ var serverCmd = &cobra.Command{

func init() {
serverCmd.Flags().String("port", "", "port to run server on")
serverCmd.Flags().Bool("dev", false, "run server in dev mode")
rootCmd.AddCommand(serverCmd)
}
14 changes: 9 additions & 5 deletions examples/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"log"
"net/http"
"time"

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

Expand All @@ -18,11 +20,11 @@ type CustomStruct struct {
func main() {
var err error

client, err := client.NewClient(&client.ClientConfig{Address: "http://localhost"})
client, err := client.NewClient(&client.ClientConfig{Address: "http://localhost:8080"})
if err != nil {
log.Fatal(err)
}
rlm, err := realm.NewRealm(realm.RealmOptions{Client: client, ApplicationVersion: "v1.0.0", Path: "root"})
rlm, err := realm.NewRealm(realm.RealmOptions{Client: client, ApplicationVersion: "v1.0.0", Path: "root", RefreshInterval: 1 * time.Minute})
if err != nil {
log.Fatal(err)
}
Expand All @@ -36,13 +38,13 @@ func main() {

mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
message, _ := rlm.String(rlm.NewContext(r.Context()), "message", "DEFAULT")
message, _ := rlm.String(r.Context(), "message", "DEFAULT")
w.Write([]byte(message))
})

mux.HandleFunc("/custom", func(w http.ResponseWriter, r *http.Request) {
var custom *CustomStruct
if err := rlm.CustomValue(rlm.NewContext(r.Context()), "custom", &custom); err != nil {
if err := rlm.CustomValue(r.Context(), "custom", &custom); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
Expand All @@ -51,8 +53,10 @@ func main() {
json.NewEncoder(w).Encode(custom)
})

rlmHandler := realmhttp.RealmHandler(rlm, mux)

log.Println("Listening on :", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", int(port)), mux)
err = http.ListenAndServe(fmt.Sprintf(":%d", int(port)), rlmHandler)
if err != nil {
log.Fatal(err)
}
Expand Down
33 changes: 21 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ module github.com/steviebps/realm
go 1.19

require (
cloud.google.com/go/storage v1.34.1
cloud.google.com/go/storage v1.36.0
github.com/NYTimes/gziphandler v1.1.1
github.com/allegro/bigcache/v3 v3.1.0
github.com/google/uuid v1.4.0
github.com/google/uuid v1.5.0
github.com/spf13/cobra v1.8.0
golang.org/x/mod v0.14.0
google.golang.org/api v0.149.0
google.golang.org/api v0.154.0
)

require (
cloud.google.com/go v0.110.10 // indirect
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
Expand All @@ -26,22 +29,28 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

require (
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-hclog v1.6.2
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
Expand Down
Loading

0 comments on commit ca81599

Please sign in to comment.