-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* place endpoint are added * errors fixed * json * nginx are added * errors fixed * docker changed * errors fixed * errors fixed * errors fixed * errors fixed * errors fixed
- Loading branch information
1 parent
ebc2d2b
commit 275e8b0
Showing
14 changed files
with
203 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
.idea | ||
go.mod | ||
2024-2-Zdes-budet-nazvanie-UykwHnIE.crt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"TripAdvisor/pkg/middleware" | ||
"TripAdvisor/internal/models" | ||
"TripAdvisor/internal/pkg/middleware" | ||
"TripAdvisor/internal/pkg/places/delivery" | ||
"TripAdvisor/internal/pkg/places/repo" | ||
"TripAdvisor/internal/pkg/places/usecase" | ||
"flag" | ||
"fmt" | ||
"github.com/gorilla/mux" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type config struct { | ||
port int | ||
env string | ||
} | ||
type application struct { | ||
config config | ||
logger *log.Logger | ||
} | ||
|
||
func main() { | ||
var cfg config | ||
flag.IntVar(&cfg.port, "port", 8080, "API server port") | ||
flag.StringVar(&cfg.env, "env", "development", "Environment") | ||
newPlaceRepo := repo.NewPLaceRepository() | ||
placeUsecase := usecase.NewPlaceUsecase(newPlaceRepo) | ||
handler := delivery.NewPlacesHandler(placeUsecase) | ||
|
||
var cfg models.Config | ||
flag.IntVar(&cfg.Port, "port", 8080, "API server port") | ||
flag.StringVar(&cfg.Env, "env", "development", "Environment") | ||
flag.StringVar(&cfg.AllowedOrigin, "allowed-origin", "*", "Allowed origin") | ||
flag.Parse() | ||
|
||
corsMiddleware := middleware.NewCORSMiddleware([]string{cfg.AllowedOrigin}) | ||
|
||
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime) | ||
app := &application{ | ||
config: cfg, | ||
logger: logger, | ||
} | ||
r := mux.NewRouter().PathPrefix("/api").Subrouter() | ||
r.Use(middleware.CORSMiddleware) | ||
r := mux.NewRouter().PathPrefix("/api/v1").Subrouter() | ||
r.Use(corsMiddleware.CorsMiddleware) | ||
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, "Not Found", http.StatusNotFound) | ||
}) | ||
r.HandleFunc("/healthcheck", app.healthcheckHandler).Methods(http.MethodGet) | ||
|
||
healthcheck := r.PathPrefix("/healthcheck").Subrouter() | ||
healthcheck.HandleFunc("", healthcheckHandler).Methods(http.MethodGet) | ||
placecheck := r.PathPrefix("/places").Subrouter() | ||
placecheck.HandleFunc("", handler.GetPlaceHandler).Methods(http.MethodGet) | ||
srv := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", cfg.port), | ||
Addr: fmt.Sprintf(":%d", cfg.Port), | ||
Handler: r, | ||
IdleTimeout: time.Minute, | ||
ReadTimeout: 10 * time.Second, | ||
WriteTimeout: 30 * time.Second, | ||
} | ||
logger.Printf("Starting server on port %d in %s mode", cfg.port, srv.Addr) | ||
|
||
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) { | ||
func 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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
module TripAdvisor | ||
|
||
go 1.22.7 | ||
|
||
require github.com/gorilla/mux v1.8.1 | ||
go 1.23.0 | ||
require github.com/gorilla/mux v1.8.1 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
type Config struct { | ||
Port int | ||
Env string | ||
AllowedOrigin string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
type Place struct { | ||
ID int `json:"id"` | ||
Name string `json:"name""` | ||
Image string `json:"image"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package middleware | ||
|
||
import "net/http" | ||
|
||
type CORSMiddleware struct { | ||
AllowedOrigins []string | ||
} | ||
|
||
func NewCORSMiddleware(allowedOrigins []string) *CORSMiddleware { | ||
return &CORSMiddleware{ | ||
AllowedOrigins: allowedOrigins, | ||
} | ||
} | ||
func (c *CORSMiddleware) CorsMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Access-Control-Allow-Methods", "POST,PUT,DELETE,GET") | ||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type") | ||
w.Header().Set("Access-Control-Allow-Credentials", "true") | ||
if len(c.AllowedOrigins) > 0 { | ||
w.Header().Set("Access-Control-Allow-Origin", c.AllowedOrigins[0]) | ||
} | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package delivery | ||
|
||
import ( | ||
"TripAdvisor/internal/pkg/places" | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type PlacesHandler struct { | ||
uc places.PlaceUsecase | ||
} | ||
|
||
func NewPlacesHandler(uc places.PlaceUsecase) *PlacesHandler { | ||
return &PlacesHandler{uc} | ||
} | ||
|
||
func (h *PlacesHandler) GetPlaceHandler(w http.ResponseWriter, r *http.Request) { | ||
places, err := h.uc.GetPlaces(r.Context()) | ||
if err != nil { | ||
http.Error(w, "Не удалось получить список достопримечательностей", http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
err = json.NewEncoder(w).Encode(places) | ||
if err != nil { | ||
http.Error(w, "Не удалось преобразовать в json", http.StatusInternalServerError) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package places | ||
|
||
import ( | ||
"TripAdvisor/internal/models" | ||
"context" | ||
) | ||
|
||
type PlaceRepo interface { | ||
GetPlaces(ctx context.Context) ([]models.Place, error) | ||
} | ||
|
||
type PlaceUsecase interface { | ||
GetPlaces(ctx context.Context) ([]models.Place, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[ | ||
{ | ||
"id": 0, | ||
"image": "/images/image1.png", | ||
"name": "Московский Кремль" | ||
}, | ||
{ | ||
"id": 1, | ||
"image": "/images/image2.png", | ||
"name": "Красная Площадь" | ||
}, | ||
{ | ||
"id": 2, | ||
"image": "/images/image3.png", | ||
"name": "Храм Василия Блаженного" | ||
}, | ||
{ | ||
"id": 3, | ||
"image": "/images/image4.png", | ||
"name": "Большой театр" | ||
}, | ||
{ | ||
"id": 4, | ||
"image": "/images/image5.png", | ||
"name": "Государственная Третьяковская галерея" | ||
}, | ||
{ | ||
"id": 5, | ||
"image": "/images/image6.png", | ||
"name": "Государственный музей Петергоф" | ||
} | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package repo | ||
|
||
import ( | ||
"TripAdvisor/internal/models" | ||
"context" | ||
_ "embed" | ||
"encoding/json" | ||
"log" | ||
) | ||
|
||
type PlaceRepository struct { | ||
} | ||
|
||
func NewPLaceRepository() *PlaceRepository { | ||
return &PlaceRepository{} | ||
} | ||
|
||
//go:embed places.json | ||
var jsonFileData []byte | ||
|
||
func (r *PlaceRepository) GetPlaces(ctx context.Context) ([]models.Place, error) { | ||
images := make([]models.Place, 0) | ||
err := json.Unmarshal(jsonFileData, &images) | ||
if err != nil { | ||
log.Println(err) | ||
return nil, err | ||
} | ||
return images, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package usecase | ||
|
||
import ( | ||
"TripAdvisor/internal/models" | ||
"TripAdvisor/internal/pkg/places" | ||
"context" | ||
) | ||
|
||
type PlaceUsecaseImpl struct { | ||
repo places.PlaceRepo | ||
} | ||
|
||
func NewPlaceUsecase(repo places.PlaceRepo) *PlaceUsecaseImpl { | ||
return &PlaceUsecaseImpl{repo: repo} | ||
} | ||
|
||
func (i *PlaceUsecaseImpl) GetPlaces(ctx context.Context) ([]models.Place, error) { | ||
places, err := i.repo.GetPlaces(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return places, nil | ||
} |
Empty file.