HW12 создал server и app

pull/13/head
Andrey Ivanov 2020-09-04 11:28:50 +03:00 committed by Andrey Ivanov
parent 195c5ec84a
commit 24985e49f8
9 changed files with 108 additions and 63 deletions

View File

@ -1,3 +1,7 @@
[Server]
Address = "localhost"
Port = "8080"
[Logger]
File = "./calendar.log"
Level = "INFO"

View File

@ -2,27 +2,37 @@ package app
import (
"context"
"net/http"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/logger"
store "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
)
type Interface interface {
CreateEvent(ctx context.Context, id string, title string) (err error)
Handler(w http.ResponseWriter, r *http.Request)
}
type App struct {
// TODO
storage store.StorageInterface
logger logger.Interface
}
type Logger interface {
// TODO
}
type Storage interface {
// TODO
}
func New(logger Logger, storage Storage) *App {
return &App{}
func New(logger logger.Interface, storage store.StorageInterface) *App {
return &App{logger: logger, storage: storage}
}
func (a *App) CreateEvent(ctx context.Context, id string, title string) (err error) {
//err = a.storage.CreateEvent(storage.Event{ID: id, Title: title})
err = a.storage.Create(event.Event{ID: id, Title: title})
if err != nil {
a.logger.Errorf("can not create event")
}
a.logger.Infof("event created")
return err
}
// TODO
func (a *App) Handler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, _ = w.Write([]byte("Hello! I'm calendar app!"))
}

View File

@ -8,6 +8,10 @@ import (
)
type Config struct {
Server struct {
Address string
Port string
}
Logger struct {
File string
Level string

View File

@ -10,7 +10,7 @@ import (
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/config"
)
type Logger interface {
type Interface interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
@ -18,13 +18,13 @@ type Logger interface {
Fatalf(format string, args ...interface{})
}
type Log struct {
type Logger struct {
Logger amitralog.Logger
}
func New(conf config.Config) (Log, error) {
func New(conf config.Config) (Interface, error) {
if conf.Logger.File == "" || !validLevel(conf.Logger.Level) {
return Log{}, errors.New("invalid logger config")
return nil, errors.New("invalid logger config")
}
c := amitralog.Configuration{
@ -41,26 +41,26 @@ func New(conf config.Config) (Log, error) {
log.Fatalf("Could not instantiate log %s", err.Error())
}
l := amitralog.WithFields(amitralog.Fields{"hw": "12"})
return Log{Logger: l}, nil
return l, nil
}
func (l Log) Debugf(format string, args ...interface{}) {
func (l *Logger) Debugf(format string, args ...interface{}) {
l.Logger.Debugf(format, args)
}
func (l *Log) Infof(format string, args ...interface{}) {
func (l *Logger) Infof(format string, args ...interface{}) {
l.Logger.Infof(format, args)
}
func (l *Log) Warnf(format string, args ...interface{}) {
func (l *Logger) Warnf(format string, args ...interface{}) {
l.Logger.Warnf(format, args)
}
func (l *Log) Errorf(format string, args ...interface{}) {
func (l *Logger) Errorf(format string, args ...interface{}) {
l.Logger.Errorf(format, args)
}
func (l *Log) Fatalf(format string, args ...interface{}) {
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.Logger.Fatalf(format, args)
os.Exit(2)
}

View File

@ -25,20 +25,23 @@ func main() {
if err != nil {
oslog.Fatal("не удалось открыть файл конфигурации:", err.Error())
}
log, err := logger.New(conf)
if err != nil {
oslog.Fatal("не удалось запустить логер:", err.Error())
}
st := store.NewStore(conf)
if err != nil {
oslog.Fatal("не удалось инициализировать хранилище:", err.Error())
storeConf := store.Config{
InMemory: conf.Storage.InMemory,
SQLHost: conf.Storage.SQLHost,
SQLPort: conf.Storage.SQLPort,
SQLDbase: conf.Storage.SQLDbase,
SQLUser: conf.Storage.SQLUser,
SQLPass: conf.Storage.SQLPass,
}
st := store.NewStore(storeConf)
calendar := app.New(log, st)
server := internalhttp.NewServer(calendar)
server := internalhttp.NewServer(calendar, conf.Server.Address, conf.Server.Port)
go func() {
signals := make(chan os.Signal, 1)

View File

@ -1,26 +1,25 @@
package internalhttp
import (
"net"
"net/http"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/app"
)
type Server struct {
// TODO
server *http.Server
app app.Interface
}
type Application interface {
// TODO
}
func NewServer(app Application) *Server {
return &Server{}
func NewServer(app app.Interface, address string, port string) *Server {
return &Server{server: &http.Server{Addr: net.JoinHostPort(address, port), Handler: http.HandlerFunc(app.Handler)}, app: app}
}
func (s *Server) Start() error {
// TODO
return nil
return s.server.ListenAndServe()
}
func (s *Server) Stop() error {
//ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
// TODO
return nil
return s.server.Close()
}
// TODO

View File

@ -1,19 +1,26 @@
package memorystorage
import (
"sync"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
)
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) Create(event event.Event) error {
if _, ok := s.Get(event.ID); !ok {
s.Mu.Lock()
s.Events = append(s.Events, event)
s.Mu.Unlock()
}
return nil
}

View File

@ -2,17 +2,24 @@ package sqlstorage
import (
"context"
"sync"
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
)
type Storage struct {
Events []event.Event
Mu sync.RWMutex
type Config struct {
Host string
Port string
Dbase string
User string
Pass string
}
func New() *Storage {
type Storage struct {
//Events []event.Event
//Mu sync.RWMutex
}
func New(conf Config) *Storage {
return &Storage{}
}
@ -26,12 +33,8 @@ func (s *Storage) Close(ctx context.Context) error {
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()
}
func (s *Storage) Create(e event.Event) error {
// TODO
return nil
}

View File

@ -1,24 +1,39 @@
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 Interface interface {
Save(event event.Event) error
type Config struct {
InMemory bool
SQLHost string
SQLPort string
SQLDbase string
SQLUser string
SQLPass string
}
type StorageInterface interface {
Create(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) Interface {
if config.Storage.InMemory {
func NewStore(conf Config) StorageInterface {
if conf.InMemory {
st := memorystorage.New()
return st
}
return sqlstorage.New()
sqlConf := sqlstorage.Config{
Host: conf.SQLHost,
Port: conf.SQLPort,
Dbase: conf.SQLDbase,
User: conf.SQLUser,
Pass: conf.SQLPass,
}
return sqlstorage.New(sqlConf)
}