From 75b0374c93dbfc34f1d33245fd9b23687ccc5d1b Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 21 Sep 2020 13:04:20 -0700 Subject: [PATCH] initial --- .drone.yml | 13 ++++++ Dockerfile | 37 +++++++++++++++++ Makefile | 56 ++++++++++++++++++++++++++ README.md | 19 +++++++++ cmd/historyposter/main.go | 19 +++++++++ process/historyposter.go | 84 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 228 insertions(+) create mode 100644 .drone.yml create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 README.md create mode 100644 cmd/historyposter/main.go create mode 100644 process/historyposter.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..e6dcd2f --- /dev/null +++ b/.drone.yml @@ -0,0 +1,13 @@ +kind: pipeline +name: test-docker-build + +steps: +- name: test-docker-build + image: plugins/docker + network_mode: bridge + settings: + repo: sneak/historyposter + dry_run: true + tags: + - ${DRONE_COMMIT_SHA} + - ${DRONE_BRANCH} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6e71885 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +## build image: +ARG GO_VERSION=1.15 +FROM golang:${GO_VERSION}-alpine AS builder + +RUN mkdir /user && \ + echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \ + echo 'nobody:x:65534:' > /user/group +RUN apk add --no-cache ca-certificates git bzip2 make gcc libc-dev + +RUN mkdir -p /go/src/git.eeqj.de/sneak/historyposter +WORKDIR /go/src/git.eeqj.de/sneak/historyposter + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY ./ ./ +RUN make build +RUN tar -c /go | bzip2 > /go.tbz2 + +## output image: +FROM scratch as final +COPY --from=builder /user/group /user/passwd /etc/ +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /go/src/git.eeqj.de/sneak/historyposter/historyposter /app/historyposter +COPY --from=builder /go.tbz2 /go.tbz2 + +WORKDIR /app + +ENV PORT 8080 +ENV DBURL none + +EXPOSE 8080 + +USER nobody:nobody + +ENTRYPOINT ["./historyposter"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4f48248 --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +VERSION := $(shell git rev-parse --short HEAD) +BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') +BUILDTIMEFILENAME := $(shell date -u '+%Y%m%d-%H%M%SZ') +BUILDTIMETAG := $(shell date -u '+%Y%m%d%H%M%S') +BUILDUSER := $(shell whoami) +BUILDHOST := $(shell hostname -s) +BUILDARCH := $(shell uname -m) + +FN := historyposter + +UNAME_S := $(shell uname -s) + +GOLDFLAGS += -X main.Version=$(VERSION) +GOLDFLAGS += -X main.Buildarch=$(BUILDARCH) + +# osx can't statically link apparently?! +ifeq ($(UNAME_S),Darwin) + GOFLAGS := -ldflags "$(GOLDFLAGS)" +endif + +ifneq ($(UNAME_S),Darwin) + GOFLAGS = -ldflags "-linkmode external -extldflags -static $(GOLDFLAGS)" +endif + +default: fmt + +fmt: + go fmt ./... + goimports -l -w . + +debug: build + GOTRACEBACK=all DEBUG=1 ./$(FN) 2>&1 | tee -a debug.log + +run: build + ./$(FN) + +clean: + -rm ./$(FN) + +build: ./$(FN) + +docker: + docker build . + +go-get: + cd cmd/$(FN) && go get -v + +./$(FN): */*.go cmd/*/*.go go-get + cd cmd/$(FN) && go build -o ../../$(FN) $(GOFLAGS) . + +vet: + go lint ./... + go vet ./... + bash -c 'test -z "$$(gofmt -l .)"' + +.PHONY: build fmt test is_uncommitted docker dist hub upload-docker-image clean run rundebug default build-docker-image-dist diff --git a/README.md b/README.md new file mode 100644 index 0000000..33f7b1f --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# historyposter + +a daemon that watches local browser history on macOS and POSTs it up to an +API somewhere + +i'm going to be using this to send history to an API that uses youtube-dl to +download/cache/archive every single youtube video i view + +# author + +sneak + +https://sneak.berlin + +sneak@sneak.berlin + +# license + +wtfpl diff --git a/cmd/historyposter/main.go b/cmd/historyposter/main.go new file mode 100644 index 0000000..cdaa758 --- /dev/null +++ b/cmd/historyposter/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "os" + + "git.eeqj.de/sneak/historyposter/process" +) + +// these are filled in at link-time by the build scripts + +// Version is the git version of the app +var Version string + +// Buildarch contains the architecture it is compiled for +var Buildarch string + +func main() { + os.Exit(process.CLIEntry(Version, Buildarch)) +} diff --git a/process/historyposter.go b/process/historyposter.go new file mode 100644 index 0000000..c1bd69c --- /dev/null +++ b/process/historyposter.go @@ -0,0 +1,84 @@ +package process + +import ( + "context" + "fmt" + "github.com/k0kubun/pp" + "github.com/mattn/go-isatty" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/spf13/viper" + "os" + "time" +) + +// CLIEntry is the main entrypoint +func CLIEntry(version string, buildarch string) int { + + hp := new(HistoryPoster) + hp.version = version + hp.buildarch = buildarch + hp.startup = time.Now() + + c := make(chan os.Signal, 1) + signal.Notify(hp.shutdownChannel, os.Interrupt) + + hp.configure() + hp.setupLogging() + hp.setupDatabase() + + ctx, cancel := context.WithCancel(context.Background()) + go func() { + sig := <-c + log.Info().Msgf("signal received: %+v", sig) + // FIXME this might need to be scoped to a specific signal + cancel() + }() + return hp.runForever(ctx) +} + +type HistoryPoster struct { + version string + buildarch string + startup time.Time +} + +func (hp *HistoryPoster) runForever(ctx context.Context) int { + +} + +func (hp *HistoryPoster) setupLogging() { + + log.Logger = log.With().Caller().Logger() + + tty := isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) + + if tty { + out := zerolog.NewConsoleWriter( + func(w *zerolog.ConsoleWriter) { + // Customize time format + w.TimeFormat = time.RFC3339 + }, + ) + log.Logger = log.Output(out) + } + + // always log in UTC + zerolog.TimestampFunc = func() time.Time { + return time.Now().UTC() + } + + zerolog.SetGlobalLevel(zerolog.InfoLevel) + if viper.GetBool("debug") { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } + + hp.identify() +} + +func (hp *HistoryPoster) identify() { + log.Info(). + Str("version", hp.version). + Str("buildarch", hp.buildarch). + Msg("starting") +}