Skip to content

Commit f41a658

Browse files
authored
Merge pull request #1 from a7d-corp/initial-setup
Initial commit of ingestion API app
2 parents d524a78 + 7786473 commit f41a658

File tree

21 files changed

+1409
-0
lines changed

21 files changed

+1409
-0
lines changed

.env.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# InfluxDB connection
2+
INFLUX_URL=http://localhost:8086
3+
INFLUX_TOKEN=your-token-here
4+
INFLUX_ORG=health
5+
INFLUX_BUCKET=health_data
6+
7+
# API authentication
8+
INGESTION_API_KEY=your-api-key-here
9+
10+
# Server
11+
PORT=8080
12+
13+
# Write behaviour
14+
INFLUX_BATCH_SIZE=500 # Max points per write call
15+
INFLUX_FLUSH_INTERVAL_MS=1000
16+
INFLUX_MAX_RETRIES=3
17+
INFLUX_RETRY_INTERVAL_MS=500

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.22-alpine AS builder
2+
WORKDIR /app
3+
COPY go.mod go.sum ./
4+
RUN go mod download
5+
COPY . .
6+
RUN CGO_ENABLED=0 GOOS=linux go build \
7+
-ldflags "-X main.version=$(git rev-parse --short HEAD 2>/dev/null || echo dev)" \
8+
-o health-ingestion .
9+
10+
FROM alpine:3.19
11+
RUN apk --no-cache add ca-certificates tzdata
12+
WORKDIR /app
13+
COPY --from=builder /app/health-ingestion .
14+
EXPOSE 8080
15+
ENTRYPOINT ["./health-ingestion"]

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11
# apple-health-ingestor
2+
3+
HTTP API that receives JSON payloads from the [Health Auto Export](https://www.healthautoexport.com/) iOS app and writes the data to InfluxDB v2.
4+
5+
## Configuration
6+
7+
Copy `.env.example` to `.env` and fill in the values. All variables are required except `PORT`.
8+
9+
| Variable | Default | Description |
10+
|---|---|---|
11+
| `INFLUX_URL` || InfluxDB URL |
12+
| `INFLUX_TOKEN` || InfluxDB API token |
13+
| `INFLUX_ORG` || InfluxDB org name |
14+
| `INFLUX_BUCKET` || InfluxDB bucket name |
15+
| `INGESTION_API_KEY` || API key for the `/ingest` endpoint |
16+
| `PORT` | `8080` | HTTP listen port |
17+
| `INFLUX_BATCH_SIZE` | `500` | Max points per write call |
18+
| `INFLUX_FLUSH_INTERVAL_MS` | `1000` | Write flush interval (ms) |
19+
| `INFLUX_MAX_RETRIES` | `3` | Write retry attempts |
20+
| `INFLUX_RETRY_INTERVAL_MS` | `500` | Delay between retries (ms) |
21+
22+
## Health Auto Export setup
23+
24+
1. Install [Health Auto Export](https://www.healthautoexport.com/) on your iPhone.
25+
2. Go to **Automations****Add Automation**.
26+
3. Set **Export Format** to `JSON` and **Export Type** to `REST API`.
27+
4. Set the URL to `http://<your-host>:<PORT>/ingest`.
28+
5. Add a header: `X-API-Key: <your INGESTION_API_KEY>`.
29+
6. Choose the metrics/workouts to export and set a sync interval.
30+
31+
## Running
32+
33+
```bash
34+
# Build and run
35+
go build -o health-ingestion .
36+
./health-ingestion
37+
38+
# Docker
39+
docker build -t health-ingestion .
40+
docker run --env-file .env -p 8080:8080 health-ingestion
41+
```
42+
43+
## API
44+
45+
| Method | Path | Auth | Description |
46+
|---|---|---|---|
47+
| `POST` | `/ingest` | `X-API-Key` header | Ingest Health Auto Export payload |
48+
| `GET` | `/health` | none | Liveness check |

go.mod

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module health-ingestion
2+
3+
go 1.22
4+
5+
require (
6+
github.com/gin-gonic/gin v1.10.0
7+
github.com/go-playground/validator/v10 v10.22.0
8+
github.com/influxdata/influxdb-client-go/v2 v2.13.0
9+
github.com/joho/godotenv v1.5.1
10+
github.com/stretchr/testify v1.9.0
11+
)
12+
13+
require (
14+
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
15+
github.com/bytedance/sonic v1.11.6 // indirect
16+
github.com/bytedance/sonic/loader v0.1.1 // indirect
17+
github.com/cloudwego/base64x v0.1.4 // indirect
18+
github.com/cloudwego/iasm v0.2.0 // indirect
19+
github.com/davecgh/go-spew v1.1.1 // indirect
20+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
21+
github.com/gin-contrib/sse v0.1.0 // indirect
22+
github.com/go-playground/locales v0.14.1 // indirect
23+
github.com/go-playground/universal-translator v0.18.1 // indirect
24+
github.com/goccy/go-json v0.10.2 // indirect
25+
github.com/google/uuid v1.3.1 // indirect
26+
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
27+
github.com/json-iterator/go v1.1.12 // indirect
28+
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
29+
github.com/leodido/go-urn v1.4.0 // indirect
30+
github.com/mattn/go-isatty v0.0.20 // indirect
31+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
32+
github.com/modern-go/reflect2 v1.0.2 // indirect
33+
github.com/oapi-codegen/runtime v1.0.0 // indirect
34+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
35+
github.com/pmezard/go-difflib v1.0.0 // indirect
36+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
37+
github.com/ugorji/go/codec v1.2.12 // indirect
38+
golang.org/x/arch v0.8.0 // indirect
39+
golang.org/x/crypto v0.23.0 // indirect
40+
golang.org/x/net v0.25.0 // indirect
41+
golang.org/x/sys v0.20.0 // indirect
42+
golang.org/x/text v0.15.0 // indirect
43+
google.golang.org/protobuf v1.34.1 // indirect
44+
gopkg.in/yaml.v3 v3.0.1 // indirect
45+
)

go.sum

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
2+
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
3+
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
4+
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
5+
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
6+
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
7+
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
8+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
9+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
10+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
11+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
12+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
13+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
15+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
16+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
17+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
18+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
19+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
20+
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
21+
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
22+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
23+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
24+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
25+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
26+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
27+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
28+
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
29+
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
30+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
31+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
32+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
33+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
34+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
35+
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
36+
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
37+
github.com/influxdata/influxdb-client-go/v2 v2.13.0 h1:ioBbLmR5NMbAjP4UVA5r9b5xGjpABD7j65pI8kFphDM=
38+
github.com/influxdata/influxdb-client-go/v2 v2.13.0/go.mod h1:k+spCbt9hcvqvUiz0sr5D8LolXHqAAOfPw9v/RIRHl4=
39+
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU=
40+
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
41+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
42+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
43+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
44+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
45+
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
46+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
47+
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
48+
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
49+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
50+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
51+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
52+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
53+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
54+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
55+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
56+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
57+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
58+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
59+
github.com/oapi-codegen/runtime v1.0.0 h1:P4rqFX5fMFWqRzY9M/3YF9+aPSPPB06IzP2P7oOxrWo=
60+
github.com/oapi-codegen/runtime v1.0.0/go.mod h1:LmCUMQuPB4M/nLXilQXhHw+BLZdDb18B34OO356yJ/A=
61+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
62+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
63+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
64+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
65+
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
66+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
67+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
68+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
69+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
70+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
71+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
72+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
73+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
74+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
75+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
76+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
77+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
78+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
79+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
80+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
81+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
82+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
83+
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
84+
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
85+
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
86+
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
87+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
88+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
89+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
90+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
91+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
92+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
93+
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
94+
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
95+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U=
96+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
97+
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
98+
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
99+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
100+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
101+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
102+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
103+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
104+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
105+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

internal/config/config.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Package config handles loading and validating configuration from environment variables.
2+
package config
3+
4+
import (
5+
"fmt"
6+
"os"
7+
"strconv"
8+
)
9+
10+
// Config holds all runtime configuration for the service.
11+
type Config struct {
12+
InfluxURL string
13+
InfluxToken string
14+
InfluxOrg string
15+
InfluxBucket string
16+
IngestionAPIKey string
17+
Port string
18+
InfluxBatchSize int
19+
InfluxFlushIntervalMS int
20+
InfluxMaxRetries int
21+
InfluxRetryIntervalMS int
22+
}
23+
24+
// Load reads configuration from environment variables.
25+
// It returns an error listing all missing required variables so the caller can
26+
// fail fast with a single descriptive message.
27+
func Load() (Config, error) {
28+
cfg := Config{}
29+
var missing []string
30+
31+
required := func(key string) string {
32+
v := os.Getenv(key)
33+
if v == "" {
34+
missing = append(missing, key)
35+
}
36+
return v
37+
}
38+
39+
cfg.InfluxURL = required("INFLUX_URL")
40+
cfg.InfluxToken = required("INFLUX_TOKEN")
41+
cfg.InfluxOrg = required("INFLUX_ORG")
42+
cfg.InfluxBucket = required("INFLUX_BUCKET")
43+
cfg.IngestionAPIKey = required("INGESTION_API_KEY")
44+
45+
if len(missing) > 0 {
46+
return Config{}, fmt.Errorf("missing required environment variables: %v", missing)
47+
}
48+
49+
cfg.Port = os.Getenv("PORT")
50+
if cfg.Port == "" {
51+
cfg.Port = "8080"
52+
}
53+
54+
cfg.InfluxBatchSize = envInt("INFLUX_BATCH_SIZE", 500)
55+
cfg.InfluxFlushIntervalMS = envInt("INFLUX_FLUSH_INTERVAL_MS", 1000)
56+
cfg.InfluxMaxRetries = envInt("INFLUX_MAX_RETRIES", 3)
57+
cfg.InfluxRetryIntervalMS = envInt("INFLUX_RETRY_INTERVAL_MS", 500)
58+
59+
return cfg, nil
60+
}
61+
62+
// envInt reads an integer environment variable, returning def if absent or unparseable.
63+
func envInt(key string, def int) int {
64+
s := os.Getenv(key)
65+
if s == "" {
66+
return def
67+
}
68+
v, err := strconv.Atoi(s)
69+
if err != nil {
70+
return def
71+
}
72+
return v
73+
}

internal/handler/health.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Package handler contains the HTTP handler functions for the health ingestion API.
2+
package handler
3+
4+
import (
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
// Version is injected at build time via ldflags: -X main.version=<sha>.
9+
// The main package sets handler.Version = version after loading it from the linker.
10+
var Version = "dev"
11+
12+
// Health handles GET /health and returns a simple liveness response.
13+
// It is unauthenticated and is suitable for use as a load-balancer health check.
14+
func Health(c *gin.Context) {
15+
c.JSON(200, gin.H{"status": "ok", "version": Version})
16+
}

internal/handler/health_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package handler
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func init() {
15+
gin.SetMode(gin.TestMode)
16+
}
17+
18+
func TestHealthCheck(t *testing.T) {
19+
r := gin.New()
20+
r.GET("/health", Health)
21+
22+
w := httptest.NewRecorder()
23+
req, _ := http.NewRequest(http.MethodGet, "/health", nil)
24+
r.ServeHTTP(w, req)
25+
26+
assert.Equal(t, http.StatusOK, w.Code)
27+
28+
var body map[string]interface{}
29+
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
30+
assert.Equal(t, "ok", body["status"])
31+
_, hasVersion := body["version"]
32+
assert.True(t, hasVersion)
33+
}

0 commit comments

Comments
 (0)