From f981e6f9bd1c3b38c2cfe464df9422a547a98732 Mon Sep 17 00:00:00 2001 From: John Lennon Date: Tue, 17 Nov 2020 21:58:17 +0000 Subject: [PATCH] Adapted application for usage in docker with system parameters. --- Dockerfile | 25 +++++++++++++++++-------- cache/config.go | 9 ++++++--- cache/connect.go | 14 +++++++++++++- database/config.go | 16 +++++++++------- limiter/config.go | 5 ----- limiter/limiter.go | 2 +- main.go | 15 +++++---------- startup/startup.go | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 88 insertions(+), 35 deletions(-) delete mode 100644 limiter/config.go create mode 100644 startup/startup.go diff --git a/Dockerfile b/Dockerfile index 568d87b..ecf0e4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,14 +9,23 @@ COPY go.sum . RUN go mod download COPY . . -ENV PORT 8080 -ENV DBHOST 0.0.0.0 -ENV DBUSER postgres -ENV DBPASSWORD postgres -ENV DBDATABASE collabyt -ENV DBPORT 5432 -ENV DBSSL disable -ENV DBSOURCE postgres +ENV APP_PORT 8080 +ENV APP_IDLE_TIMEOUT 120 +ENV APP_READ_TIMEOUT 5 +ENV APP_WRITE_TIMEOUT 5 + +ENV DB_HOST localhost +ENV DB_USER postgres +ENV DB_PASSWORD postgres +ENV DB_DATABASE collabyt +ENV DB_PORT 5432 +ENV DB_SSL disable +ENV DB_SOURCE postgres + +ENV CACHE_TTL 60 +ENV CACHE_HOST localhost +ENV CACHE_PORT 6379 +ENV CACHE_PASSWORD "" RUN go build CMD ["./Backend"] \ No newline at end of file diff --git a/cache/config.go b/cache/config.go index 8c57922..497dde7 100644 --- a/cache/config.go +++ b/cache/config.go @@ -1,8 +1,11 @@ package cache +import "os" + var ( - cacheHost = "localhost" - cachePort = "6379" - cachePassword = "" + cacheHost = os.Getenv("CACHE_HOST") + cachePort = os.Getenv("CACHE_PORT") + cachePassword = os.Getenv("CACHE_PASSWORD") cacheDB = 0 + CacheTTL = 0 ) diff --git a/cache/connect.go b/cache/connect.go index 3e9c978..6cb0447 100644 --- a/cache/connect.go +++ b/cache/connect.go @@ -3,6 +3,8 @@ package cache import ( "context" "fmt" + "os" + "strconv" "github.com/go-redis/redis/v8" ) @@ -14,6 +16,11 @@ var ( // Connect will initiate a connection with the 0 DB on the REDIS server func Connect() { + ttl, err := loadTTL() + if err != nil { + panic(err) + } + CacheTTL = ttl rOptions := &redis.Options{ Addr: fmt.Sprintf("%s:%s", cacheHost, cachePort), Password: cachePassword, @@ -21,10 +28,15 @@ func Connect() { } rClient := redis.NewClient(rOptions) ctx := context.Background() - _, err := rClient.Ping(ctx).Result() + _, err = rClient.Ping(ctx).Result() if err != nil { panic(err) } _ = rClient.FlushDB(ctx).Err() Cache = rClient } + +func loadTTL() (int, error) { + ttl, err := strconv.Atoi(os.Getenv("CACHE_TTL")) + return ttl, err +} diff --git a/database/config.go b/database/config.go index 4dd3baa..9210220 100644 --- a/database/config.go +++ b/database/config.go @@ -1,12 +1,14 @@ package database +import "os" + var ( - dbHost = "localhost" // TODO: Change to environment variabe - dbUser = "postgres" // TODO: Change to environment variabe - dbPassword = "postgres" // TODO: Change to environment variabe - dbDatabase = "collabyt" // TODO: Change to environment variabe - dbPort = "5432" // TODO: Change to environment variabe + dbHost = os.Getenv("DB_HOST") + dbUser = os.Getenv("DB_USER") + dbPassword = os.Getenv("DB_PASSWORD") + dbDatabase = os.Getenv("DB_DATABASE") + dbPort = os.Getenv("DB_PORT") - dbSsl = "disable" // TODO: Change to environment variabe - dbSource = "postgres" // TODO: Change to environment variabe + dbSsl = os.Getenv("DB_SSL") + dbSource = os.Getenv("DB_SOURCE") ) diff --git a/limiter/config.go b/limiter/config.go deleted file mode 100644 index b805317..0000000 --- a/limiter/config.go +++ /dev/null @@ -1,5 +0,0 @@ -package limiter - -var ( - timeToDumpVisitor = 60 // TODO: Change to environment variabe -) diff --git a/limiter/limiter.go b/limiter/limiter.go index a0f3e32..2ffe03a 100644 --- a/limiter/limiter.go +++ b/limiter/limiter.go @@ -23,7 +23,7 @@ func Limit(rClient *redis.Client, next http.Handler) http.Handler { } ctx := r.Context() limiter := redis_rate.NewLimiter(cache.Cache) - rRet, err := limiter.Allow(ctx, ip, redis_rate.PerMinute(timeToDumpVisitor)) + rRet, err := limiter.Allow(ctx, ip, redis_rate.PerMinute(cache.CacheTTL)) if err != nil { handler.WriteErrorReply(w, http.StatusInternalServerError) return diff --git a/main.go b/main.go index 900464c..667d6d3 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,10 @@ package main import ( "net/http" - "time" - "github.com/collabyt/Backend/cache" "github.com/collabyt/Backend/database" "github.com/collabyt/Backend/handler" - "github.com/collabyt/Backend/limiter" + "github.com/collabyt/Backend/startup" "github.com/gorilla/mux" ) @@ -37,14 +35,11 @@ func main() { fs := http.FileServer(http.Dir("./static")) http.Handle("/", fs) - server := http.Server{ - Addr: ":8080", - Handler: limiter.Limit(cache.Cache, r), - IdleTimeout: 120 * time.Second, - ReadTimeout: 5 * time.Second, - WriteTimeout: 5 * time.Second, + server, err := startup.SetupServer(r) + if err != nil { + panic(err) } - err := server.ListenAndServe() + err = server.ListenAndServe() if err != nil { panic(err) } diff --git a/startup/startup.go b/startup/startup.go new file mode 100644 index 0000000..e9c11ff --- /dev/null +++ b/startup/startup.go @@ -0,0 +1,37 @@ +package startup + +import ( + "fmt" + "net/http" + "os" + "strconv" + "time" + + "github.com/collabyt/Backend/cache" + "github.com/collabyt/Backend/limiter" + "github.com/gorilla/mux" +) + +func SetupServer(r *mux.Router) (http.Server, error) { + address := os.Getenv("APP_PORT") + handler := limiter.Limit(cache.Cache, r) + idleTimeout, err := strconv.Atoi(os.Getenv("APP_IDLE_TIMEOUT")) + if err != nil { + return http.Server{}, fmt.Errorf("server stup faile: impossible to get %s from system's parameters", "APP_IDLE_TIMEOUT") + } + readTimeout, err := strconv.Atoi(os.Getenv("APP_READ_TIMEOUT")) + if err != nil { + return http.Server{}, fmt.Errorf("server stup faile: impossible to get %s from system's parameters", "APP_READ_TIMEOUT") + } + writeTimeout, err := strconv.Atoi(os.Getenv("APP_WRITE_TIMEOUT")) + if err != nil { + return http.Server{}, fmt.Errorf("server stup faile: impossible to get %s from system's parameters", "APP_WRITE_TIMEOUT") + } + return http.Server{ + Addr: address, + Handler: handler, + IdleTimeout: time.Duration(idleTimeout), + ReadTimeout: time.Duration(readTimeout), + WriteTimeout: time.Duration(writeTimeout), + }, err +}