HW12 создал server и app
parent
195c5ec84a
commit
24985e49f8
|
@ -1,3 +1,7 @@
|
||||||
|
[Server]
|
||||||
|
Address = "localhost"
|
||||||
|
Port = "8080"
|
||||||
|
|
||||||
[Logger]
|
[Logger]
|
||||||
File = "./calendar.log"
|
File = "./calendar.log"
|
||||||
Level = "INFO"
|
Level = "INFO"
|
||||||
|
|
|
@ -2,27 +2,37 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"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 {
|
type App struct {
|
||||||
// TODO
|
storage store.StorageInterface
|
||||||
|
logger logger.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
type Logger interface {
|
func New(logger logger.Interface, storage store.StorageInterface) *App {
|
||||||
// TODO
|
return &App{logger: logger, storage: storage}
|
||||||
}
|
|
||||||
|
|
||||||
type Storage interface {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(logger Logger, storage Storage) *App {
|
|
||||||
return &App{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) CreateEvent(ctx context.Context, id string, title string) (err error) {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
func (a *App) Handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(200)
|
||||||
|
_, _ = w.Write([]byte("Hello! I'm calendar app!"))
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
Server struct {
|
||||||
|
Address string
|
||||||
|
Port string
|
||||||
|
}
|
||||||
Logger struct {
|
Logger struct {
|
||||||
File string
|
File string
|
||||||
Level string
|
Level string
|
||||||
|
|
|
@ -10,7 +10,7 @@ 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/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Logger interface {
|
type Interface interface {
|
||||||
Debugf(format string, args ...interface{})
|
Debugf(format string, args ...interface{})
|
||||||
Infof(format string, args ...interface{})
|
Infof(format string, args ...interface{})
|
||||||
Warnf(format string, args ...interface{})
|
Warnf(format string, args ...interface{})
|
||||||
|
@ -18,13 +18,13 @@ type Logger interface {
|
||||||
Fatalf(format string, args ...interface{})
|
Fatalf(format string, args ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type Log struct {
|
type Logger struct {
|
||||||
Logger amitralog.Logger
|
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) {
|
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{
|
c := amitralog.Configuration{
|
||||||
|
@ -41,26 +41,26 @@ func New(conf config.Config) (Log, error) {
|
||||||
log.Fatalf("Could not instantiate log %s", err.Error())
|
log.Fatalf("Could not instantiate log %s", err.Error())
|
||||||
}
|
}
|
||||||
l := amitralog.WithFields(amitralog.Fields{"hw": "12"})
|
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)
|
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)
|
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)
|
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)
|
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)
|
l.Logger.Fatalf(format, args)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,20 +25,23 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
oslog.Fatal("не удалось открыть файл конфигурации:", err.Error())
|
oslog.Fatal("не удалось открыть файл конфигурации:", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log, err := logger.New(conf)
|
log, err := logger.New(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
oslog.Fatal("не удалось запустить логер:", err.Error())
|
oslog.Fatal("не удалось запустить логер:", err.Error())
|
||||||
}
|
}
|
||||||
|
storeConf := store.Config{
|
||||||
st := store.NewStore(conf)
|
InMemory: conf.Storage.InMemory,
|
||||||
if err != nil {
|
SQLHost: conf.Storage.SQLHost,
|
||||||
oslog.Fatal("не удалось инициализировать хранилище:", err.Error())
|
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)
|
calendar := app.New(log, st)
|
||||||
|
|
||||||
server := internalhttp.NewServer(calendar)
|
server := internalhttp.NewServer(calendar, conf.Server.Address, conf.Server.Port)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
signals := make(chan os.Signal, 1)
|
signals := make(chan os.Signal, 1)
|
||||||
|
|
|
@ -1,26 +1,25 @@
|
||||||
package internalhttp
|
package internalhttp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/app"
|
||||||
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
// TODO
|
server *http.Server
|
||||||
|
app app.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
type Application interface {
|
func NewServer(app app.Interface, address string, port string) *Server {
|
||||||
// TODO
|
return &Server{server: &http.Server{Addr: net.JoinHostPort(address, port), Handler: http.HandlerFunc(app.Handler)}, app: app}
|
||||||
}
|
|
||||||
|
|
||||||
func NewServer(app Application) *Server {
|
|
||||||
return &Server{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() error {
|
func (s *Server) Start() error {
|
||||||
// TODO
|
return s.server.ListenAndServe()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Stop() error {
|
func (s *Server) Stop() error {
|
||||||
//ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
return s.server.Close()
|
||||||
// TODO
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
|
|
|
@ -1,19 +1,26 @@
|
||||||
package memorystorage
|
package memorystorage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
|
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Storage struct {
|
||||||
//events []event.Event
|
Events []event.Event
|
||||||
//mu sync.RWMutex
|
Mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Storage {
|
func New() *Storage {
|
||||||
return &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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,17 +2,24 @@ package sqlstorage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
|
"github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/event"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Storage struct {
|
type Config struct {
|
||||||
Events []event.Event
|
Host string
|
||||||
Mu sync.RWMutex
|
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{}
|
return &Storage{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,12 +33,8 @@ func (s *Storage) Close(ctx context.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) Save(e event.Event) error {
|
func (s *Storage) Create(e event.Event) error {
|
||||||
if _, ok := s.Get(e.ID); !ok {
|
// TODO
|
||||||
s.Mu.Lock()
|
|
||||||
s.Events = append(s.Events, e)
|
|
||||||
s.Mu.Unlock()
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,39 @@
|
||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
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"
|
"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"
|
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"
|
sqlstorage "github.com/tiburon-777/HW_OTUS/hw12_13_14_15_calendar/src/storage/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interface interface {
|
type Config struct {
|
||||||
Save(event event.Event) error
|
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
|
Update(event event.Event) error
|
||||||
Delete(event event.Event) error
|
Delete(event event.Event) error
|
||||||
List() []event.Event
|
List() []event.Event
|
||||||
Get(id string) (event.Event, bool)
|
Get(id string) (event.Event, bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(config config.Config) Interface {
|
func NewStore(conf Config) StorageInterface {
|
||||||
if config.Storage.InMemory {
|
if conf.InMemory {
|
||||||
st := memorystorage.New()
|
st := memorystorage.New()
|
||||||
return st
|
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue