Skip to content

Commit

Permalink
🐛 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
egorgasay committed May 6, 2024
1 parent 9d71919 commit ac5ff2b
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 26 deletions.
6 changes: 6 additions & 0 deletions config/config-servers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# List of servers to balance.
Servers = [
"127.0.0.1:10001",
"127.0.0.1:10002",
"127.0.0.1:10003",
]
15 changes: 12 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,36 @@ type LoggingConfig struct {
}

var _configFlag = flag.String("config", "", "Specify the path to the config file")
var _configServersFlag = flag.String("config-servers", "", "Specify the path to the config file")

var _noSecurity = SecurityConfig{
MandatoryAuthorization: false,
MandatoryEncryption: false,
}

const _defaultPathToConfig = "config/config.toml"
const _defaultPathToServers = "config/config-servers.toml"

func New() (*Config, error) {
flag.Parse()

cfg := &Config{}

var pathToConfig string
if * _configFlag!= "" {
pathToConfig = * _configFlag
if *_configFlag != "" {
pathToConfig = *_configFlag
} else if *_configServersFlag != "" {
pathToConfig = *_configServersFlag
} else {
pathToConfig = _defaultPathToConfig
}

_, err := toml.DecodeFile(pathToConfig, cfg)
_, err := toml.DecodeFile(_defaultPathToServers, &cfg.Balancer)
if err != nil {
return nil, fmt.Errorf("failed to decode config: %w", err)
}

_, err = toml.DecodeFile(pathToConfig, cfg)
if err != nil {
return nil, fmt.Errorf("failed to decode config: %w", err)
}
Expand Down
7 changes: 0 additions & 7 deletions config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ FastHTTP = ""
[Balancer]
On = true

# List of servers to balance.
Servers = [
"127.0.0.1:10001",
"127.0.0.1:10002",
"127.0.0.1:10003",
]

# Balancing only mode.
BalancingOnly = false

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ require (
github.com/brpaz/echozap v1.1.3
github.com/docker/distribution v2.8.3+incompatible
github.com/dolthub/swiss v0.2.1
github.com/egorgasay/gost v0.0.35
github.com/egorgasay/gost v0.0.37
github.com/egorgasay/gotils v0.0.3
github.com/egorgasay/itisadb-go-sdk v0.18.2
github.com/egorgasay/itisadb-go-sdk v0.18.3
github.com/egorgasay/itisadb-shared-proto/go v0.0.0-20240414181125-856cb36fef2a
github.com/go-chi/chi v1.5.5
github.com/golang-jwt/jwt/v5 v5.2.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ github.com/dolthub/swiss v0.2.1 h1:gs2osYs5SJkAaH5/ggVJqXQxRXtWshF6uE0lgR/Y3Gw=
github.com/dolthub/swiss v0.2.1/go.mod h1:8AhKZZ1HK7g18j7v7k6c5cYIGEZJcPn0ARsai8cUrh0=
github.com/egorgasay/gost v0.0.35 h1:w/umGwPDgwH7xv66ua9e0kWI3e/42qFIP9eLqLZCUj4=
github.com/egorgasay/gost v0.0.35/go.mod h1:Qu2cg7WMCbGAuMnVY6EIPQtCfT/itf+Y6m7YcOtk3NA=
github.com/egorgasay/gost v0.0.36 h1:u95vl1PktMu1m5Eyc0U/+y2kBmmJyOVd403PWKL+Y3k=
github.com/egorgasay/gost v0.0.36/go.mod h1:Qu2cg7WMCbGAuMnVY6EIPQtCfT/itf+Y6m7YcOtk3NA=
github.com/egorgasay/gost v0.0.37 h1:WmbyYPIDHNf+miVdkL6T2Y6ACtKDulGoZxKaKkw//oI=
github.com/egorgasay/gost v0.0.37/go.mod h1:Qu2cg7WMCbGAuMnVY6EIPQtCfT/itf+Y6m7YcOtk3NA=
github.com/egorgasay/gotils v0.0.3 h1:udG8DtDya5y3CWPkIbAFcU45sXgDmMQdvV2SORrqJEo=
github.com/egorgasay/gotils v0.0.3/go.mod h1:wZ4NPoSUEi8eYmnltb120w0dwLNmPkm0hJlvwmWj/ao=
github.com/egorgasay/itisadb-go-sdk v0.18.2 h1:gH5LXaXbykO3Bp7YGFXH79RM1I9laDfCMBylPMBOSJY=
github.com/egorgasay/itisadb-go-sdk v0.18.2/go.mod h1:l7xxKdIzhlf/EEIV8TQn9TnmS+R7b4gZA11Z2++caps=
github.com/egorgasay/itisadb-go-sdk v0.18.3 h1:2YEC11/XHZ68DELHTnd7uBMC5o44/YM2mCL99SOSW6Y=
github.com/egorgasay/itisadb-go-sdk v0.18.3/go.mod h1:RDFa1QUUjtegpVgqOvPKTiuKKgNIgnca7Jc3BC9V/Ng=
github.com/egorgasay/itisadb-shared-proto v0.12.0 h1:JzbA8++S3FKEKqroyjJFEj7wqEEQLQQwDUy6EJtXqLo=
github.com/egorgasay/itisadb-shared-proto/go v0.0.0-20240414181125-856cb36fef2a h1:7mxsSnwaipsAYh7TFvJI05F4N7tTvMRPzOYXjwUGHcs=
github.com/egorgasay/itisadb-shared-proto/go v0.0.0-20240414181125-856cb36fef2a/go.mod h1:CJn1R2QKWuyI57jimg5oSu3Y4f5yVGrMofWwBB/akrY=
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
Expand Down
20 changes: 17 additions & 3 deletions internal/cli/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"itisadb/internal/constants"
"strconv"
"strings"

Expand Down Expand Up @@ -51,6 +52,7 @@ const (
_del = "del"
_deleteEl = "delete"
_change = "change"
_server = "server"

_object = "object"
_userLevel = "user.level"
Expand Down Expand Up @@ -85,12 +87,12 @@ func (c *Commands) Do(ctx context.Context, act string, args ...string) (res gost

r := c.set(ctx, cmd)
if r.IsErr() {
return res.ErrNew(InvalidCode, InputExtCode, r.Error().Error())
return res.Err(r.Error())
}

switch savedTo := r.Unwrap(); savedTo {
case setToAll:
return res.Ok(fmt.Sprintf("status: ok, saved on all balancer"))
return res.Ok("status: ok, saved on all balancer")
default:
return res.Ok(fmt.Sprintf("status: ok, saved on server #%d", savedTo))
}
Expand Down Expand Up @@ -129,6 +131,18 @@ func (c *Commands) Do(ctx context.Context, act string, args ...string) (res gost
return res.Ok(fmt.Sprintf("object %s created on server #%d", args[1], serv))
}
return res.ErrNew(InvalidCode, InputExtCode, r.Error().Error())
case _server:
r := itisadb.Internal.AddServer(ctx, c.sdk, args[1])
if r.IsOk() {
return res.Ok("server has been added")
}

err := r.Error()
if err.Is(constants.ErrAlreadyExists) {
return res.Err(gost.NewErrX(0, "server has been already added"))
}

return res.Err(r.Error())
default:
return res.Err(ErrWrongInput)
}
Expand Down Expand Up @@ -353,7 +367,7 @@ func (c *Commands) newObject(ctx context.Context, args []string) (res gost.Resul
Level: itisadb.Level(lvl),
}); r.Switch() {
case gost.IsOk:
return res.Ok(r.Unwrap())
return res.Ok(r.Unwrap().Server())
case gost.IsErr:
return res.Err(r.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s SetCommand) Extract() SetCommand {
------------------- [ MODE ] --- [ LEVEL ] - [ SERVER ]
SET key "value" [ RO | UQ | NX | XX ] [ D | R | S ] [ [0-9]+ ]
SET key "value" [ RO | UQ | NX | XX ] [ R | S ] [ [0-9]+ ]
----------------------------------------------------------------------
Expand Down
14 changes: 8 additions & 6 deletions internal/cli/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ func (h *Handler) Action(c echo.Context) error {
res, err := h.logic.ProcessQuery(c.Request().Context(), cookie.Value, action)
if err != nil {
st, ok := status.FromError(err)
if ok && st.Code().String() == codes.NotFound.String() {
err = errors.New("not found")
} else if st.Message() == "unknown server" {
err = errors.New("unknown server")
} else if ok && st.Code().String() == codes.Unavailable.String() {
err = errors.New("memory balancer is offline")
if ok {
if st.Code().String() == codes.NotFound.String() {
err = errors.New("not found")
} else if st.Message() == "unknown server" {
err = errors.New("unknown server")
} else if st.Code().String() == codes.Unavailable.String() {
err = errors.New("memory balancer is offline")
}
}

var t = schema.Response{Text: err.Error()}
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func New(cfg config.WebAppConfig, storage *storage.Storage, balancer string, lg

return &UseCase{
conn: b, storage: storage, cmds: cmds,
tokens: map[string]string{"itisadb": "itisadb"},
tokens: map[string]string{"itisadb": "itisadb"}, // TODO: ??
logger: lg, mainToken: resp.Token,
}
}
Expand All @@ -69,8 +69,7 @@ func (uc *UseCase) ProcessQuery(ctx context.Context, token string, line string)

res := uc.cmds.Do(withAuth(ctx, token), strings.ToLower(split[0]), split[1:]...)
if res.IsErr() {
uc.logger.Warn(res.Error().Error())
return "", res.Error()
return "", fmt.Errorf("ERROR: %s", res.Error().MessagesSpace())
}

return strings.Replace(strings.Replace(res.Unwrap(), "\n", "<br/>", -1), "\t", "&emsp;", -1), nil
Expand Down
1 change: 1 addition & 0 deletions internal/domains/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Server interface {
Number() int32
IsOffline() bool
Reconnect(ctx context.Context) (res gost.ResultN)
Address() string

appLogic
userLogic
Expand Down
5 changes: 5 additions & 0 deletions internal/service/servers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ func (s *LocalServer) ChangeLevel(ctx context.Context, claims gost.Option[models

return s.Logic.ChangeLevel(ctx, claims, login, level)
}

func (s *LocalServer) Address() string {
return ""
}

4 changes: 4 additions & 0 deletions internal/service/servers/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,7 @@ func fromUsersToInternalUsersSDK(users []models.User) []itisadb.Internal_User {
return sdkUsers

}

func (s *RemoteServer) Address() string {
return s.address
}
7 changes: 7 additions & 0 deletions internal/service/servers/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ func (s *Servers) AddServer(ctx context.Context, address string, force bool) (in
}
}

// check if server is already exists
for _, serv := range s.servers {
if serv.Address() == address {
return 0, constants.ErrAlreadyExists
}
}

// saving last id
f, err := os.OpenFile("servers", os.O_TRUNC|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewSyncer(servers domains.Servers, logger *zap.Logger, repo domains.Storage
logger: logger,
}

f, err := os.OpenFile("sync", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
f, err := os.OpenFile("sync", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return nil, fmt.Errorf("can't open sync file: %w", err)
}
Expand Down

0 comments on commit ac5ff2b

Please sign in to comment.