diff --git a/hw12_13_14_15_calendar/Makefile b/hw12_13_14_15_calendar/Makefile index 733d4d1..60ddfd8 100644 --- a/hw12_13_14_15_calendar/Makefile +++ b/hw12_13_14_15_calendar/Makefile @@ -1,8 +1,8 @@ build: - go build -o ./bin/calendar ./cmd/calendar + go build -o ./bin/calendar ./src test: - go test -race ./internal/... ./pkg/... + go test -race ./src/... install-lint-deps: (which golangci-lint > /dev/null) || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0 diff --git a/hw12_13_14_15_calendar/internal/storage/memory/storage.go b/hw12_13_14_15_calendar/internal/storage/memory/storage.go deleted file mode 100644 index bbc033d..0000000 --- a/hw12_13_14_15_calendar/internal/storage/memory/storage.go +++ /dev/null @@ -1,14 +0,0 @@ -package memorystorage - -import "sync" - -type Storage struct { - // TODO - mu sync.RWMutex -} - -func New() *Storage { - return &Storage{} -} - -// TODO diff --git a/hw12_13_14_15_calendar/internal/storage/memory/storage_test.go b/hw12_13_14_15_calendar/internal/storage/memory/storage_test.go deleted file mode 100644 index 7e303bc..0000000 --- a/hw12_13_14_15_calendar/internal/storage/memory/storage_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package memorystorage - -import "testing" - -func TestStorage(t *testing.T) { - // TODO -} diff --git a/hw12_13_14_15_calendar/internal/storage/sql/storage.go b/hw12_13_14_15_calendar/internal/storage/sql/storage.go deleted file mode 100644 index 550e449..0000000 --- a/hw12_13_14_15_calendar/internal/storage/sql/storage.go +++ /dev/null @@ -1,21 +0,0 @@ -package sqlstorage - -import "context" - -type Storage struct { - // TODO -} - -func New() *Storage { - return &Storage{} -} - -func (s *Storage) Connect(ctx context.Context) error { - // TODO - return nil -} - -func (s *Storage) Close(ctx context.Context) error { - // TODO - return nil -} diff --git a/hw12_13_14_15_calendar/internal/app/app.go b/hw12_13_14_15_calendar/src/app/app.go similarity index 100% rename from hw12_13_14_15_calendar/internal/app/app.go rename to hw12_13_14_15_calendar/src/app/app.go diff --git a/hw12_13_14_15_calendar/internal/config/config.go b/hw12_13_14_15_calendar/src/config/config.go similarity index 67% rename from hw12_13_14_15_calendar/internal/config/config.go rename to hw12_13_14_15_calendar/src/config/config.go index d876311..9a94e24 100644 --- a/hw12_13_14_15_calendar/internal/config/config.go +++ b/hw12_13_14_15_calendar/src/config/config.go @@ -7,28 +7,32 @@ import ( ) type Config struct { - Logger struct{ - File string - Level string + Logger struct { + File string + Level string MuteStdout bool } Storage struct { In_memory bool - Sql_host string - Sql_port string + Sql_host string + Sql_port string Sql_dbase string - Sql_user string - Sql_pass string + Sql_user string + Sql_pass string } } // Confita может быти и хороша, но она не возвращает ошибки, если не может распарсить файл в структуру. Мне не нравится такая "молчаливость" func NewConfig(configFile string) (Config, error) { - f,err:= os.Open(configFile) - if err != nil {return Config{}, err} + f, err := os.Open(configFile) + if err != nil { + return Config{}, err + } defer f.Close() - s,err := ioutil.ReadAll(f) - if err != nil {return Config{}, err} + s, err := ioutil.ReadAll(f) + if err != nil { + return Config{}, err + } var config Config _, err = toml.Decode(string(s), &config) return config, err diff --git a/hw12_13_14_15_calendar/internal/config/config_test.go b/hw12_13_14_15_calendar/src/config/config_test.go similarity index 61% rename from hw12_13_14_15_calendar/internal/config/config_test.go rename to hw12_13_14_15_calendar/src/config/config_test.go index 9d9d14c..a9fb374 100644 --- a/hw12_13_14_15_calendar/internal/config/config_test.go +++ b/hw12_13_14_15_calendar/src/config/config_test.go @@ -16,7 +16,9 @@ var _ = func() bool { func TestNewConfig(t *testing.T) { badfile, err := ioutil.TempFile("", "conf.") - if err != nil { oslog.Fatal(err) } + if err != nil { + oslog.Fatal(err) + } defer os.Remove(badfile.Name()) badfile.WriteString(`aefSD sadfg @@ -25,7 +27,9 @@ V`) badfile.Sync() goodfile, err := ioutil.TempFile("", "conf.") - if err != nil { oslog.Fatal(err) } + if err != nil { + oslog.Fatal(err) + } defer os.Remove(goodfile.Name()) goodfile.WriteString(`[storage] in_memory = true @@ -33,22 +37,22 @@ sql_host = "localhost"`) goodfile.Sync() t.Run("No such file", func(t *testing.T) { - c,e := NewConfig("adfergdth") - require.Equal(t,Config{},c) - require.Error(t,e) + c, e := NewConfig("adfergdth") + require.Equal(t, Config{}, c) + require.Error(t, e) }) t.Run("Bad file", func(t *testing.T) { - c,e := NewConfig(badfile.Name()) - require.Equal(t,Config{},c) - require.Error(t,e) + c, e := NewConfig(badfile.Name()) + require.Equal(t, Config{}, c) + require.Error(t, e) }) t.Run("TOML reading", func(t *testing.T) { - c,e := NewConfig(goodfile.Name()) - require.Equal(t,true,c.Storage.In_memory) - require.Equal(t,"localhost",c.Storage.Sql_host) - require.NoError(t,e) + c, e := NewConfig(goodfile.Name()) + require.Equal(t, true, c.Storage.In_memory) + require.Equal(t, "localhost", c.Storage.Sql_host) + require.NoError(t, e) }) } diff --git a/hw12_13_14_15_calendar/internal/logger/logger.go b/hw12_13_14_15_calendar/src/logger/logger.go similarity index 85% rename from hw12_13_14_15_calendar/internal/logger/logger.go rename to hw12_13_14_15_calendar/src/logger/logger.go index 459c010..a4f2642 100644 --- a/hw12_13_14_15_calendar/internal/logger/logger.go +++ b/hw12_13_14_15_calendar/src/logger/logger.go @@ -3,7 +3,7 @@ package logger import ( "errors" amitralog "github.com/amitrai48/logger" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/config" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config" "log" "os" "strings" @@ -23,7 +23,7 @@ type Log struct { func New(conf config.Config) (Log, error) { - if conf.Logger.File=="" || !validLevel(conf.Logger.Level) { + if conf.Logger.File == "" || !validLevel(conf.Logger.Level) { return Log{}, errors.New("invalid logger config") } @@ -66,11 +66,11 @@ func (l *Log) Fatal(format string, args ...interface{}) { } func validLevel(level string) bool { - l := []string{"debug","info","warn","error","fatal"} - for _,v:=range l { - if strings.ToLower(level)==v { + l := []string{"debug", "info", "warn", "error", "fatal"} + for _, v := range l { + if strings.ToLower(level) == v { return true } } return false -} \ No newline at end of file +} diff --git a/hw12_13_14_15_calendar/internal/logger/logger_test.go b/hw12_13_14_15_calendar/src/logger/logger_test.go similarity index 69% rename from hw12_13_14_15_calendar/internal/logger/logger_test.go rename to hw12_13_14_15_calendar/src/logger/logger_test.go index f7dd5fb..14288ec 100644 --- a/hw12_13_14_15_calendar/internal/logger/logger_test.go +++ b/hw12_13_14_15_calendar/src/logger/logger_test.go @@ -2,7 +2,7 @@ package logger import ( "github.com/stretchr/testify/require" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/config" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config" "io/ioutil" oslog "log" "os" @@ -12,7 +12,9 @@ import ( func TestLoggerLogic(t *testing.T) { tmpfile, err := ioutil.TempFile("", "log.") - if err != nil { oslog.Fatal(err) } + if err != nil { + oslog.Fatal(err) + } defer os.Remove(tmpfile.Name()) conf := config.Config{Logger: struct { @@ -21,16 +23,20 @@ func TestLoggerLogic(t *testing.T) { MuteStdout bool }{File: tmpfile.Name(), Level: "warn", MuteStdout: false}} log, err := New(conf) - if err != nil { oslog.Fatal(err) } + if err != nil { + oslog.Fatal(err) + } t.Run("Messages arround the level", func(t *testing.T) { log.Debug("debug message") log.Error("error message") - res,err := ioutil.ReadAll(tmpfile) - if err != nil { oslog.Fatal(err) } - require.Less(t,strings.Index(string(res),"debug message"),0) - require.Greater(t,strings.Index(string(res),"error message"),0) + res, err := ioutil.ReadAll(tmpfile) + if err != nil { + oslog.Fatal(err) + } + require.Less(t, strings.Index(string(res), "debug message"), 0) + require.Greater(t, strings.Index(string(res), "error message"), 0) }) } @@ -42,7 +48,7 @@ func TestLoggerNegative(t *testing.T) { MuteStdout bool }{File: "", Level: "debug", MuteStdout: true}} _, err := New(conf) - require.Error(t,err, "invalid logger config") + require.Error(t, err, "invalid logger config") }) t.Run("Bad level", func(t *testing.T) { @@ -52,6 +58,6 @@ func TestLoggerNegative(t *testing.T) { MuteStdout bool }{File: "asdafad", Level: "wegretryjt", MuteStdout: true}} _, err := New(conf) - require.Error(t,err,"invalid logger config") + require.Error(t, err, "invalid logger config") }) -} \ No newline at end of file +} diff --git a/hw12_13_14_15_calendar/cmd/calendar/main.go b/hw12_13_14_15_calendar/src/main.go similarity index 51% rename from hw12_13_14_15_calendar/cmd/calendar/main.go rename to hw12_13_14_15_calendar/src/main.go index 91a04dc..09ef456 100644 --- a/hw12_13_14_15_calendar/cmd/calendar/main.go +++ b/hw12_13_14_15_calendar/src/main.go @@ -2,14 +2,14 @@ package main import ( "flag" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/app" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/logger" + internalhttp "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/server/http" + store "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage" oslog "log" "os" "os/signal" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/config" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/app" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/logger" - internalhttp "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/server/http" - memorystorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/internal/storage/memory" ) var configFile string @@ -21,13 +21,21 @@ func init() { func main() { conf, err := config.NewConfig(configFile) - if err != nil { oslog.Fatal("не удалось открыть файл конфигурации:", err.Error())} + if err != nil { + oslog.Fatal("не удалось открыть файл конфигурации:", err.Error()) + } log, err := logger.New(conf) - if err != nil { oslog.Fatal("не удалось запустить логер:", err.Error())} + if err != nil { + oslog.Fatal("не удалось запустить логер:", err.Error()) + } - storage := memorystorage.New() - calendar := app.New(log, storage) + st := store.NewStore(conf) + if err != nil { + oslog.Fatal("не удалось инициализировать хранилище:", err.Error()) + } + + calendar := app.New(log, st) server := internalhttp.NewServer(calendar) diff --git a/hw12_13_14_15_calendar/internal/server/http/middleware.go b/hw12_13_14_15_calendar/src/server/http/middleware.go similarity index 100% rename from hw12_13_14_15_calendar/internal/server/http/middleware.go rename to hw12_13_14_15_calendar/src/server/http/middleware.go diff --git a/hw12_13_14_15_calendar/internal/server/http/server.go b/hw12_13_14_15_calendar/src/server/http/server.go similarity index 100% rename from hw12_13_14_15_calendar/internal/server/http/server.go rename to hw12_13_14_15_calendar/src/server/http/server.go diff --git a/hw12_13_14_15_calendar/internal/storage/event.go b/hw12_13_14_15_calendar/src/storage/event/event.go similarity index 64% rename from hw12_13_14_15_calendar/internal/storage/event.go rename to hw12_13_14_15_calendar/src/storage/event/event.go index 39b2d1a..eb9e0eb 100644 --- a/hw12_13_14_15_calendar/internal/storage/event.go +++ b/hw12_13_14_15_calendar/src/storage/event/event.go @@ -1,7 +1,7 @@ -package storage +package event type Event struct { ID string Title string - // TODO + Date string } diff --git a/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go b/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go new file mode 100644 index 0000000..9e3567a --- /dev/null +++ b/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go @@ -0,0 +1,35 @@ +package memorystorage + +import ( + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event" + "sync" +) + +type Storage struct { + events []event.Event + mu sync.RWMutex +} + +func New() *Storage { + return &Storage{} +} + +func (s Storage) Save(event event.Event) error { + return nil +} + +func (s Storage) Update(event event.Event) error { + return nil +} + +func (s Storage) Delete(event event.Event) error { + return nil +} + +func (s Storage) List() []event.Event { + return []event.Event{} +} + +func (s Storage) Get(id string) (event.Event, bool) { + return event.Event{}, false +} diff --git a/hw12_13_14_15_calendar/src/storage/sql/sqlstorage.go b/hw12_13_14_15_calendar/src/storage/sql/sqlstorage.go new file mode 100644 index 0000000..cbb1714 --- /dev/null +++ b/hw12_13_14_15_calendar/src/storage/sql/sqlstorage.go @@ -0,0 +1,51 @@ +package sqlstorage + +import ( + "context" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event" + "sync" +) + +type Storage struct { + Events []event.Event + Mu sync.RWMutex +} + +func New() *Storage { + return &Storage{} +} + +func (s *Storage) Connect(ctx context.Context) error { + // TODO + return nil +} + +func (s *Storage) Close(ctx context.Context) error { + // TODO + return nil +} + +func (s Storage) Save(e event.Event) error { + if _, ok := s.Get(e.ID); !ok { + s.Mu.Lock() + s.Events = append(s.Events, e) + s.Mu.Unlock() + } + return nil +} + +func (s Storage) Update(event event.Event) error { + return nil +} + +func (s Storage) Delete(event event.Event) error { + return nil +} + +func (s Storage) List() []event.Event { + return []event.Event{} +} + +func (s Storage) Get(id string) (event.Event, bool) { + return event.Event{}, false +} diff --git a/hw12_13_14_15_calendar/src/storage/store.go b/hw12_13_14_15_calendar/src/storage/store.go new file mode 100644 index 0000000..44e75bc --- /dev/null +++ b/hw12_13_14_15_calendar/src/storage/store.go @@ -0,0 +1,24 @@ +package store + +import ( + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event" + memorystorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/memory" + sqlstorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/sql" +) + +type StoreInterface interface { + Save(event event.Event) error + Update(event event.Event) error + Delete(event event.Event) error + List() []event.Event + Get(id string) (event.Event, bool) +} + +func NewStore(config config.Config) StoreInterface { + if config.Storage.In_memory { + st := memorystorage.New() + return st + } + return sqlstorage.New() +}