From 195c5ec84a623766af33b604ac0f1afb43f10cf4 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Tue, 1 Sep 2020 15:52:51 +0300 Subject: [PATCH] =?UTF-8?q?HW12=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D1=83?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B1=D0=BE=D0=BB=D0=B5=D0=B5=20=D0=BF=D1=80=D0=B8=D0=B2=D1=8B?= =?UTF-8?q?=D1=87=D0=BD=D1=83=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw12_13_14_15_calendar/configs/config.toml | 20 +++++++------- hw12_13_14_15_calendar/src/config/config.go | 17 ++++++------ .../src/config/config_test.go | 8 +++--- hw12_13_14_15_calendar/src/logger/logger.go | 26 +++++++++---------- .../src/logger/logger_test.go | 4 +-- hw12_13_14_15_calendar/src/main.go | 11 ++++---- .../src/server/http/middleware.go | 14 ++++------ .../src/storage/memory/memorystorage.go | 15 +++++------ .../src/storage/sql/sqlstorage.go | 13 +++++----- hw12_13_14_15_calendar/src/storage/store.go | 6 ++--- 10 files changed, 66 insertions(+), 68 deletions(-) diff --git a/hw12_13_14_15_calendar/configs/config.toml b/hw12_13_14_15_calendar/configs/config.toml index dc28273..7abae37 100644 --- a/hw12_13_14_15_calendar/configs/config.toml +++ b/hw12_13_14_15_calendar/configs/config.toml @@ -1,11 +1,11 @@ -[logger] -file = "./calendar.log" -level = "INFO" +[Logger] +File = "./calendar.log" +Level = "INFO" -[storage] -in_memory = false -sql_host = "localhost" -sql_port = "5432" -sql_dbase = "calendar" -sql_user = "calendar" -sql_pass = "12345678" \ No newline at end of file +[Storage] +inMemory = false +SQLHost = "localhost" +SQLPort = "5432" +SQLDbase = "calendar" +SQLUser = "calendar" +SQLPass = "12345678" \ No newline at end of file diff --git a/hw12_13_14_15_calendar/src/config/config.go b/hw12_13_14_15_calendar/src/config/config.go index 9a94e24..3478a5d 100644 --- a/hw12_13_14_15_calendar/src/config/config.go +++ b/hw12_13_14_15_calendar/src/config/config.go @@ -1,9 +1,10 @@ package config import ( - "github.com/BurntSushi/toml" "io/ioutil" "os" + + "github.com/BurntSushi/toml" ) type Config struct { @@ -13,16 +14,16 @@ type Config struct { MuteStdout bool } Storage struct { - In_memory bool - Sql_host string - Sql_port string - Sql_dbase string - Sql_user string - Sql_pass string + InMemory bool + SQLHost string + SQLPort string + SQLDbase string + SQLUser string + SQLPass string } } -// Confita может быти и хороша, но она не возвращает ошибки, если не может распарсить файл в структуру. Мне не нравится такая "молчаливость" +// Confita может быти и хороша, но она не возвращает ошибки, если не может распарсить файл в структуру. Мне не нравится такая "молчаливость". func NewConfig(configFile string) (Config, error) { f, err := os.Open(configFile) if err != nil { diff --git a/hw12_13_14_15_calendar/src/config/config_test.go b/hw12_13_14_15_calendar/src/config/config_test.go index a9fb374..3fdd126 100644 --- a/hw12_13_14_15_calendar/src/config/config_test.go +++ b/hw12_13_14_15_calendar/src/config/config_test.go @@ -32,8 +32,8 @@ V`) } defer os.Remove(goodfile.Name()) goodfile.WriteString(`[storage] -in_memory = true -sql_host = "localhost"`) +inMemory = true +SQLHost = "localhost"`) goodfile.Sync() t.Run("No such file", func(t *testing.T) { @@ -50,8 +50,8 @@ sql_host = "localhost"`) 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.Equal(t, true, c.Storage.InMemory) + require.Equal(t, "localhost", c.Storage.SQLHost) require.NoError(t, e) }) diff --git a/hw12_13_14_15_calendar/src/logger/logger.go b/hw12_13_14_15_calendar/src/logger/logger.go index a4f2642..616cc11 100644 --- a/hw12_13_14_15_calendar/src/logger/logger.go +++ b/hw12_13_14_15_calendar/src/logger/logger.go @@ -2,19 +2,20 @@ package logger import ( "errors" - amitralog "github.com/amitrai48/logger" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config" "log" "os" "strings" + + amitralog "github.com/amitrai48/logger" + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config" ) type Logger interface { - Debug(msg string) - Info(msg string) - Warn(msg string) - Error(msg string) - Fatal(msg string) + Debugf(format string, args ...interface{}) + Infof(format string, args ...interface{}) + Warnf(format string, args ...interface{}) + Errorf(format string, args ...interface{}) + Fatalf(format string, args ...interface{}) } type Log struct { @@ -22,7 +23,6 @@ type Log struct { } func New(conf config.Config) (Log, error) { - if conf.Logger.File == "" || !validLevel(conf.Logger.Level) { return Log{}, errors.New("invalid logger config") } @@ -44,23 +44,23 @@ func New(conf config.Config) (Log, error) { return Log{Logger: l}, nil } -func (l Log) Debug(format string, args ...interface{}) { +func (l Log) Debugf(format string, args ...interface{}) { l.Logger.Debugf(format, args) } -func (l *Log) Info(format string, args ...interface{}) { +func (l *Log) Infof(format string, args ...interface{}) { l.Logger.Infof(format, args) } -func (l *Log) Warn(format string, args ...interface{}) { +func (l *Log) Warnf(format string, args ...interface{}) { l.Logger.Warnf(format, args) } -func (l *Log) Error(format string, args ...interface{}) { +func (l *Log) Errorf(format string, args ...interface{}) { l.Logger.Errorf(format, args) } -func (l *Log) Fatal(format string, args ...interface{}) { +func (l *Log) Fatalf(format string, args ...interface{}) { l.Logger.Fatalf(format, args) os.Exit(2) } diff --git a/hw12_13_14_15_calendar/src/logger/logger_test.go b/hw12_13_14_15_calendar/src/logger/logger_test.go index 14288ec..b2adf61 100644 --- a/hw12_13_14_15_calendar/src/logger/logger_test.go +++ b/hw12_13_14_15_calendar/src/logger/logger_test.go @@ -28,8 +28,8 @@ func TestLoggerLogic(t *testing.T) { } t.Run("Messages arround the level", func(t *testing.T) { - log.Debug("debug message") - log.Error("error message") + log.Debugf("debug message") + log.Errorf("error message") res, err := ioutil.ReadAll(tmpfile) if err != nil { diff --git a/hw12_13_14_15_calendar/src/main.go b/hw12_13_14_15_calendar/src/main.go index 09ef456..f1b4268 100644 --- a/hw12_13_14_15_calendar/src/main.go +++ b/hw12_13_14_15_calendar/src/main.go @@ -2,14 +2,15 @@ package main import ( "flag" + oslog "log" + "os" + "os/signal" + "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" ) var configFile string @@ -47,12 +48,12 @@ func main() { signal.Stop(signals) if err := server.Stop(); err != nil { - log.Error("failed to stop http server: " + err.Error()) + log.Errorf("failed to stop http server: " + err.Error()) } }() if err := server.Start(); err != nil { - log.Error("failed to start http server: " + err.Error()) + log.Errorf("failed to start http server: " + err.Error()) os.Exit(1) } } diff --git a/hw12_13_14_15_calendar/src/server/http/middleware.go b/hw12_13_14_15_calendar/src/server/http/middleware.go index 803cade..48654a3 100644 --- a/hw12_13_14_15_calendar/src/server/http/middleware.go +++ b/hw12_13_14_15_calendar/src/server/http/middleware.go @@ -1,11 +1,7 @@ package internalhttp -import ( - "net/http" -) - -func loggingMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // TODO - }) -} +//func loggingMiddleware(next http.Handler) http.Handler { +// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { +// TODO +// }) +//} diff --git a/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go b/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go index 9e3567a..8316fae 100644 --- a/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go +++ b/hw12_13_14_15_calendar/src/storage/memory/memorystorage.go @@ -2,34 +2,33 @@ 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 + //events []event.Event + //mu sync.RWMutex } func New() *Storage { return &Storage{} } -func (s Storage) Save(event event.Event) error { +func (s *Storage) Save(event event.Event) error { return nil } -func (s Storage) Update(event event.Event) error { +func (s *Storage) Update(event event.Event) error { return nil } -func (s Storage) Delete(event event.Event) error { +func (s *Storage) Delete(event event.Event) error { return nil } -func (s Storage) List() []event.Event { +func (s *Storage) List() []event.Event { return []event.Event{} } -func (s Storage) Get(id string) (event.Event, bool) { +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 index cbb1714..3c544d5 100644 --- a/hw12_13_14_15_calendar/src/storage/sql/sqlstorage.go +++ b/hw12_13_14_15_calendar/src/storage/sql/sqlstorage.go @@ -2,8 +2,9 @@ package sqlstorage import ( "context" - "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event" "sync" + + "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event" ) type Storage struct { @@ -25,7 +26,7 @@ func (s *Storage) Close(ctx context.Context) error { return nil } -func (s Storage) Save(e event.Event) error { +func (s *Storage) Save(e event.Event) error { if _, ok := s.Get(e.ID); !ok { s.Mu.Lock() s.Events = append(s.Events, e) @@ -34,18 +35,18 @@ func (s Storage) Save(e event.Event) error { return nil } -func (s Storage) Update(event event.Event) error { +func (s *Storage) Update(event event.Event) error { return nil } -func (s Storage) Delete(event event.Event) error { +func (s *Storage) Delete(event event.Event) error { return nil } -func (s Storage) List() []event.Event { +func (s *Storage) List() []event.Event { return []event.Event{} } -func (s Storage) Get(id string) (event.Event, bool) { +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 index 44e75bc..05fb7d4 100644 --- a/hw12_13_14_15_calendar/src/storage/store.go +++ b/hw12_13_14_15_calendar/src/storage/store.go @@ -7,7 +7,7 @@ import ( sqlstorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/sql" ) -type StoreInterface interface { +type Interface interface { Save(event event.Event) error Update(event event.Event) error Delete(event event.Event) error @@ -15,8 +15,8 @@ type StoreInterface interface { Get(id string) (event.Event, bool) } -func NewStore(config config.Config) StoreInterface { - if config.Storage.In_memory { +func NewStore(config config.Config) Interface { + if config.Storage.InMemory { st := memorystorage.New() return st }