diff --git a/cmd/main.go b/cmd/main.go index 2131f58..17ec298 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -16,12 +16,14 @@ type config struct { env string } type application struct { - config config - logger *log.Logger + config config + logger *log.Logger + repousecase auth.PlaceUsecase } func main() { - repos := auth.NewRepository() + connStr := "user=postgres password=mypassword host=localhost port=5432 dbname=landmarks sslmode=disable" + repos := auth.NewRepository(connStr) repoUsecase := auth.NewRepoUsecase(repos) var cfg config flag.IntVar(&cfg.port, "port", 8080, "API server port") @@ -29,8 +31,9 @@ func main() { flag.Parse() logger := log.New(os.Stdout, "", log.Ldate|log.Ltime) app := &application{ - config: cfg, - logger: logger, + config: cfg, + logger: logger, + repousecase: repoUsecase, } mux := http.NewServeMux() mux.HandleFunc("/healthcheck", app.healthcheckHandler) @@ -45,26 +48,29 @@ func main() { logger.Printf("starting %s server on %s", cfg.env, srv.Addr) err := srv.ListenAndServe() if err != nil { - logger.Fatal(err) + fmt.Errorf("Failed to start server: %v", err) + os.Exit(1) } } func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { _, err := fmt.Fprintf(w, "STATUS: OK") if err != nil { - fmt.Printf("ERROR: healthcheckHandler: %s\n", err) + http.Error(w, "", http.StatusBadRequest) + fmt.Errorf("ERROR: healthcheckHandler: %s\n", err) } } func (app *application) getPlaceHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - places, err := repoUsecase.getPlaces() + places, err := app.repousecase.GetPlace() if err != nil { http.Error(w, "Не удалось получить список достопримечательностей", http.StatusInternalServerError) return } err = json.NewEncoder(w).Encode(places) if err != nil { + http.Error(w, "Не удалось преобразовать в json", http.StatusInternalServerError) return } } diff --git a/pkg/auth/repo.go b/pkg/auth/repo.go index a05b2dc..7c78c3a 100644 --- a/pkg/auth/repo.go +++ b/pkg/auth/repo.go @@ -6,15 +6,17 @@ type Repo interface { getPlaces() ([]Place, error) } -type Repository struct{} +type Repository struct { + connectStr string +} -func NewRepository() *Repository { - return &Repository{} +func NewRepository(c string) *Repository { + return &Repository{connectStr: c} } func (r *Repository) getPlaces() ([]Place, error) { - connStr := "user=postgres password=mypassword host=localhost port=5432 dbname=landmarks sslmode=disable" - db, err := sql.Open("postgres", connStr) + + db, err := sql.Open("postgres", r.connectStr) if err != nil { return nil, err } diff --git a/pkg/auth/usecase.go b/pkg/auth/usecase.go index 2920a74..82f2566 100644 --- a/pkg/auth/usecase.go +++ b/pkg/auth/usecase.go @@ -7,7 +7,7 @@ type Place struct { } type PlaceUsecase interface { - getPlace() ([]Place, error) + GetPlace() ([]Place, error) } type RepoUsecase struct { @@ -18,7 +18,7 @@ func NewRepoUsecase(repos *Repository) *RepoUsecase { return &RepoUsecase{repos: *repos} } -func (i *RepoUsecase) getPlace() ([]Place, error) { +func (i *RepoUsecase) GetPlace() ([]Place, error) { places, err := i.repos.getPlaces() if err != nil { return nil, err