Golang unit tests.
This commit is contained in:
parent
a32c041fa5
commit
66c7bddb2e
|
|
@ -1,5 +1,5 @@
|
||||||
web/package-lock.json
|
webui/package-lock.json
|
||||||
web/node_modules
|
webui/node_modules
|
||||||
**/*.swp
|
**/*.swp
|
||||||
**/*.swo
|
**/*.swo
|
||||||
gen/
|
gen/
|
||||||
|
|
|
||||||
19
Makefile
19
Makefile
|
|
@ -1,6 +1,19 @@
|
||||||
compile:
|
compile: daemon-compile
|
||||||
|
|
||||||
|
daemon-compile:
|
||||||
go build -o OliveTin github.com/jamesread/OliveTin/cmd/OliveTin
|
go build -o OliveTin github.com/jamesread/OliveTin/cmd/OliveTin
|
||||||
|
|
||||||
|
daemon-codestyle:
|
||||||
|
go fmt ./...
|
||||||
|
go vet ./...
|
||||||
|
golint ./...
|
||||||
|
gocyclo -over 4 cmd internal
|
||||||
|
|
||||||
|
daemon-unittests:
|
||||||
|
mkdir -p reports
|
||||||
|
go test ./... -coverprofile reports/unittests.out
|
||||||
|
go tool cover -html=reports/unittests.out -o reports/unittests.html
|
||||||
|
|
||||||
grpc:
|
grpc:
|
||||||
protoc -I.:/usr/share/gocode/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ --go_out=plugins=grpc:gen/grpc/ OliveTin.proto
|
protoc -I.:/usr/share/gocode/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ --go_out=plugins=grpc:gen/grpc/ OliveTin.proto
|
||||||
protoc -I.:/usr/share/gocode/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ --grpc-gateway_out=gen/grpc --grpc-gateway_opt paths=source_relative OliveTin.proto
|
protoc -I.:/usr/share/gocode/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/ --grpc-gateway_out=gen/grpc --grpc-gateway_opt paths=source_relative OliveTin.proto
|
||||||
|
|
@ -20,4 +33,8 @@ devrun: compile
|
||||||
|
|
||||||
devcontainer: compile podman-image podman-container
|
devcontainer: compile podman-image podman-container
|
||||||
|
|
||||||
|
webui-codestyle:
|
||||||
|
cd webui && eslint main.js js/*
|
||||||
|
cd webui && stylelint style.css
|
||||||
|
|
||||||
.PHONY: grpc
|
.PHONY: grpc
|
||||||
|
|
|
||||||
|
|
@ -3,25 +3,25 @@ package main
|
||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
restApi "github.com/jamesread/OliveTin/pkg/restApi"
|
grpcapi "github.com/jamesread/OliveTin/internal/grpcapi"
|
||||||
grpcApi "github.com/jamesread/OliveTin/pkg/grpcApi"
|
restapi "github.com/jamesread/OliveTin/internal/restapi"
|
||||||
webuiServer "github.com/jamesread/OliveTin/pkg/webuiServer"
|
webuiServer "github.com/jamesread/OliveTin/internal/webuiServer"
|
||||||
|
|
||||||
config "github.com/jamesread/OliveTin/pkg/config"
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
executor "github.com/jamesread/OliveTin/pkg/executor"
|
executor "github.com/jamesread/OliveTin/internal/executor"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg *config.Config;
|
cfg *config.Config
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.Info("OliveTin initializing");
|
log.Info("OliveTin initializing")
|
||||||
log.SetLevel(log.DebugLevel) // Default to debug, to catch cfg issues
|
log.SetLevel(log.DebugLevel) // Default to debug, to catch cfg issues
|
||||||
|
|
||||||
viper.AutomaticEnv();
|
viper.AutomaticEnv()
|
||||||
viper.SetConfigName("config.yaml")
|
viper.SetConfigName("config.yaml")
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
|
|
@ -29,35 +29,35 @@ func init() {
|
||||||
viper.AddConfigPath("/etc/OliveTin/")
|
viper.AddConfigPath("/etc/OliveTin/")
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
log.Errorf("Config file error at startup. %s", err);
|
log.Errorf("Config file error at startup. %s", err)
|
||||||
os.Exit(1);
|
os.Exit(1)
|
||||||
};
|
}
|
||||||
|
|
||||||
cfg = config.DefaultConfig();
|
cfg = config.DefaultConfig()
|
||||||
|
|
||||||
if err := viper.UnmarshalExact(cfg); err != nil {
|
if err := viper.UnmarshalExact(cfg); err != nil {
|
||||||
log.Errorf("Config unmarshal error %+v", err)
|
log.Errorf("Config unmarshal error %+v", err)
|
||||||
os.Exit(1);
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetLevel(cfg.GetLogLevel())
|
log.SetLevel(cfg.GetLogLevel())
|
||||||
|
|
||||||
viper.WatchConfig();
|
viper.WatchConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.WithFields(log.Fields {
|
log.WithFields(log.Fields{
|
||||||
"listenAddressGrpcActions": cfg.ListenAddressGrpcActions,
|
"listenAddressGrpcActions": cfg.ListenAddressGrpcActions,
|
||||||
"listenAddressRestActions": cfg.ListenAddressRestActions,
|
"listenAddressRestActions": cfg.ListenAddressRestActions,
|
||||||
"listenAddressWebUi": cfg.ListenAddressWebUi,
|
"listenAddressWebUI": cfg.ListenAddressWebUI,
|
||||||
}).Info("OliveTin started");
|
}).Info("OliveTin started")
|
||||||
|
|
||||||
log.Debugf("%+v", cfg)
|
log.Debugf("%+v", cfg)
|
||||||
|
|
||||||
executor.Cfg = cfg;
|
executor.Cfg = cfg
|
||||||
|
|
||||||
go grpcApi.Start(cfg.ListenAddressGrpcActions, cfg)
|
go grpcapi.Start(cfg.ListenAddressGrpcActions, cfg)
|
||||||
go restApi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg)
|
go restapi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg)
|
||||||
|
|
||||||
webuiServer.Start(cfg.ListenAddressWebUi, cfg.ListenAddressRestActions)
|
webuiServer.Start(cfg.ListenAddressWebUI, cfg.ListenAddressRestActions)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
4
go.mod
4
go.mod
|
|
@ -10,10 +10,8 @@ require (
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
github.com/stretchr/testify v1.7.0 // indirect
|
github.com/stretchr/testify v1.7.0 // indirect
|
||||||
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
|
|
||||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
|
|
||||||
golang.org/x/text v0.3.5 // indirect
|
golang.org/x/text v0.3.5 // indirect
|
||||||
golang.org/x/tools v0.1.0 // indirect
|
golang.org/x/tools v0.1.1 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20210224155714-063164c882e6
|
google.golang.org/genproto v0.0.0-20210224155714-063164c882e6
|
||||||
google.golang.org/grpc v1.37.0
|
google.golang.org/grpc v1.37.0
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ActionButton struct {
|
||||||
|
Title string
|
||||||
|
Icon string
|
||||||
|
Shell string
|
||||||
|
CSS map[string]string `mapstructure:"omitempty"`
|
||||||
|
Timeout int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Entity struct {
|
||||||
|
Title string
|
||||||
|
Icon string
|
||||||
|
ActionButtons []ActionButton `mapstructure:"actions"`
|
||||||
|
CSS map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
ListenAddressWebUI string
|
||||||
|
ListenAddressRestActions string
|
||||||
|
ListenAddressGrpcActions string
|
||||||
|
LogLevel string
|
||||||
|
ActionButtons []ActionButton `mapstructure:"actions"`
|
||||||
|
Entities []Entity `mapstructure:"omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
config := Config{}
|
||||||
|
config.ListenAddressWebUI = "0.0.0.0:1337"
|
||||||
|
config.ListenAddressRestActions = "0.0.0.0:1338"
|
||||||
|
config.ListenAddressGrpcActions = "0.0.0.0:1339"
|
||||||
|
config.LogLevel = "INFO"
|
||||||
|
|
||||||
|
return &config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) GetLogLevel() log.Level {
|
||||||
|
switch strings.ToUpper(cfg.LogLevel) {
|
||||||
|
case "INFO":
|
||||||
|
return log.InfoLevel
|
||||||
|
case "WARN":
|
||||||
|
return log.WarnLevel
|
||||||
|
case "DEBUG":
|
||||||
|
return log.DebugLevel
|
||||||
|
default:
|
||||||
|
return log.InfoLevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetLog(t *testing.T) {
|
||||||
|
c := Config{}
|
||||||
|
c.LogLevel = ""
|
||||||
|
|
||||||
|
assert.Equal(t, c.GetLogLevel(), log.InfoLevel, "Info log level should be default")
|
||||||
|
|
||||||
|
c.LogLevel = "INFO"
|
||||||
|
assert.Equal(t, c.GetLogLevel(), log.InfoLevel, "set info log level")
|
||||||
|
|
||||||
|
c.LogLevel = "WARN"
|
||||||
|
assert.Equal(t, c.GetLogLevel(), log.WarnLevel, "set warn log level")
|
||||||
|
|
||||||
|
c.LogLevel = "DEBUG"
|
||||||
|
assert.Equal(t, c.GetLogLevel(), log.DebugLevel, "set debug log level")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateDefaultConfig(t *testing.T) {
|
||||||
|
c := DefaultConfig()
|
||||||
|
|
||||||
|
assert.NotNil(t, c, "Create a default config")
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cors
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AllowCors takes a HTTP handler and adds Access-Control-Allow-Origin headers to
|
||||||
|
// responses.
|
||||||
|
//
|
||||||
|
// Note: HTTP OPTIONS requests (which need to be preflighted" for CORS) are not
|
||||||
|
// handled because this app does not use HTTP PUT/PATCH/etc.
|
||||||
|
func AllowCors(h http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if origin := r.Header.Get("Origin"); origin != "" {
|
||||||
|
log.Debugf("Adding CORS header origin: %v", origin)
|
||||||
|
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
package cors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCors(t *testing.T) {
|
||||||
|
req, _ := http.NewRequest("GET", "/health-check", nil)
|
||||||
|
req.Header.Add("Origin", "1.2.3.4")
|
||||||
|
|
||||||
|
blat := AllowCors(http.FileServer(http.Dir(".")))
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
blat.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusNotFound, rr.Code, "HTTP 404 on CORS")
|
||||||
|
assert.Equal(t, "1.2.3.4", rr.Header().Get("Access-Control-Allow-Origin"), "CORS Header set")
|
||||||
|
}
|
||||||
|
|
@ -1,23 +1,23 @@
|
||||||
package executor
|
package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
config "github.com/jamesread/OliveTin/pkg/config"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
pb "github.com/jamesread/OliveTin/gen/grpc"
|
pb "github.com/jamesread/OliveTin/gen/grpc"
|
||||||
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"context"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Cfg *config.Config;
|
Cfg *config.Config
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExecAction(action string) (*pb.StartActionResponse) {
|
func ExecAction(action string) *pb.StartActionResponse {
|
||||||
res := &pb.StartActionResponse{}
|
res := &pb.StartActionResponse{}
|
||||||
res.TimedOut = false;
|
res.TimedOut = false
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"actionName": action,
|
"actionName": action,
|
||||||
|
|
@ -30,12 +30,12 @@ func ExecAction(action string) (*pb.StartActionResponse) {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields {
|
log.WithFields(log.Fields{
|
||||||
"title": actualAction.Title,
|
"title": actualAction.Title,
|
||||||
"timeout": actualAction.Timeout,
|
"timeout": actualAction.Timeout,
|
||||||
}).Infof("Found action")
|
}).Infof("Found action")
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
cmd := exec.CommandContext(ctx, "sh", "-c", actualAction.Shell)
|
cmd := exec.CommandContext(ctx, "sh", "-c", actualAction.Shell)
|
||||||
|
|
@ -54,7 +54,7 @@ func ExecAction(action string) (*pb.StartActionResponse) {
|
||||||
res.TimedOut = true
|
res.TimedOut = true
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields {
|
log.WithFields(log.Fields{
|
||||||
"stdout": res.Stdout,
|
"stdout": res.Stdout,
|
||||||
"stderr": res.Stderr,
|
"stderr": res.Stderr,
|
||||||
"timedOut": res.TimedOut,
|
"timedOut": res.TimedOut,
|
||||||
|
|
@ -81,5 +81,3 @@ func findAction(actionTitle string) (*config.ActionButton, error) {
|
||||||
|
|
||||||
return nil, errors.New("Action not found")
|
return nil, errors.New("Action not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package grpcapi
|
||||||
|
|
||||||
|
var emojis = map[string]string{
|
||||||
|
"poop": "💩",
|
||||||
|
"smile": "😀",
|
||||||
|
"ping": "📡",
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupHTMLIcon(keyToLookup string) string {
|
||||||
|
if emoji, found := emojis[keyToLookup]; found {
|
||||||
|
return emoji
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyToLookup
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package grpcapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetEmojiByShortName(t *testing.T) {
|
||||||
|
assert.Equal(t, "😀", lookupHTMLIcon("smile"), "Find an eomji by short name")
|
||||||
|
|
||||||
|
assert.Equal(t, "notfound", lookupHTMLIcon("notfound"), "Find an eomji by undefined short name")
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
package grpcapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
ctx "context"
|
||||||
|
pb "github.com/jamesread/OliveTin/gen/grpc"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
|
executor "github.com/jamesread/OliveTin/internal/executor"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
cfg *config.Config
|
||||||
|
)
|
||||||
|
|
||||||
|
type oliveTinAPI struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *oliveTinAPI) StartAction(ctx ctx.Context, req *pb.StartActionRequest) (*pb.StartActionResponse, error) {
|
||||||
|
return executor.ExecAction(req.ActionName), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (*pb.GetButtonsResponse, error) {
|
||||||
|
res := &pb.GetButtonsResponse{}
|
||||||
|
|
||||||
|
for _, action := range cfg.ActionButtons {
|
||||||
|
btn := pb.ActionButton{
|
||||||
|
Title: action.Title,
|
||||||
|
Icon: lookupHTMLIcon(action.Icon),
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Actions = append(res.Actions, &btn)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("getButtons: %v", res)
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Start(listenAddress string, globalConfig *config.Config) {
|
||||||
|
cfg = globalConfig
|
||||||
|
|
||||||
|
lis, err := net.Listen("tcp", listenAddress)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to listen - %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
grpcServer := grpc.NewServer()
|
||||||
|
pb.RegisterOliveTinApiServer(grpcServer, newServer())
|
||||||
|
grpcServer.Serve(lis)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newServer() *oliveTinAPI {
|
||||||
|
server := oliveTinAPI{}
|
||||||
|
return &server
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package grpcapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateApi(t *testing.T) {
|
||||||
|
api := newServer()
|
||||||
|
|
||||||
|
assert.NotNil(t, api, "Create new server")
|
||||||
|
|
||||||
|
cfg = config.DefaultConfig()
|
||||||
|
}
|
||||||
|
|
@ -1,25 +1,24 @@
|
||||||
package restApi;
|
package restapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"google.golang.org/grpc"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"context"
|
"context"
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"google.golang.org/grpc"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
gw "github.com/jamesread/OliveTin/gen/grpc"
|
gw "github.com/jamesread/OliveTin/gen/grpc"
|
||||||
|
|
||||||
cors "github.com/jamesread/OliveTin/pkg/cors"
|
cors "github.com/jamesread/OliveTin/internal/cors"
|
||||||
|
|
||||||
config "github.com/jamesread/OliveTin/pkg/config"
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg *config.Config;
|
cfg *config.Config
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Start(listenAddressRest string, listenAddressGrpc string, globalConfig *config.Config) error {
|
||||||
func Start(listenAddressRest string, listenAddressGrpc string, globalConfig *config.Config) (error) {
|
|
||||||
cfg = globalConfig
|
cfg = globalConfig
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
package staticWebserverForUi
|
package webuiServer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
cors "github.com/jamesread/OliveTin/internal/cors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
cors "github.com/jamesread/OliveTin/pkg/cors"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebUiSettings struct {
|
type WebUISettings struct {
|
||||||
Rest string
|
Rest string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,14 +35,14 @@ func Start(listenAddress string, listenAddressRest string) {
|
||||||
http.Handle("/", cors.AllowCors(http.FileServer(http.Dir(findWebuiDir()))))
|
http.Handle("/", cors.AllowCors(http.FileServer(http.Dir(findWebuiDir()))))
|
||||||
|
|
||||||
http.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) {
|
||||||
ret := WebUiSettings {
|
ret := WebUISettings{
|
||||||
Rest: "http://" + listenAddressRest + "/",
|
Rest: "http://" + listenAddressRest + "/",
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonRet, _ := json.Marshal(ret)
|
jsonRet, _ := json.Marshal(ret)
|
||||||
|
|
||||||
w.Write([]byte(jsonRet));
|
w.Write([]byte(jsonRet))
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(listenAddress, nil));
|
log.Fatal(http.ListenAndServe(listenAddress, nil))
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package webuiServer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetWebuiDir(t *testing.T) {
|
||||||
|
dir := findWebuiDir()
|
||||||
|
|
||||||
|
assert.Equal(t, "./webui", dir, "Finding the webui dir")
|
||||||
|
}
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ActionButton struct {
|
|
||||||
Title string
|
|
||||||
Icon string
|
|
||||||
Shell string
|
|
||||||
Css map[string]string `mapstructure:omitempty`
|
|
||||||
Timeout int
|
|
||||||
}
|
|
||||||
|
|
||||||
type Entity struct {
|
|
||||||
Title string
|
|
||||||
Icon string
|
|
||||||
ActionButtons []ActionButton `mapstructure:"actions"`
|
|
||||||
Css map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
ListenAddressWebUi string
|
|
||||||
ListenAddressRestActions string
|
|
||||||
ListenAddressGrpcActions string
|
|
||||||
LogLevel string
|
|
||||||
ActionButtons []ActionButton `mapstructure:"actions"`
|
|
||||||
Entities []Entity `mapstructure:omitempty`
|
|
||||||
}
|
|
||||||
|
|
||||||
func DefaultConfig() *Config {
|
|
||||||
config := Config{};
|
|
||||||
config.ListenAddressWebUi = "0.0.0.0:1337"
|
|
||||||
config.ListenAddressRestActions = "0.0.0.0:1338"
|
|
||||||
config.ListenAddressGrpcActions = "0.0.0.0:1339"
|
|
||||||
config.LogLevel = "INFO"
|
|
||||||
|
|
||||||
return &config
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *Config) GetLogLevel() (log.Level) {
|
|
||||||
switch (strings.ToUpper(cfg.LogLevel)) {
|
|
||||||
case "INFO": return log.InfoLevel;
|
|
||||||
case "WARN": return log.WarnLevel;
|
|
||||||
case "DEBUG": return log.DebugLevel;
|
|
||||||
default: return log.DebugLevel;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetLog(t *testing.T) {
|
|
||||||
c := Config{}
|
|
||||||
c.LogLevel = ""
|
|
||||||
|
|
||||||
if c.GetLogLevel() != log.InfoLevel {
|
|
||||||
t.Errorf("Info expected")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package cors
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func AllowCors(h http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if origin := r.Header.Get("Origin"); origin != "" {
|
|
||||||
log.Infof("Setting CORS header: %v", origin)
|
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
||||||
|
|
||||||
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
|
|
||||||
preflightHandler(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
h.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func preflightHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
log.Infof("preflight request for %s", r.URL.Path)
|
|
||||||
|
|
||||||
headers := []string{"Content-Type", "Accept"}
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
|
|
||||||
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
|
|
||||||
}
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
package grpcApi;
|
|
||||||
|
|
||||||
import (
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
pb "github.com/jamesread/OliveTin/gen/grpc"
|
|
||||||
"net"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
ctx "context"
|
|
||||||
|
|
||||||
config "github.com/jamesread/OliveTin/pkg/config"
|
|
||||||
executor "github.com/jamesread/OliveTin/pkg/executor"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
cfg *config.Config;
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
type OliveTinApi struct {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *OliveTinApi) StartAction(ctx ctx.Context, req *pb.StartActionRequest) (*pb.StartActionResponse, error) {
|
|
||||||
return executor.ExecAction(req.ActionName), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *OliveTinApi) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (*pb.GetButtonsResponse, error) {
|
|
||||||
res := &pb.GetButtonsResponse{};
|
|
||||||
|
|
||||||
for _, action := range cfg.ActionButtons {
|
|
||||||
btn := pb.ActionButton {
|
|
||||||
Title: action.Title,
|
|
||||||
Icon: lookupHtmlIcon(action.Icon),
|
|
||||||
}
|
|
||||||
|
|
||||||
res.Actions = append(res.Actions, &btn);
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("getButtons: %v", res)
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Start(listenAddress string, globalConfig *config.Config) {
|
|
||||||
cfg = globalConfig
|
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", listenAddress);
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to listen - %v", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
grpcServer := grpc.NewServer();
|
|
||||||
pb.RegisterOliveTinApiServer(grpcServer, newServer());
|
|
||||||
grpcServer.Serve(lis)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newServer() (*OliveTinApi) {
|
|
||||||
server := OliveTinApi {};
|
|
||||||
return &server;
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
package grpcApi
|
|
||||||
|
|
||||||
var emojis = map[string]string {
|
|
||||||
"poop": "💩",
|
|
||||||
"smile": "😀",
|
|
||||||
"ping": "📡",
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupHtmlIcon(keyToLookup string) (string) {
|
|
||||||
if emoji, found := emojis[keyToLookup]; found {
|
|
||||||
return emoji;
|
|
||||||
}
|
|
||||||
|
|
||||||
return keyToLookup;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue