Dump changes

This commit is contained in:
Andrey Chervyakov 2022-06-03 23:44:08 +06:00
parent e12550a643
commit aac2ea1b74
Signed by: cognio
GPG key ID: DAA316147EB0D58D
25 changed files with 129 additions and 76 deletions

View file

@ -1,9 +1,9 @@
package main package main
import ( import (
"brainbuffer/pkg/brainbuffer" "brainbuffer/pkg/brainbuffer/infrastructure"
"brainbuffer/pkg/brainbuffer/database" database2 "brainbuffer/pkg/brainbuffer/infrastructure/database"
appserver "brainbuffer/pkg/brainbuffer/server" appserver "brainbuffer/pkg/brainbuffer/infrastructure/server"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"os" "os"
@ -12,12 +12,13 @@ import (
func main() { func main() {
configureLogger() configureLogger()
conf := brainbuffer.NewConfig() conf := infrastructure.NewConfig()
pool := database.Pool(conf) pool := database2.NewPool(conf)
database.Migrate(pool) database2.Migrate(pool)
server := appserver.New(conf, pool) context := infrastructure.NewContext(pool, conf)
server := appserver.New(context)
server.Logger.Fatal(server.Start(":8080")) server.Logger.Fatal(server.Start(":8080"))
} }

40
go.mod
View file

@ -1,28 +1,50 @@
module brainbuffer module brainbuffer
go 1.16 go 1.18
require ( require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/cornelk/hashmap v1.0.1 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jackc/pgx/v4 v4.10.1 github.com/jackc/pgx/v4 v4.10.1
github.com/jackc/tern v1.12.4 github.com/jackc/tern v1.12.4
github.com/knadh/koanf v0.15.0 github.com/knadh/koanf v0.15.0
github.com/labstack/echo/v4 v4.2.1 github.com/labstack/echo/v4 v4.2.1
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mitchellh/copystructure v1.1.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1
github.com/robfig/cron/v3 v3.0.1 github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.20.0 github.com/rs/zerolog v1.20.0
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
)
require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.8.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.6.2 // indirect
github.com/jackc/puddle v1.1.3 // indirect
github.com/labstack/gommon v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/copystructure v1.1.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/reflectwalk v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b // indirect golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b // indirect
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 // indirect golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 // indirect
golang.org/x/sys v0.0.0-20210317091845-390168757d9c // indirect golang.org/x/sys v0.0.0-20210317091845-390168757d9c // indirect
golang.org/x/text v0.3.5 // indirect golang.org/x/text v0.3.5 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
) )

6
go.sum
View file

@ -26,15 +26,11 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cornelk/hashmap v1.0.1 h1:RXGcy29hEdLLV8T6aK4s+BAd4tq4+3Hq50N2GoG0uIg=
github.com/cornelk/hashmap v1.0.1/go.mod h1:8wbysTUDnwJGrPZ1Iwsou3m+An6sldFrJItjRhfegCw=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.1.0 h1:1Rs9eTUlZLPBEvV+2sTaM8O0NWn0ppbgqS7p11aWawI=
github.com/dchest/siphash v1.1.0/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
@ -98,7 +94,6 @@ github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
@ -119,7 +114,6 @@ github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2 h1:JVX6jT/XfzNqIjye47
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=

View file

@ -1,27 +0,0 @@
package appointment
import (
"brainbuffer/pkg/brainbuffer/scheduling"
"time"
)
type Status int
const (
Overdue Status = iota
Upcoming
Missed
Completed
)
type Appointment struct {
ID int64
TaskID int64
Status Status
SchedulingPattern scheduling.Pattern
Time time.Time
DurationOffset time.Duration
CreationTime time.Time
}
type Appointments []*Appointment

View file

@ -0,0 +1,42 @@
package appointment
import (
"brainbuffer/pkg/brainbuffer/domain/scheduling"
"time"
)
type CompletionStatus int
const (
None CompletionStatus = iota
Missed
Completed
Skipped
)
type TimeStatus int
const (
Overdue TimeStatus = iota
Upcoming
)
type Appointment struct {
ID int64
TaskID int64
CompletionStatus CompletionStatus
SchedulingPattern scheduling.Pattern
PlannedTime time.Time
DurationOffset time.Duration
CreationTime time.Time
}
func (a *Appointment) TimeStatus() TimeStatus {
if a.PlannedTime.Before(time.Now()) {
return Overdue
} else {
return Upcoming
}
}
type Appointments []*Appointment

View file

@ -1,7 +1,7 @@
package appointment package appointment
import ( import (
"brainbuffer/pkg/brainbuffer/repository" "brainbuffer/pkg/brainbuffer/domain/repository"
"time" "time"
) )
@ -54,7 +54,7 @@ func (repo *inMemoryRepository) GetAllByTasksBefore(taskIds []int64, beforeTime
taskAppointments := make(Appointments, 0) taskAppointments := make(Appointments, 0)
for _, val := range repo.db { for _, val := range repo.db {
if _, found := findInIntSlice(val.TaskID, taskIds); found && val.Time.Before(beforeTime) { if _, found := findInIntSlice(val.TaskID, taskIds); found && val.PlannedTime.Before(beforeTime) {
taskAppointments = append(taskAppointments, val) taskAppointments = append(taskAppointments, val)
} }
} }

View file

@ -1,7 +1,7 @@
package appointment package appointment
import ( import (
"brainbuffer/pkg/brainbuffer/repository" "brainbuffer/pkg/brainbuffer/domain/repository"
"time" "time"
) )

View file

@ -1,8 +1,8 @@
package appointment package appointment
import ( import (
apperrors "brainbuffer/pkg/brainbuffer/errors" "brainbuffer/pkg/brainbuffer/domain/repository"
"brainbuffer/pkg/brainbuffer/repository" apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
"time" "time"
) )
@ -37,7 +37,7 @@ func (service *defaultService) Create(appointment *Appointment) error {
} }
func (service *defaultService) CreateNextAppointment(appointment *Appointment) error { func (service *defaultService) CreateNextAppointment(appointment *Appointment) error {
nextTime, err := appointment.SchedulingPattern.NextTime(appointment.Time) nextTime, err := appointment.SchedulingPattern.NextTime(appointment.PlannedTime)
if err != nil { if err != nil {
return apperrors.UnknownError{Err: err} return apperrors.UnknownError{Err: err}
} }
@ -45,9 +45,9 @@ func (service *defaultService) CreateNextAppointment(appointment *Appointment) e
nextAppointment := Appointment{ nextAppointment := Appointment{
ID: 0, ID: 0,
TaskID: appointment.TaskID, TaskID: appointment.TaskID,
Status: Upcoming, CompletionStatus: None,
SchedulingPattern: appointment.SchedulingPattern, SchedulingPattern: appointment.SchedulingPattern,
Time: *nextTime, PlannedTime: *nextTime,
DurationOffset: 0, DurationOffset: 0,
CreationTime: time.Now(), CreationTime: time.Now(),
} }

View file

@ -1,7 +1,7 @@
package scheduling package scheduling
import ( import (
apperrors "brainbuffer/pkg/brainbuffer/errors" apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"time" "time"

View file

@ -1,7 +1,7 @@
package summary package summary
import ( import (
"brainbuffer/pkg/brainbuffer/appointment" "brainbuffer/pkg/brainbuffer/domain/appointment"
"time" "time"
) )

View file

@ -1,7 +1,7 @@
package task package task
import ( import (
"brainbuffer/pkg/brainbuffer/scheduling" "brainbuffer/pkg/brainbuffer/domain/scheduling"
"time" "time"
) )

View file

@ -1,7 +1,7 @@
package task package task
import ( import (
"brainbuffer/pkg/brainbuffer/scheduling" "brainbuffer/pkg/brainbuffer/domain/scheduling"
"time" "time"
) )

View file

@ -1,6 +1,8 @@
package task package task
import "brainbuffer/pkg/brainbuffer/repository" import (
"brainbuffer/pkg/brainbuffer/domain/repository"
)
type inMemoryRepository struct { type inMemoryRepository struct {
db map[int64]*Task db map[int64]*Task

View file

@ -1,7 +1,7 @@
package task package task
import ( import (
"brainbuffer/pkg/brainbuffer/repository" "brainbuffer/pkg/brainbuffer/domain/repository"
) )
type Repository interface { type Repository interface {

View file

@ -1,10 +1,10 @@
package task package task
import ( import (
"brainbuffer/pkg/brainbuffer/appointment" "brainbuffer/pkg/brainbuffer/domain/appointment"
apperrors "brainbuffer/pkg/brainbuffer/errors" "brainbuffer/pkg/brainbuffer/domain/repository"
"brainbuffer/pkg/brainbuffer/repository" "brainbuffer/pkg/brainbuffer/domain/scheduling"
"brainbuffer/pkg/brainbuffer/scheduling" apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
"time" "time"
) )
@ -50,9 +50,9 @@ func (serv *defaultService) Create(task *Task) error {
firstAppointment := appointment.Appointment{ firstAppointment := appointment.Appointment{
ID: 0, ID: 0,
TaskID: 0, TaskID: 0,
Status: appointment.Upcoming, CompletionStatus: appointment.None,
SchedulingPattern: pattern, SchedulingPattern: pattern,
Time: *nextTime, PlannedTime: *nextTime,
DurationOffset: 0, DurationOffset: 0,
CreationTime: time.Now(), CreationTime: time.Now(),
} }

View file

@ -1,4 +1,4 @@
package brainbuffer package infrastructure
import ( import (
"github.com/knadh/koanf" "github.com/knadh/koanf"

View file

@ -0,0 +1,18 @@
package infrastructure
import (
"github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf"
)
type Context struct {
Database *pgxpool.Pool
Config *koanf.Koanf
}
func NewContext(db *pgxpool.Pool, config *koanf.Koanf) *Context {
return &Context{
Database: db,
Config: config,
}
}

View file

@ -8,7 +8,7 @@ import (
"github.com/knadh/koanf" "github.com/knadh/koanf"
) )
func Pool(config *koanf.Koanf) *pgxpool.Pool { func NewPool(config *koanf.Koanf) *pgxpool.Pool {
connStr, err := getConnectionString(config) connStr, err := getConnectionString(config)
if err != nil { if err != nil {
panic(err) panic(err)

View file

@ -1,8 +1,9 @@
package server package server
import ( import (
apperrors "brainbuffer/pkg/brainbuffer/errors" "brainbuffer/pkg/brainbuffer/domain/task"
"brainbuffer/pkg/brainbuffer/task" "brainbuffer/pkg/brainbuffer/infrastructure"
apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
"github.com/knadh/koanf" "github.com/knadh/koanf"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -11,13 +12,13 @@ import (
"net/http" "net/http"
) )
func New(conf *koanf.Koanf, pool *pgxpool.Pool) *echo.Echo { func New(context *infrastructure.Context) *echo.Echo {
server := echo.New() server := echo.New()
server.HTTPErrorHandler = errorHandler(server) server.HTTPErrorHandler = errorHandler(server)
registerHandlers(server, pool) registerHandlers(server, context.Database)
registerMiddleware(server, conf) registerMiddleware(server, context.Config)
return server return server
} }