Skip to content

Commit

Permalink
Adapted application for usage in docker with system parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
John Lennon committed Nov 17, 2020
1 parent 60afb53 commit f981e6f
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 35 deletions.
25 changes: 17 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 6 additions & 3 deletions cache/config.go
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 13 additions & 1 deletion cache/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cache
import (
"context"
"fmt"
"os"
"strconv"

"github.com/go-redis/redis/v8"
)
Expand All @@ -14,17 +16,27 @@ 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,
DB: 0,
}
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
}
16 changes: 9 additions & 7 deletions database/config.go
Original file line number Diff line number Diff line change
@@ -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")
)
5 changes: 0 additions & 5 deletions limiter/config.go

This file was deleted.

2 changes: 1 addition & 1 deletion limiter/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
37 changes: 37 additions & 0 deletions startup/startup.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f981e6f

Please sign in to comment.