Skip to content

Commit

Permalink
errors fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
timurIsaevIY committed Sep 28, 2024
1 parent 0a796ee commit 3f8090f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
22 changes: 14 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ 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")
flag.StringVar(&cfg.env, "env", "development", "Environment")
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)
Expand All @@ -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)

Check failure on line 51 in cmd/main.go

View workflow job for this annotation

GitHub Actions / linters

Error return value of `fmt.Errorf` is not checked (errcheck)
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)

Check failure on line 60 in cmd/main.go

View workflow job for this annotation

GitHub Actions / linters

Error return value of `fmt.Errorf` is not checked (errcheck)
}
}

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
}
}
12 changes: 7 additions & 5 deletions pkg/auth/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Place struct {
}

type PlaceUsecase interface {
getPlace() ([]Place, error)
GetPlace() ([]Place, error)
}

type RepoUsecase struct {
Expand All @@ -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
Expand Down

0 comments on commit 3f8090f

Please sign in to comment.