Skip to content

Commit 92993a7

Browse files
committed
Added ENV for building Docker
1 parent bc7d20c commit 92993a7

File tree

6 files changed

+112
-24
lines changed

6 files changed

+112
-24
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
files/*
2-
poenskelisten
2+
poenskelisten
3+
poenskelisten.exe

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
files/*
2-
poenskelisten
1+
/files/*
2+
poenskelisten
3+
poenskelisten.exe

Dockerfile

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ FROM golang:1.19-alpine
33
LABEL org.opencontainers.image.source=https://github.com/aunefyren/poenskelisten
44

55
ENV port=8080
6+
ENV timezone=Europe/Oslo
7+
ENV dbip=localhost
8+
ENV dbport=3306
9+
ENV dbname=poenskelisten
10+
ENV dbusername=root
11+
ENV dbpassword=root
612

713
RUN apk update
814
RUN apk add git
@@ -15,6 +21,4 @@ COPY . .
1521

1622
RUN CGO_ENABLED=0 go build
1723

18-
EXPOSE 8080
19-
20-
ENTRYPOINT /app/poenskelisten
24+
ENTRYPOINT /app/poenskelisten -port ${port} -timezone ${timezone} -dbip ${dbip} -dbport ${dbport} -dbname ${dbname} -dbusername ${dbusername} -dbpassword ${dbpassword}

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ services:
123123
restart: unless-stopped
124124
environment:
125125
# The table name you chose
126-
MYSQL_DATABASE: 'db'
126+
MYSQL_DATABASE: 'ponske'
127127
# User, so you don't have to use root
128128
MYSQL_USER: 'myuser'
129129
# Please switch this password
@@ -149,13 +149,23 @@ services:
149149
- ./data/:/app/files/
150150
ports:
151151
- '8080:8080'
152+
environment:
153+
# The container will only respect these ENV if they are empty in the config.json
154+
# Useful for first setup
155+
port: 8080
156+
timezone: Europe/Oslo
157+
dbip: db
158+
dbport: 3306
159+
dbname: ponske
160+
dbusername: myuser
161+
dbpassword: mystrongpassword
152162
phpmyadmin:
153163
image: phpmyadmin:latest
154164
restart: unless-stopped
155165
environment:
156166
- PMA_ARBITRARY=1
157167
# DB table
158-
- PMA_HOST:db
168+
- PMA_HOST:ponske
159169
# Root password
160170
- MYSQL_ROOT_PASSWORD:root
161171
# Timezone

files/config.json

-15
This file was deleted.

main.go

+88-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"aunefyren/poenskelisten/controllers"
77
"aunefyren/poenskelisten/database"
88
"aunefyren/poenskelisten/middlewares"
9+
"aunefyren/poenskelisten/models"
910
"aunefyren/poenskelisten/utilities"
11+
"flag"
1012
"fmt"
1113
"log"
1214
"net/http"
@@ -59,12 +61,20 @@ func main() {
5961
log.Println("Configuration file loaded.")
6062
fmt.Println("Configuration file loaded.")
6163

64+
// Change the config to respect flags
65+
Config, err = parseFlags(Config)
66+
if err != nil {
67+
log.Println("Failed to parse input flags. Error: " + err.Error())
68+
fmt.Println("Failed to parse input flags. Error: " + err.Error())
69+
70+
os.Exit(1)
71+
}
72+
6273
// Set time zone from config if it is not empty
6374
if Config.Timezone != "" {
6475
loc, err := time.LoadLocation(Config.Timezone)
6576
if err != nil {
6677
fmt.Println("Failed to set time zone from config. Error: " + err.Error())
67-
fmt.Println(err)
6878
fmt.Println("Removing value...")
6979

7080
log.Println("Failed to set time zone from config. Error: " + err.Error())
@@ -250,3 +260,80 @@ func initRouter() *gin.Engine {
250260

251261
return router
252262
}
263+
264+
func parseFlags(Config *models.ConfigStruct) (*models.ConfigStruct, error) {
265+
266+
// Define flag variables with the configuration file as default values
267+
var port int
268+
flag.IntVar(&port, "port", Config.PoenskelistenPort, "The port Pønskelisten is listening on.")
269+
270+
var timezone string
271+
flag.StringVar(&timezone, "timezone", Config.Timezone, "The timezone Pønskelisten is running in.")
272+
273+
var dbPort int
274+
flag.IntVar(&dbPort, "dbport", Config.DBPort, "The port the database is listening on.")
275+
276+
var dbUsername string
277+
flag.StringVar(&dbUsername, "dbusername", Config.DBUsername, "The username used to interact with the database.")
278+
279+
var dbPassword string
280+
flag.StringVar(&dbPassword, "dbpassword", Config.DBPassword, "The password used to interact with the database.")
281+
282+
var dbName string
283+
flag.StringVar(&dbName, "dbname", Config.DBName, "The database table used within the database.")
284+
285+
var dbIP string
286+
flag.StringVar(&dbIP, "dbip", Config.DBIP, "The IP address used to reach the database.")
287+
288+
// Parse the flags from input
289+
flag.Parse()
290+
291+
// Respect the flag if config is empty
292+
if Config.PoenskelistenPort == 0 {
293+
Config.PoenskelistenPort = port
294+
}
295+
296+
// Respect the flag if config is empty
297+
if Config.Timezone == "" {
298+
Config.Timezone = timezone
299+
}
300+
301+
// Respect the flag if config is empty
302+
if Config.DBPort == 0 {
303+
Config.DBPort = dbPort
304+
}
305+
306+
// Respect the flag if config is empty
307+
if Config.DBUsername == "" {
308+
Config.DBUsername = dbUsername
309+
}
310+
311+
// Respect the flag if config is empty
312+
if Config.DBPassword == "" {
313+
Config.DBPassword = dbPassword
314+
}
315+
316+
// Respect the flag if config is empty
317+
if Config.DBName == "" {
318+
Config.DBName = dbName
319+
}
320+
321+
// Respect the flag if config is empty
322+
if Config.DBIP == "" {
323+
Config.DBIP = dbIP
324+
}
325+
326+
// Failsafe, if port is 0, set to default 8080
327+
if Config.PoenskelistenPort == 0 {
328+
Config.PoenskelistenPort = 8080
329+
}
330+
331+
// Save the new config
332+
err := config.SaveConfig(Config)
333+
if err != nil {
334+
return &models.ConfigStruct{}, err
335+
}
336+
337+
return Config, nil
338+
339+
}

0 commit comments

Comments
 (0)