From f6010f01270ecb827f866bb7fbe08309881dfe82 Mon Sep 17 00:00:00 2001 From: mevain Date: Sat, 28 Sep 2024 12:03:59 +0300 Subject: [PATCH 1/4] feat: add api and conf for nginx --- build/main.Dockerfile | 12 ++++----- cmd/main.go | 19 ++++++++++--- go.mod | 4 +++ go.sum | 2 ++ nginx.conf | 61 ++++++++++++++++++++++++++++++++++++++++++ pkg/middleware/cors.go | 16 +++++++++++ 6 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 go.sum create mode 100644 nginx.conf create mode 100644 pkg/middleware/cors.go diff --git a/build/main.Dockerfile b/build/main.Dockerfile index b1f303f..1cde78e 100644 --- a/build/main.Dockerfile +++ b/build/main.Dockerfile @@ -3,13 +3,11 @@ COPY . /github.com/go-park-mail-ru/2024_2_ThereWillBeName WORKDIR /github.com/go-park-mail-ru/2024_2_ThereWillBeName RUN go mod download RUN go clean --modcache -RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/main/main.go +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/main.go FROM scratch AS runner WORKDIR /build COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/.bin . -COPY --from=builder /github.com/go-park-mail-ru/2024_2_ThereWillBeName/config config/ -COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip / -ENV TZ="Europe/Moscow" -ENV ZONEINFO=/zoneinfo.zip -EXPOSE 80 443 8080 -ENTRYPOINT ["./.bin"] \ No newline at end of file +EXPOSE 8080 +ENTRYPOINT ["./.bin"] + + diff --git a/cmd/main.go b/cmd/main.go index b81f701..7079e2b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,12 +1,15 @@ package main import ( + "TripAdvisor/pkg/middleware" "flag" "fmt" "log" "net/http" "os" "time" + + "github.com/gorilla/mux" ) type config struct { @@ -28,20 +31,28 @@ func main() { config: cfg, logger: logger, } - mux := http.NewServeMux() - mux.HandleFunc("/healthcheck", app.healthcheckHandler) + r := mux.NewRouter().PathPrefix("/api").Subrouter() + r.Use(middleware.CORSMiddleware) + r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Not Found", http.StatusNotFound) + }) + // для каждой логической сущности из pkg свой саброутер + healthcheck := r.PathPrefix("/healthcheck").Subrouter() + healthcheck.HandleFunc("", app.healthcheckHandler).Methods(http.MethodGet) + srv := &http.Server{ Addr: fmt.Sprintf(":%d", cfg.port), - Handler: mux, + Handler: r, IdleTimeout: time.Minute, ReadTimeout: 10 * time.Second, WriteTimeout: 30 * time.Second, } - logger.Printf("starting %s server on %s", cfg.env, srv.Addr) + logger.Printf("Starting server on port %d in %s mode", cfg.port, srv.Addr) err := srv.ListenAndServe() if err != nil { logger.Fatal(err) } + } func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { diff --git a/go.mod b/go.mod index 5c6bdf8..7dc8d9b 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,5 @@ module TripAdvisor + +go 1.23.1 + +require github.com/gorilla/mux v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..4c5bc1c --- /dev/null +++ b/nginx.conf @@ -0,0 +1,61 @@ +user www-data; +worker_processes auto; +pid /run/nginx.pid; +error_log /var/log/nginx/error.log; +include /etc/nginx/modules-enabled/*.conf; + +events { + worker_connections 768; + # multi_accept on; +} + +http { + + ## + # Basic Settings + ## + + sendfile on; + tcp_nopush on; + types_hash_max_size 2048; + include /etc/nginx/mime.types; + server { + listen 80; + server_name 109.120.181.229; + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + location / { + root /home/ubuntu/public; + try_files $uri /index.html; + } + location /api/ { + proxy_pass http://localhost:8080; + } +} + + ## + # SSL Settings + ## + + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POO> + ssl_prefer_server_ciphers on; + + ## + # Logging Settings + ## + + access_log /var/log/nginx/access.log; + + ## + # Gzip Settings + ## + + gzip on; + + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; + +} + + + diff --git a/pkg/middleware/cors.go b/pkg/middleware/cors.go new file mode 100644 index 0000000..c555a2e --- /dev/null +++ b/pkg/middleware/cors.go @@ -0,0 +1,16 @@ +package middleware + +import "net/http" + +func 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") + w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) + if r.Method == http.MethodOptions { + return + } + next.ServeHTTP(w, r) + }) +} From 2f6cc51862d807203873e42af9711dca9d156edf Mon Sep 17 00:00:00 2001 From: mevain Date: Sat, 28 Sep 2024 13:05:19 +0300 Subject: [PATCH 2/4] fix : go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 7dc8d9b..f2859b0 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module TripAdvisor -go 1.23.1 +go 1.22.7 require github.com/gorilla/mux v1.8.1 From 74443dccc082f9a6949edf13f449609ba06cace4 Mon Sep 17 00:00:00 2001 From: mevain Date: Sat, 28 Sep 2024 14:55:49 +0300 Subject: [PATCH 3/4] fix: delete subrouter and fix nginx.conf --- cmd/main.go | 5 ++--- nginx.conf | 25 +++++++++---------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7079e2b..8b6cf36 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -37,8 +37,8 @@ func main() { http.Error(w, "Not Found", http.StatusNotFound) }) // для каждой логической сущности из pkg свой саброутер - healthcheck := r.PathPrefix("/healthcheck").Subrouter() - healthcheck.HandleFunc("", app.healthcheckHandler).Methods(http.MethodGet) + // healthcheck := r.PathPrefix("").Subrouter() + r.HandleFunc("/healthcheck", app.healthcheckHandler).Methods(http.MethodGet) srv := &http.Server{ Addr: fmt.Sprintf(":%d", cfg.port), @@ -52,7 +52,6 @@ func main() { if err != nil { logger.Fatal(err) } - } func (app *application) healthcheckHandler(w http.ResponseWriter, r *http.Request) { diff --git a/nginx.conf b/nginx.conf index 4c5bc1c..6fa739f 100644 --- a/nginx.conf +++ b/nginx.conf @@ -20,25 +20,18 @@ http { types_hash_max_size 2048; include /etc/nginx/mime.types; server { - listen 80; - server_name 109.120.181.229; - access_log /var/log/nginx/access.log; - error_log /var/log/nginx/error.log; - location / { + listen 80; + server_name 109.120.181.229; + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + location / { root /home/ubuntu/public; try_files $uri /index.html; + } + location /api/ { + proxy_pass http://localhost:8080; + } } - location /api/ { - proxy_pass http://localhost:8080; - } -} - - ## - # SSL Settings - ## - - ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POO> - ssl_prefer_server_ciphers on; ## # Logging Settings From 86d3360a58fed054aa8cbcd70ff7e68efb8d63d0 Mon Sep 17 00:00:00 2001 From: mevain Date: Sat, 28 Sep 2024 14:57:50 +0300 Subject: [PATCH 4/4] fix: comments --- cmd/main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 8b6cf36..9442103 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -36,8 +36,6 @@ func main() { r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, "Not Found", http.StatusNotFound) }) - // для каждой логической сущности из pkg свой саброутер - // healthcheck := r.PathPrefix("").Subrouter() r.HandleFunc("/healthcheck", app.healthcheckHandler).Methods(http.MethodGet) srv := &http.Server{