feat: move cmd to own dir
This commit is contained in:
parent
0fe5c8ce03
commit
a26d337ff9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
cache.json
|
||||
dist/*
|
||||
|
@ -1,6 +1,7 @@
|
||||
project_name: "glucose_exporter"
|
||||
builds:
|
||||
- env:
|
||||
- main: ./cmd/glucose_exporter/
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- linux
|
||||
@ -10,9 +11,9 @@ builds:
|
||||
- "-s"
|
||||
- "-w"
|
||||
- "-extldflags '-static'"
|
||||
- "-X git.wobcom.de/smartmetering/kerouac/cmd.version={{.Version}}"
|
||||
- "-X git.wobcom.de/smartmetering/kerouac/cmd.commit={{.ShortCommit}}"
|
||||
- "-X git.wobcom.de/smartmetering/kerouac/cmd.date={{.Date}}"
|
||||
- "-X main.version={{.Version}}"
|
||||
- "-X main.commit={{.ShortCommit}}"
|
||||
- "-X main.date={{.Date}}"
|
||||
checksum:
|
||||
name_template: "checksums.txt"
|
||||
snapshot:
|
||||
|
15
Earthfile
15
Earthfile
@ -1,5 +1,5 @@
|
||||
VERSION 0.7
|
||||
ARG --global GO_VERSION=1.22.0
|
||||
ARG --global GO_VERSION=1.22.1
|
||||
ARG --global GOLANGCILINT_VERSION=v1.54.2
|
||||
ARG --global GORELEASER_VERSION=v1.24.0
|
||||
FROM golang:$GO_VERSION-alpine3.18
|
||||
@ -10,12 +10,14 @@ SAVE_CODE:
|
||||
SAVE ARTIFACT go.mod AS LOCAL go.mod
|
||||
SAVE ARTIFACT go.sum AS LOCAL go.sum
|
||||
SAVE ARTIFACT api AS LOCAL api
|
||||
SAVE ARTIFACT cmd AS LOCAL cmd
|
||||
SAVE ARTIFACT httpslog AS LOCAL httpslog
|
||||
SAVE ARTIFACT internal AS LOCAL internal
|
||||
|
||||
code:
|
||||
COPY go.mod go.sum ./
|
||||
COPY --dir api ./
|
||||
COPY --dir cmd ./
|
||||
COPY --dir httpslog ./
|
||||
COPY --dir internal ./
|
||||
COPY --dir vendor ./
|
||||
@ -71,8 +73,17 @@ goreleaser:
|
||||
RUN goreleaser $CMD
|
||||
SAVE ARTIFACT dist dist
|
||||
|
||||
ssl-certs:
|
||||
RUN set -ex \
|
||||
&& apk add --no-cache ca-certificates
|
||||
SAVE ARTIFACT /etc/ssl/certs/ca-certificates.crt ca-certificates.crt
|
||||
|
||||
docker:
|
||||
FROM scratch
|
||||
ARG DOCKER_TAG
|
||||
COPY +goreleaser/dist/glucose_exporter_linux_amd64_v1/glucose_exporter /bin/glucose_exporter
|
||||
COPY +ssl-certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY +goreleaser/dist/glucose_exporter_linux_amd64_v1/glucose_exporter /glucose_exporter
|
||||
VOLUME /var/cache/glucose_exporter
|
||||
ENTRYPOINT ["/glucose_exporter"]
|
||||
CMD ["serve"]
|
||||
SAVE IMAGE --push ghcr.io/xsteadfastx/glucose_exporter:$DOCKER_TAG
|
||||
|
@ -1,7 +1,10 @@
|
||||
//nolint:forbidigo,gochecknoglobals
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -9,19 +12,58 @@ import (
|
||||
|
||||
"github.com/caarlos0/env/v10"
|
||||
"github.com/lmittmann/tint"
|
||||
|
||||
"go.xsfx.dev/glucose_exporter/httpslog"
|
||||
"go.xsfx.dev/glucose_exporter/internal/cache"
|
||||
"go.xsfx.dev/glucose_exporter/internal/config"
|
||||
"go.xsfx.dev/glucose_exporter/internal/metrics"
|
||||
)
|
||||
|
||||
const addr = ":2112"
|
||||
var (
|
||||
version = "dev"
|
||||
commit = "none"
|
||||
date = "unknown"
|
||||
)
|
||||
|
||||
const help = "expected 'serve' or 'version' subcommands"
|
||||
|
||||
func main() {
|
||||
logOpts := &tint.Options{Level: slog.LevelInfo, TimeFormat: time.Kitchen}
|
||||
initLogging(logOpts)
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
slog.Error(help)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
switch os.Args[1] {
|
||||
case "serve":
|
||||
flags := flag.NewFlagSet("serve", flag.ExitOnError)
|
||||
|
||||
addr := flags.String("addr", ":2112", "address to listen")
|
||||
flags.BoolFunc("help", "print help", func(_ string) error {
|
||||
flags.PrintDefaults()
|
||||
os.Exit(0)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := flags.Parse(os.Args[2:]); err != nil {
|
||||
slog.Error("parsing flags", "err", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
serveCmd(*addr)
|
||||
|
||||
case "version":
|
||||
versionCmd()
|
||||
|
||||
default:
|
||||
slog.Error(help)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func serveCmd(addr string) {
|
||||
if err := env.Parse(&config.Cfg); err != nil {
|
||||
slog.Error("parsing env config", "err", err)
|
||||
os.Exit(1)
|
||||
@ -40,9 +82,9 @@ func main() {
|
||||
if err := os.WriteFile(cache.FullPath(), []byte("{}"), 0o600); err != nil {
|
||||
cacheFileLogger.Error("init cache file", "err", err)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
cacheFileLogger.Debug("init cache file")
|
||||
}
|
||||
|
||||
cacheFileLogger.Debug("init cache file")
|
||||
} else {
|
||||
cacheFileLogger.Error("getting cache file stat", "err", err)
|
||||
os.Exit(1)
|
||||
@ -54,12 +96,23 @@ func main() {
|
||||
|
||||
slog.Info("listening", "addr", addr)
|
||||
|
||||
if err := http.ListenAndServe(addr, httpslog.Handler()(mux)); err != nil {
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
ReadTimeout: time.Second,
|
||||
ReadHeaderTimeout: time.Second,
|
||||
Handler: httpslog.Handler()(mux),
|
||||
}
|
||||
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
slog.Error("listen and serve", "err", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func versionCmd() {
|
||||
fmt.Printf("glucose_exporter %s, commit %s, %s\n", version, commit, date)
|
||||
}
|
||||
|
||||
func initLogging(opts *tint.Options) {
|
||||
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, opts)))
|
||||
}
|
Loading…
Reference in New Issue
Block a user