Skip to content

Commit ee5de07

Browse files
committed
feat: change env variables to arg flags
1 parent 61c8e9f commit ee5de07

File tree

7 files changed

+25
-40
lines changed

7 files changed

+25
-40
lines changed

Dockerfile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ RUN apk update \
2222
&& update-ca-certificates
2323

2424
ADD bin/ARG_OS-ARG_ARCH/ARG_SRC_BIN /ARG_BIN
25+
ENV BINSRC_ENV="/ARG_BIN"
2526
COPY scripts/docker-entrypoint.sh /usr/local/bin/
2627

2728
EXPOSE 5000
2829

2930
USER nobody:nobody
3031
ENTRYPOINT ["docker-entrypoint.sh"]
31-
CMD ["/ARG_BIN"]

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: P=$PORT CACHE=$CACHE_ENV STATIC_PATH=/app/cmd/server/public ./bin/server
1+
web: ./bin/server - P=$PORT -CACHE=$CACHE_ENV -STATIC_PATH=/app/cmd/server/public

cmd/server/helpers.go

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import (
77
"path/filepath"
88
)
99

10-
// GetEnv ... get value from env variables or return the fallback value
11-
func GetEnv(key, fallback string) string {
12-
if value, ok := os.LookupEnv(key); ok {
13-
return value
14-
}
15-
return fallback
16-
}
17-
1810
// LsFiles ... list file in directory
1911
func LsFiles(pattern string) {
2012
err := filepath.Walk(pattern,

cmd/server/helpers_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
package main
22

3-
import (
4-
"testing"
5-
)
6-
7-
func TestGetEnv(t *testing.T) {
8-
9-
}

cmd/server/main.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ import (
1111
"os"
1212
"path/filepath"
1313
"runtime"
14-
"strconv"
1514
"time"
1615
)
1716

1817
var (
19-
host string
20-
port int
21-
cacheDriver string
22-
redisUrl string
18+
host *string
19+
port *int
20+
cacheDriver *string
21+
redisUrl *string
2322
// Storage ... Server Cache Storage
2423
Storage cache.Storage
2524
// Logger ... Logger Driver
@@ -32,10 +31,9 @@ var (
3231
routes = map[string]func(w http.ResponseWriter, r *http.Request){
3332
`/convert`: Convert,
3433
}
35-
3634
_, filename, _, _ = runtime.Caller(0)
3735
defaultStaticPath = filepath.Dir(filename) + `/public`
38-
staticPath = defaultStaticPath
36+
staticPath = &defaultStaticPath
3937
)
4038

4139
// init ... init function of the server
@@ -50,19 +48,19 @@ func init() {
5048
logging.SetBackend(backendLevelFormatted)
5149

5250
// Caching
53-
host = GetEnv(`H`, `0.0.0.0`)
54-
port, _ = strconv.Atoi(GetEnv(`P`, `5000`))
55-
cacheDriver = GetEnv(`CACHE`, `memory`)
56-
redisUrl = GetEnv(`REDIS_URL`, ``)
57-
staticPath = GetEnv(`STATIC_PATH`, defaultStaticPath)
51+
host = flag.String(`H`, `0.0.0.0`, `Host binding address`)
52+
port = flag.Int(`P`, 5000, `Host binding port`)
53+
cacheDriver = flag.String(`CACHE`, `memory`, `Cache driver (default memory)`)
54+
redisUrl = flag.String(`REDIS_URL`, ``, `Redis URI for redis cache driver`)
55+
staticPath = flag.String(`STATIC_PATH`, defaultStaticPath, `Webserver static path`)
5856

5957
flag.Parse()
6058

6159
var err error
6260

63-
switch cacheDriver {
61+
switch *cacheDriver {
6462
case `redis`:
65-
if Storage, err = redis.NewStorage(redisUrl); err != nil {
63+
if Storage, err = redis.NewStorage(*redisUrl); err != nil {
6664
Logger.Panic(err)
6765
}
6866
break
@@ -75,18 +73,18 @@ func init() {
7573
// main ... main function start the server
7674
func main() {
7775

78-
Logger.Infof("host %s", host)
79-
Logger.Infof("port %d", port)
80-
Logger.Infof("cacheDriver %s", cacheDriver)
81-
Logger.Infof("REDIS_URL %s", redisUrl)
82-
Logger.Infof("Static dir %s", staticPath)
76+
Logger.Infof("host %s", *host)
77+
Logger.Infof("port %d", *port)
78+
Logger.Infof("cacheDriver %s", *cacheDriver)
79+
Logger.Infof("REDIS_URL %s", *redisUrl)
80+
Logger.Infof("Static dir %s", *staticPath)
8381

8482
// handle routers
8583
for k, v := range routes {
8684
http.HandleFunc(k, v)
8785
}
8886

89-
go serveHTTP(host, port)
87+
go serveHTTP(*host, *port)
9088
select {}
9189
}
9290

@@ -96,7 +94,7 @@ func serveHTTP(host string, port int) {
9694
mux := http.NewServeMux()
9795
for k, v := range routes {
9896
mux.HandleFunc(k, v)
99-
mux.Handle(`/`, http.FileServer(http.Dir(staticPath)))
97+
mux.Handle(`/`, http.FileServer(http.Dir(*staticPath)))
10098
}
10199

102100
addr := fmt.Sprintf("%v:%d", host, port)

scripts/docker-entrypoint.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh
22

33
echo Your container args are: "$@"
4+
echo Your BINSRC_ENV are: "${BINSRC_ENV}"
45

5-
exec "$@"
6+
exec ${BINSRC_ENV} "$@"
67

scripts/server-redis.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ set -o pipefail
77
my_dir="$(dirname "$0")"
88

99
GO_FILES=`find ${my_dir}/../cmd/server/. -type f \( -iname "*.go" ! -iname "*_test.go" \)`
10-
REDIS_URL=redis://localhost:6379 CACHE=redis go run ${GO_FILES}
10+
cmd="go run ${GO_FILES} -CACHE=redis -REDIS_URL=redis://localhost:6379"
11+
${cmd}

0 commit comments

Comments
 (0)