This commit is contained in:
Jeffrey Paul 2020-09-21 13:04:20 -07:00
commit 75b0374c93
6 changed files with 228 additions and 0 deletions

13
.drone.yml Normal file
View File

@ -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}

37
Dockerfile Normal file
View File

@ -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"]

56
Makefile Normal file
View File

@ -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

19
README.md Normal file
View File

@ -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

19
cmd/historyposter/main.go Normal file
View File

@ -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))
}

84
process/historyposter.go Normal file
View File

@ -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")
}