From c2c384cf650a6cce31b250782e159418c14bceff Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Wed, 3 Apr 2019 08:21:25 +0200 Subject: [PATCH] Add Dockerfile --- .dockerignore | 4 ++++ Dockerfile | 22 ++++++++++++++++++++++ database.go | 5 ++++- helpers/packrSource.go | 9 ++++----- 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3275d2c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.idea +data +.dockerignore +Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..62d48eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.12-alpine as packr +RUN apk add --no-cache git +RUN go get github.com/gobuffalo/packr/v2/packr2 + +FROM golang:1.12-alpine as build +COPY --from=packr /go/bin/packr2 /go/bin +ADD . /app +WORKDIR /app +RUN apk add --no-cache git gcc musl-dev +RUN GO111MODULE=on packr2 +RUN go build kis3.dev/kis3 + +FROM alpine:3.9 +RUN adduser -S -D -H -h /app kis3 +COPY --from=build /app/kis3 /app/ +RUN chown -R kis3 /app +USER kis3 +WORKDIR /app +RUN mkdir data +VOLUME ["/app/data"] +EXPOSE 8080 +CMD ["./kis3"] diff --git a/database.go b/database.go index 3f15206..5787d5e 100644 --- a/database.go +++ b/database.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "fmt" + "github.com/gobuffalo/packr/v2" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/sqlite3" _ "github.com/mattn/go-sqlite3" @@ -30,7 +31,9 @@ func initDatabase() (database *Database, e error) { } func migrateDatabase(database *sql.DB) (e error) { - sourceDriver, e := (&helpers.PackrSource{}).Open("") + sourceDriver, e := (&helpers.PackrSource{ + Box: packr.New("migrations", "migrations"), + }).Open("") if e != nil { return } diff --git a/helpers/packrSource.go b/helpers/packrSource.go index 841314e..8d9fb5b 100644 --- a/helpers/packrSource.go +++ b/helpers/packrSource.go @@ -15,13 +15,13 @@ import ( type PackrSource struct { lock sync.Mutex - box *packr.Box + Box *packr.Box migrations *source.Migrations } func (s *PackrSource) loadMigrations() (*source.Migrations, error) { migrations := source.NewMigrations() - for _, filename := range s.box.List() { + for _, filename := range s.Box.List() { migration, err := source.Parse(filename) if err != nil { return nil, err @@ -34,7 +34,6 @@ func (s *PackrSource) loadMigrations() (*source.Migrations, error) { func (s *PackrSource) Open(url string) (source.Driver, error) { s.lock.Lock() defer s.lock.Unlock() - s.box = packr.New("migrations", "../migrations") if migrations, err := s.loadMigrations(); err != nil { return nil, err } else { @@ -78,7 +77,7 @@ func (s *PackrSource) ReadUp(version uint) (r io.ReadCloser, identifier string, if migration, ok := s.migrations.Up(version); !ok { return nil, "", os.ErrNotExist } else { - b, _ := s.box.Find(migration.Raw) + b, _ := s.Box.Find(migration.Raw) return ioutil.NopCloser(bytes.NewBuffer(b)), migration.Identifier, nil @@ -89,7 +88,7 @@ func (s *PackrSource) ReadDown(version uint) (r io.ReadCloser, identifier string if migration, ok := s.migrations.Down(version); !ok { return nil, "", migrate.ErrNilVersion } else { - b := s.box.Bytes(migration.Raw) + b := s.Box.Bytes(migration.Raw) return ioutil.NopCloser(bytes.NewBuffer(b)), migration.Identifier, nil