diff --git a/Dockerfile b/Dockerfile index 9bf7358..1827302 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o my_app ./cmd/scheduler/mai FROM alpine:3.18 ENV TODO_PORT=7540 -ENV TODO_DBFILE=/storage/scheduler.db +ENV TODO_DBFILE=/app/storage/scheduler.db WORKDIR /app diff --git a/cmd/scheduler/main.go b/cmd/scheduler/main.go index 44d3cc1..5199a94 100644 --- a/cmd/scheduler/main.go +++ b/cmd/scheduler/main.go @@ -9,8 +9,7 @@ import ( ) func main() { - cfg := config.MustLoad() - _ = cfg + config.MustLoad() port := ":" + config.Port diff --git a/internal/config/config.go b/internal/config/config.go index db34c75..2c0bdcc 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -23,10 +23,7 @@ var ( SecretKeyBytes []byte ) -type Config struct { -} - -func MustLoad() *Config { +func MustLoad() { dir, err := os.Getwd() // current directory if err != nil { @@ -63,9 +60,6 @@ func MustLoad() *Config { } SecretKeyBytes = []byte(secretKey) - var cfg Config - - return &cfg } func absPath(dir, path string) string { diff --git a/internal/http-server/handlers/nextdate.go b/internal/http-server/handlers/nextdate.go index f8c4afa..fa80c86 100644 --- a/internal/http-server/handlers/nextdate.go +++ b/internal/http-server/handlers/nextdate.go @@ -17,6 +17,7 @@ func GetNextDate(w http.ResponseWriter, r *http.Request) { if err != nil { writeErrorResponse(w, err, http.StatusBadRequest) log.Printf("time cannot pasre: %s", err) + return } newDate, err := scheduler.NextDate(nowDate, date, repeat) diff --git a/internal/http-server/handlers/routes.go b/internal/http-server/handlers/routes.go index 9faff84..d7be5ac 100644 --- a/internal/http-server/handlers/routes.go +++ b/internal/http-server/handlers/routes.go @@ -6,7 +6,7 @@ import ( "github.com/go-chi/chi/v5" ) -func RegisterRoutes(router *chi.Mux, scheduler *scheduler.Scheduler) { +func RegisterRoutes(router *chi.Mux, scheduler *scheduler.Planner) { router.Get("/api/nextdate", GetNextDate) router.Post("/api/signin", LoginHandler) diff --git a/internal/http-server/handlers/tasks.go b/internal/http-server/handlers/tasks.go index 253c764..5d5197d 100644 --- a/internal/http-server/handlers/tasks.go +++ b/internal/http-server/handlers/tasks.go @@ -40,7 +40,12 @@ func SaveTask(taskSaver TaskSaver) http.HandlerFunc { id, err := taskSaver.SaveTask(&task) if err != nil { - writeErrorResponse(w, err, http.StatusBadRequest) + switch { + case errors.Is(err, sql.ErrConnDone): + writeErrorResponse(w, err, http.StatusInternalServerError) + default: + writeErrorResponse(w, err, http.StatusBadRequest) + } return } @@ -55,7 +60,12 @@ func GetTasks(taskGetter TaskGetter) http.HandlerFunc { tasks, err := taskGetter.GetTasks(search) if err != nil { - writeErrorResponse(w, err, http.StatusBadRequest) + switch { + case errors.Is(err, sql.ErrConnDone): + writeErrorResponse(w, err, http.StatusInternalServerError) + default: + writeErrorResponse(w, err, http.StatusBadRequest) + } return } diff --git a/internal/scheduler/task.go b/internal/scheduler/task.go index c4b55d8..f3b89e3 100644 --- a/internal/scheduler/task.go +++ b/internal/scheduler/task.go @@ -9,12 +9,12 @@ import ( "github.com/denisushakov/todo-rest/pkg/models" ) -type Scheduler struct { +type Planner struct { Storage *sqlite.Storage } -func NewScheduler(dataBase *sqlite.Storage) *Scheduler { - return &Scheduler{ +func NewScheduler(dataBase *sqlite.Storage) *Planner { + return &Planner{ Storage: dataBase, } } @@ -28,7 +28,7 @@ type TaskScheduler interface { DeleteTask(string) error } -func (s *Scheduler) SaveTask(task *models.Task) (int64, error) { +func (s *Planner) SaveTask(task *models.Task) (int64, error) { if err := check(task); err != nil { return 0, err } @@ -40,7 +40,7 @@ func (s *Scheduler) SaveTask(task *models.Task) (int64, error) { return id, nil } -func (s *Scheduler) GetTasks(search string) ([]*models.Task, error) { +func (s *Planner) GetTasks(search string) ([]*models.Task, error) { var sr_st sqlite.Search if search != "" { @@ -61,7 +61,7 @@ func (s *Scheduler) GetTasks(search string) ([]*models.Task, error) { return tasks, nil } -func (s *Scheduler) GetTaskByID(id string) (*models.Task, error) { +func (s *Planner) GetTaskByID(id string) (*models.Task, error) { task, err := s.Storage.GetTaskByID(id) if err != nil { return nil, err @@ -69,7 +69,7 @@ func (s *Scheduler) GetTaskByID(id string) (*models.Task, error) { return task, nil } -func (s *Scheduler) UpdateTask(task *models.Task) error { +func (s *Planner) UpdateTask(task *models.Task) error { if task.ID == "" { return fmt.Errorf("id is empty") } @@ -120,7 +120,7 @@ func check(task *models.Task) error { return nil } -func (s *Scheduler) MarkTaskCompleted(id string) error { +func (s *Planner) MarkTaskCompleted(id string) error { var now = time.Now().Truncate(24 * time.Hour) task, err := s.GetTaskByID(id) if err != nil { @@ -146,7 +146,7 @@ func (s *Scheduler) MarkTaskCompleted(id string) error { return nil } -func (s *Scheduler) DeleteTask(id string) error { +func (s *Planner) DeleteTask(id string) error { if err := s.Storage.DeleteTask(id); err != nil { return err } diff --git a/pkg/router/router.go b/pkg/router/router.go index 8d4d361..7b1ef86 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -3,7 +3,6 @@ package router import ( "log" "net/http" - "os" "github.com/denisushakov/todo-rest/internal/scheduler" "github.com/denisushakov/todo-rest/internal/storage/sqlite" @@ -21,9 +20,8 @@ func SetupRouter() *chi.Mux { storage, err := sqlite.New(config.DBFilePath) if err != nil { log.Fatalf("Failed to connect to the database: %v", err) - os.Exit(1) } - scheduler := scheduler.NewScheduler(storage) + planner := scheduler.NewScheduler(storage) router := chi.NewRouter() @@ -31,7 +29,7 @@ func SetupRouter() *chi.Mux { router.Handle("/*", http.FileServer(http.Dir(webDir))) - handlers.RegisterRoutes(router, scheduler) + handlers.RegisterRoutes(router, planner) return router } diff --git a/tests/server_test.go b/tests/server_test.go index 7bacc7c..74a82bf 100644 --- a/tests/server_test.go +++ b/tests/server_test.go @@ -10,9 +10,7 @@ import ( ) func createTestServer() *httptest.Server { - - cfg := config.MustLoad() - _ = cfg + config.MustLoad() router := router.SetupRouter()