Skip to content

Commit

Permalink
Add dockerfile, compose-file + github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasdoerfler committed Sep 6, 2024
1 parent e2a8b83 commit 419c3cf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ jobs:

- name: Build
run: go build


docker:
runs-on: ubuntu-latest
needs: build

steps:
- uses: actions/checkout@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build Docker image
run: docker build -t ghcr.io/${{ github.repository }}:latest .

- name: Push Docker image
run: docker push ghcr.io/${{ github.repository }}:latest
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.21-alpine AS builder

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o weather-exporter .



FROM alpine:latest
RUN addgroup -S app && adduser -S app -G app
WORKDIR /app

COPY --from=builder /app/weather-exporter .

EXPOSE 8080
USER app
CMD ["./weather-exporter"]
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3.8'

services:
weather-exporter:
build: .
environment:
- WEATHER_EXPORTER_WEBSERVERPORT=8082
- WEATHER_EXPORTER_LOGLEVEL=info
- WEATHER_EXPORTER_JSONEXPORTER_ENABLED=true
- WEATHER_EXPORTER_PROMETHEUSEXPORTER_ENABLED=true
- WEATHER_EXPORTER_INFLUXDBEXPORTER_ENABLED=false
- WEATHER_EXPORTER_INFLUXDBEXPORTER_SERVER=1.2.3.4
- WEATHER_EXPORTER_INFLUXDBEXPORTER_PORT=8086
- WEATHER_EXPORTER_INFLUXDBEXPORTER_ORG=<org>
- WEATHER_EXPORTER_INFLUXDBEXPORTER_TOKEN=<token> # or <username>:<password>
- WEATHER_EXPORTER_INFLUXDBEXPORTER_DATABASE=<database(bucket)>
- WEATHER_EXPORTER_INFLUXDBEXPORTER_MEASUREMENT=<measurement>
- WEATHER_EXPORTER_MQTT_ENABLED=false
- WEATHER_EXPORTER_MQTT_BROKERADDRESS=1.2.3.4
- WEATHER_EXPORTER_MQTT_BROKERPORT=1883
- WEATHER_EXPORTER_MQTT_USERNAME=username
- WEATHER_EXPORTER_MQTT_PASSWORD=password
- WEATHER_EXPORTER_MQTT_CLIENTID=weather-exporter
- WEATHER_EXPORTER_MQTT_TOPICPREFIX=weather
ports:
- "8080:8080"
7 changes: 6 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -45,14 +46,18 @@ func loadConfig() {
viper.SetConfigName("config")
viper.AddConfigPath(configPath)

viper.AutomaticEnv()
viper.SetEnvPrefix("weather_exporter")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

viper.SetDefault("webserverPort", 8080)
viper.SetDefault("loglevel", "info")
viper.SetDefault("jsonExporter", map[string]interface{}{"enabled": true})
viper.SetDefault("prometheusExporter", map[string]interface{}{"enabled": true})
viper.SetDefault("influxDbExporter", map[string]interface{}{"enabled": false})

if err := viper.ReadInConfig(); err != nil {
log.Warnf("Error reading config file, using default values. %s", err)
log.Infof("Unable to read config file, using default values or environment. %s", err)
}
err := viper.Unmarshal(&config)
if err != nil {
Expand Down

0 comments on commit 419c3cf

Please sign in to comment.