Skip to content

Commit

Permalink
Merge pull request #4 from go-park-mail-ru/task/TRI-19
Browse files Browse the repository at this point in the history
Nginx
  • Loading branch information
vr009 authored Sep 28, 2024
2 parents e429bd1 + 86d3360 commit 7fdd430
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 11 deletions.
12 changes: 5 additions & 7 deletions build/main.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
EXPOSE 8080
ENTRYPOINT ["./.bin"]


16 changes: 12 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"TripAdvisor/pkg/middleware"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
)

type config struct {
Expand All @@ -28,16 +31,21 @@ 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)
})
r.HandleFunc("/healthcheck", 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)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module TripAdvisor

go 1.22.7

require github.com/gorilla/mux v1.8.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
54 changes: 54 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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;
}
}

##
# Logging Settings
##

access_log /var/log/nginx/access.log;

##
# Gzip Settings
##

gzip on;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

}



16 changes: 16 additions & 0 deletions pkg/middleware/cors.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 7fdd430

Please sign in to comment.