From 66c7bddb2ec0cf6bd63f1a0f999e6cf0c3a6aee7 Mon Sep 17 00:00:00 2001 From: jamesread Date: Tue, 18 May 2021 00:27:48 +0100 Subject: [PATCH] Golang unit tests. --- .gitignore | 4 +- Makefile | 19 +++++- cmd/OliveTin/main.go | 42 +++++++------- go.mod | 4 +- internal/config/config.go | 53 +++++++++++++++++ internal/config/config_test.go | 29 ++++++++++ internal/cors/cors.go | 23 ++++++++ internal/cors/cors_test.go | 22 +++++++ {pkg => internal}/executor/executor.go | 30 +++++----- internal/grpcapi/emoji.go | 15 +++++ internal/grpcapi/emoji_test.go | 12 ++++ internal/grpcapi/grpcApi.go | 59 +++++++++++++++++++ internal/grpcapi/grpcApi_test.go | 16 +++++ {pkg/restApi => internal/restapi}/restapi.go | 15 +++-- {pkg => internal}/webuiServer/webuiServer.go | 14 ++--- internal/webuiServer/webuiServer_test.go | 12 ++++ pkg/config/config.go | 50 ---------------- pkg/config/config_test.go | 15 ----- pkg/cors/cors.go | 33 ----------- pkg/grpcApi/grpcApi.go | 61 -------------------- pkg/grpcApi/utils.go | 15 ----- 21 files changed, 311 insertions(+), 232 deletions(-) create mode 100644 internal/config/config.go create mode 100644 internal/config/config_test.go create mode 100644 internal/cors/cors.go create mode 100644 internal/cors/cors_test.go rename {pkg => internal}/executor/executor.go (74%) create mode 100644 internal/grpcapi/emoji.go create mode 100644 internal/grpcapi/emoji_test.go create mode 100644 internal/grpcapi/grpcApi.go create mode 100644 internal/grpcapi/grpcApi_test.go rename {pkg/restApi => internal/restapi}/restapi.go (82%) rename {pkg => internal}/webuiServer/webuiServer.go (79%) create mode 100644 internal/webuiServer/webuiServer_test.go delete mode 100644 pkg/config/config.go delete mode 100644 pkg/config/config_test.go delete mode 100644 pkg/cors/cors.go delete mode 100644 pkg/grpcApi/grpcApi.go delete mode 100644 pkg/grpcApi/utils.go diff --git a/.gitignore b/.gitignore index a41636c..3ae3997 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -web/package-lock.json -web/node_modules +webui/package-lock.json +webui/node_modules **/*.swp **/*.swo gen/ diff --git a/Makefile b/Makefile index 2357369..4442b7d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,19 @@ -compile: +compile: daemon-compile + +daemon-compile: 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: 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 @@ -20,4 +33,8 @@ devrun: compile devcontainer: compile podman-image podman-container +webui-codestyle: + cd webui && eslint main.js js/* + cd webui && stylelint style.css + .PHONY: grpc diff --git a/cmd/OliveTin/main.go b/cmd/OliveTin/main.go index 3f49526..7816be6 100644 --- a/cmd/OliveTin/main.go +++ b/cmd/OliveTin/main.go @@ -3,25 +3,25 @@ package main import ( log "github.com/sirupsen/logrus" - restApi "github.com/jamesread/OliveTin/pkg/restApi" - grpcApi "github.com/jamesread/OliveTin/pkg/grpcApi" - webuiServer "github.com/jamesread/OliveTin/pkg/webuiServer" + grpcapi "github.com/jamesread/OliveTin/internal/grpcapi" + restapi "github.com/jamesread/OliveTin/internal/restapi" + webuiServer "github.com/jamesread/OliveTin/internal/webuiServer" - config "github.com/jamesread/OliveTin/pkg/config" - executor "github.com/jamesread/OliveTin/pkg/executor" + config "github.com/jamesread/OliveTin/internal/config" + executor "github.com/jamesread/OliveTin/internal/executor" "github.com/spf13/viper" "os" ) var ( - cfg *config.Config; + cfg *config.Config ) func init() { - log.Info("OliveTin initializing"); + log.Info("OliveTin initializing") log.SetLevel(log.DebugLevel) // Default to debug, to catch cfg issues - viper.AutomaticEnv(); + viper.AutomaticEnv() viper.SetConfigName("config.yaml") viper.SetConfigType("yaml") viper.AddConfigPath(".") @@ -29,35 +29,35 @@ func init() { viper.AddConfigPath("/etc/OliveTin/") if err := viper.ReadInConfig(); err != nil { - log.Errorf("Config file error at startup. %s", err); - os.Exit(1); - }; + log.Errorf("Config file error at startup. %s", err) + os.Exit(1) + } - cfg = config.DefaultConfig(); + cfg = config.DefaultConfig() if err := viper.UnmarshalExact(cfg); err != nil { log.Errorf("Config unmarshal error %+v", err) - os.Exit(1); + os.Exit(1) } log.SetLevel(cfg.GetLogLevel()) - viper.WatchConfig(); + viper.WatchConfig() } func main() { - log.WithFields(log.Fields { + log.WithFields(log.Fields{ "listenAddressGrpcActions": cfg.ListenAddressGrpcActions, "listenAddressRestActions": cfg.ListenAddressRestActions, - "listenAddressWebUi": cfg.ListenAddressWebUi, - }).Info("OliveTin started"); + "listenAddressWebUI": cfg.ListenAddressWebUI, + }).Info("OliveTin started") log.Debugf("%+v", cfg) - executor.Cfg = cfg; + executor.Cfg = cfg - go grpcApi.Start(cfg.ListenAddressGrpcActions, cfg) - go restApi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg) + go grpcapi.Start(cfg.ListenAddressGrpcActions, cfg) + go restapi.Start(cfg.ListenAddressRestActions, cfg.ListenAddressGrpcActions, cfg) - webuiServer.Start(cfg.ListenAddressWebUi, cfg.ListenAddressRestActions) + webuiServer.Start(cfg.ListenAddressWebUI, cfg.ListenAddressRestActions) } diff --git a/go.mod b/go.mod index e106975..b109261 100644 --- a/go.mod +++ b/go.mod @@ -10,10 +10,8 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.7.1 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/tools v0.1.0 // indirect + golang.org/x/tools v0.1.1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/genproto v0.0.0-20210224155714-063164c882e6 google.golang.org/grpc v1.37.0 diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..f3b90d7 --- /dev/null +++ b/internal/config/config.go @@ -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 + } +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..98d38d4 --- /dev/null +++ b/internal/config/config_test.go @@ -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") +} diff --git a/internal/cors/cors.go b/internal/cors/cors.go new file mode 100644 index 0000000..9bf6a5f --- /dev/null +++ b/internal/cors/cors.go @@ -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) + }) +} diff --git a/internal/cors/cors_test.go b/internal/cors/cors_test.go new file mode 100644 index 0000000..bfd87e2 --- /dev/null +++ b/internal/cors/cors_test.go @@ -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") +} diff --git a/pkg/executor/executor.go b/internal/executor/executor.go similarity index 74% rename from pkg/executor/executor.go rename to internal/executor/executor.go index d8dedfe..c7ae113 100644 --- a/pkg/executor/executor.go +++ b/internal/executor/executor.go @@ -1,23 +1,23 @@ -package executor +package executor import ( - config "github.com/jamesread/OliveTin/pkg/config" - log "github.com/sirupsen/logrus" pb "github.com/jamesread/OliveTin/gen/grpc" + config "github.com/jamesread/OliveTin/internal/config" + log "github.com/sirupsen/logrus" + "context" "errors" "os/exec" - "context" "time" ) var ( - Cfg *config.Config; + Cfg *config.Config ) -func ExecAction(action string) (*pb.StartActionResponse) { +func ExecAction(action string) *pb.StartActionResponse { res := &pb.StartActionResponse{} - res.TimedOut = false; + res.TimedOut = false log.WithFields(log.Fields{ "actionName": action, @@ -30,12 +30,12 @@ func ExecAction(action string) (*pb.StartActionResponse) { return res } - log.WithFields(log.Fields { - "title": actualAction.Title, + log.WithFields(log.Fields{ + "title": actualAction.Title, "timeout": actualAction.Timeout, }).Infof("Found action") - ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() cmd := exec.CommandContext(ctx, "sh", "-c", actualAction.Shell) @@ -54,11 +54,11 @@ func ExecAction(action string) (*pb.StartActionResponse) { res.TimedOut = true } - log.WithFields(log.Fields { - "stdout": res.Stdout, - "stderr": res.Stderr, + log.WithFields(log.Fields{ + "stdout": res.Stdout, + "stderr": res.Stderr, "timedOut": res.TimedOut, - "exit": res.ExitCode, + "exit": res.ExitCode, }).Infof("Finished command.") return res @@ -81,5 +81,3 @@ func findAction(actionTitle string) (*config.ActionButton, error) { return nil, errors.New("Action not found") } - - diff --git a/internal/grpcapi/emoji.go b/internal/grpcapi/emoji.go new file mode 100644 index 0000000..c31b29f --- /dev/null +++ b/internal/grpcapi/emoji.go @@ -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 +} diff --git a/internal/grpcapi/emoji_test.go b/internal/grpcapi/emoji_test.go new file mode 100644 index 0000000..2b5dafc --- /dev/null +++ b/internal/grpcapi/emoji_test.go @@ -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") +} diff --git a/internal/grpcapi/grpcApi.go b/internal/grpcapi/grpcApi.go new file mode 100644 index 0000000..282ac91 --- /dev/null +++ b/internal/grpcapi/grpcApi.go @@ -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 +} diff --git a/internal/grpcapi/grpcApi_test.go b/internal/grpcapi/grpcApi_test.go new file mode 100644 index 0000000..abcc93c --- /dev/null +++ b/internal/grpcapi/grpcApi_test.go @@ -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() +} diff --git a/pkg/restApi/restapi.go b/internal/restapi/restapi.go similarity index 82% rename from pkg/restApi/restapi.go rename to internal/restapi/restapi.go index 0444f9d..1d5dd30 100644 --- a/pkg/restApi/restapi.go +++ b/internal/restapi/restapi.go @@ -1,25 +1,24 @@ -package restApi; +package restapi import ( - "google.golang.org/grpc" - log "github.com/sirupsen/logrus" "context" "github.com/grpc-ecosystem/grpc-gateway/runtime" + log "github.com/sirupsen/logrus" + "google.golang.org/grpc" "net/http" 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 ( - 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 ctx := context.Background() diff --git a/pkg/webuiServer/webuiServer.go b/internal/webuiServer/webuiServer.go similarity index 79% rename from pkg/webuiServer/webuiServer.go rename to internal/webuiServer/webuiServer.go index b1757cc..990e5bd 100644 --- a/pkg/webuiServer/webuiServer.go +++ b/internal/webuiServer/webuiServer.go @@ -1,14 +1,14 @@ -package staticWebserverForUi +package webuiServer import ( - "net/http" "encoding/json" + cors "github.com/jamesread/OliveTin/internal/cors" log "github.com/sirupsen/logrus" - cors "github.com/jamesread/OliveTin/pkg/cors" + "net/http" "os" ) -type WebUiSettings struct { +type WebUISettings struct { Rest string } @@ -35,14 +35,14 @@ func Start(listenAddress string, listenAddressRest string) { http.Handle("/", cors.AllowCors(http.FileServer(http.Dir(findWebuiDir())))) http.HandleFunc("/webUiSettings.json", func(w http.ResponseWriter, r *http.Request) { - ret := WebUiSettings { + ret := WebUISettings{ Rest: "http://" + listenAddressRest + "/", } jsonRet, _ := json.Marshal(ret) - w.Write([]byte(jsonRet)); + w.Write([]byte(jsonRet)) }) - log.Fatal(http.ListenAndServe(listenAddress, nil)); + log.Fatal(http.ListenAndServe(listenAddress, nil)) } diff --git a/internal/webuiServer/webuiServer_test.go b/internal/webuiServer/webuiServer_test.go new file mode 100644 index 0000000..a8f1e27 --- /dev/null +++ b/internal/webuiServer/webuiServer_test.go @@ -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") +} diff --git a/pkg/config/config.go b/pkg/config/config.go deleted file mode 100644 index 14a993a..0000000 --- a/pkg/config/config.go +++ /dev/null @@ -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; - } -} - diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go deleted file mode 100644 index 74259ef..0000000 --- a/pkg/config/config_test.go +++ /dev/null @@ -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") - } -} diff --git a/pkg/cors/cors.go b/pkg/cors/cors.go deleted file mode 100644 index 0c41c99..0000000 --- a/pkg/cors/cors.go +++ /dev/null @@ -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, ",")) -} diff --git a/pkg/grpcApi/grpcApi.go b/pkg/grpcApi/grpcApi.go deleted file mode 100644 index e5c73eb..0000000 --- a/pkg/grpcApi/grpcApi.go +++ /dev/null @@ -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; -} diff --git a/pkg/grpcApi/utils.go b/pkg/grpcApi/utils.go deleted file mode 100644 index 46f6b89..0000000 --- a/pkg/grpcApi/utils.go +++ /dev/null @@ -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; -}