Skip to content

Commit

Permalink
Merg api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Dec 27, 2020
1 parent b649850 commit 63ff0ec
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 28 deletions.
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"name": "Launch server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/server",
"cwd": "${workspaceFolder}/testing",
"env": {},
"args": ["-config", "config.yaml"]
},
{
"name": "Launch UI",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/ui",
"cwd": "${workspaceFolder}/testing/ui",
"env": {},
"args": ["-config", "config.yaml"]
}
]
}
4 changes: 2 additions & 2 deletions cmd/client/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/MouseHatGames/hat/cmd/client

go 1.15

replace github.com/MouseHatGames/hat/pkg/client => ../../pkg/client

replace github.com/MouseHatGames/hat => ../../

go 1.15

require (
github.com/MouseHatGames/hat/pkg/client v0.0.0-00010101000000-000000000000
github.com/alecthomas/kong v0.2.12
Expand Down
5 changes: 4 additions & 1 deletion cmd/ui/front/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = {
outDir: "../dist"
outDir: "../dist",
proxy: {
"/api/data": "http://localhost:8080/api/data"
}
}
23 changes: 22 additions & 1 deletion cmd/ui/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@ module github.com/MouseHatGames/hat-ui

go 1.15

replace github.com/MouseHatGames/hat/pkg/client => ../../pkg/client

replace github.com/MouseHatGames/hat => ../../

require (
github.com/kataras/bindata v0.0.2 // indirect
github.com/MouseHatGames/hat/pkg/client v0.0.0-00010101000000-000000000000
github.com/ajg/form v1.5.1 // indirect
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/kataras/iris/v12 v12.1.8
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/moul/http2curl v1.0.0 // indirect
github.com/onsi/ginkgo v1.14.2 // indirect
github.com/onsi/gomega v1.10.4 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/valyala/fasthttp v1.18.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)
185 changes: 182 additions & 3 deletions cmd/ui/go.sum

Large diffs are not rendered by default.

34 changes: 28 additions & 6 deletions cmd/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"

"github.com/MouseHatGames/hat-ui/config"
"github.com/MouseHatGames/hat-ui/widget"
"github.com/MouseHatGames/hat/pkg/client"
"github.com/kataras/iris/v12"
)

Expand All @@ -15,20 +17,40 @@ func main() {
log.Fatalf("failed to load configuration: %s", err)
}

hat, err :=
hat, err := client.Dial(cfg.Endpoint)
if err != nil {
log.Fatalf("failed to connect to server: %s", err)
}

app := iris.New()
app.Get("/api/widgets", func(ctx iris.Context) {
ctx.JSON(cfg.WidgetRows)
})
app.Get("/api/data", func(ctx iris.Context) {
data := make(map[string]interface{})
resp := &struct {
Widgets []map[string]*widget.Widget `json:"widgets"`
Data map[string]interface{} `json:"data"`
}{
Widgets: cfg.WidgetRows,
Data: make(map[string]interface{}),
}

for _, row := range cfg.WidgetRows {
for path, w := range row {

clvalue := hat.Get(client.SplitPath(path)...)
if err := clvalue.Error(); err != nil {
//TODO Handle error
continue
}

value, err := w.UnmarshalValue(clvalue.Raw())
if err != nil {
//TODO Handle error
continue
}

resp.Data[path] = value
}
}

ctx.JSON(resp)
})

app.HandleDir("/", "./dist", iris.DirOptions{
Expand Down
64 changes: 50 additions & 14 deletions cmd/ui/widget/widget.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package widget

import "errors"

type WidgetType string

const (
WidgetOnOff WidgetType = "onoff"
WidgetText WidgetType = "text"
WidgetGroup WidgetType = "group"
WidgetOptions WidgetType = "options"
import (
"encoding/json"
"errors"
)

var (
Expand All @@ -17,13 +11,22 @@ var (
ErrMissingChildren = errors.New("missing group children")
ErrMissingOptions = errors.New("missing options")
ErrUnknownType = errors.New("unknown type")
ErrInvalidType = errors.New("invalid type")
)

type WidgetType string

const (
WidgetOnOff WidgetType = "onoff"
WidgetText WidgetType = "text"
WidgetGroup WidgetType = "group"
WidgetOptions WidgetType = "options"
)

type Widget struct {
Title string `json:"title" yaml:"title"`
Type WidgetType `json:"type" yaml:"type"`
Description string `json:"description,omitempty" yaml:"description"`
Value interface{} `json:"value" yaml:"-"`
Title string `json:"title" yaml:"title"`
Type WidgetType `json:"type" yaml:"type"`
Description string `json:"description,omitempty" yaml:"description"`

// Text widget
Placeholder string `json:"placeholder,omitempty" yaml:"placeholder"`
Expand All @@ -33,7 +36,8 @@ type Widget struct {
Children []*Widget `json:"children,omitempty" yaml:"children"`

// Options widget
Options []string `json:"options,omitempty" yaml:"options"`
Options []string `json:"options,omitempty" yaml:"options"`
StoreIndex bool `json:"-" yaml:"storeIndex"`
}

func (w *Widget) IsValid() (valid bool, reason error) {
Expand Down Expand Up @@ -74,3 +78,35 @@ func (w *Widget) IsValid() (valid bool, reason error) {

return true, nil
}

func (w *Widget) UnmarshalValue(str string) (value interface{}, err error) {
b := []byte(str)

switch w.Type {
case WidgetOnOff:
var on bool
err = json.Unmarshal(b, &on)
value = on

case WidgetText:
var str string
err = json.Unmarshal(b, &str)
value = str

case WidgetOptions:
if w.StoreIndex {
var idx int
err = json.Unmarshal(b, &idx)
value = idx
} else {
var sel string
err = json.Unmarshal(b, &sel)
value = sel
}

default:
err = ErrInvalidOptions
}

return
}
29 changes: 29 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/MouseHatGames/hat/internal/proto"
"google.golang.org/grpc"
Expand All @@ -13,6 +14,7 @@ type Client interface {
io.Closer

Get(path ...string) Value
GetBulk(paths [][]string) ([]Value, error)
Set(val string, path ...string) error
Del(path ...string) error
}
Expand All @@ -29,6 +31,11 @@ func Dial(addr string) (Client, error) {
}, nil
}

// SplitPath splits a path (i.e. foo.bar.test) into its parts (i.e. ["foo", "bar", "test"])
func SplitPath(path string) []string {
return strings.Split(path, ".")
}

type client struct {
conn *grpc.ClientConn
cl proto.HatClient
Expand All @@ -46,6 +53,28 @@ func (c *client) Get(path ...string) Value {
return &jsonValue{val: d.Json}
}

func (c *client) GetBulk(paths [][]string) ([]Value, error) {
req := proto.BulkRequest{
Paths: make([]*proto.Path, len(paths)),
}

for i, p := range paths {
req.Paths[i] = &proto.Path{Parts: p}
}

resp, err := c.cl.GetBulk(context.Background(), &req)
if err != nil {
return nil, err
}

values := make([]Value, len(resp.Data))
for i, v := range resp.Data {
values[i] = &jsonValue{val: v.Json}
}

return values, nil
}

func (c *client) Set(val string, path ...string) error {
_, err := c.cl.Set(context.Background(), &proto.SetRequest{
Path: &proto.Path{Parts: path},
Expand Down

0 comments on commit 63ff0ec

Please sign in to comment.