Skip to content

Commit

Permalink
ref: embed files, bump to go 1.16 (muety#167)
Browse files Browse the repository at this point in the history
* ref: embed portion of files
* fix: readd pkger
* ref: embed version.txt
* fix: wrong mail template import path
* refactor: get rid of sql-migrate
refactor: get rid of pkger in favor of go embed (resolve muety#164)
* chore: remove unused var [ci-skip]

Co-authored-by: Ferdinand Mütsch <[email protected]>
  • Loading branch information
YC and muety authored Apr 11, 2021
1 parent 2a9fbfd commit 6256c8e
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 448 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/linux-build-on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ jobs:

- name: Get dependencies
run: |
go get github.com/markbates/pkger/cmd/pkger
go get
go generate
- name: Build
run: GO111MODULE=on go build -v .
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/win-build-on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ jobs:

- name: Get dependencies
run: |
go get github.com/markbates/pkger/cmd/pkger
go get
go generate
- name: Enable Go 1.11 modules
run: cmd /c "set GO111MODULE=on"
Expand Down
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Build Stage

FROM golang:1.15 AS build-env
FROM golang:1.16 AS build-env
WORKDIR /src

ADD ./go.mod .
RUN go mod download && go get github.com/markbates/pkger/cmd/pkger
RUN go mod download

RUN curl "https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh" -o wait-for-it.sh && \
chmod +x wait-for-it.sh

ADD . .
RUN go generate && go build -o wakapi
RUN go build -o wakapi

WORKDIR /app
RUN cp /src/wakapi . && \
Expand All @@ -31,6 +31,7 @@ WORKDIR /app
RUN apt update && \
apt install -y ca-certificates

# See README.md and config.default.yml for all config options
ENV ENVIRONMENT prod
ENV WAKAPI_DB_TYPE sqlite3
ENV WAKAPI_DB_USER ''
Expand Down
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ $ ./wakapi

### 🧑‍💻 Option 4: Run from source
#### Prerequisites
* Go >= 1.13 (with `$GOPATH` properly set)
* Go >= 1.16 (with `$GOPATH` properly set)
* gcc (to compile [go-sqlite3](https://github.com/mattn/go-sqlite3))
* Fedora / RHEL: `dnf install @development-tools`
* Ubuntu / Debian: `apt install build-essential`
Expand All @@ -121,12 +121,7 @@ $ ./wakapi
$ cp config.default.yml config.yml
$ vi config.yml

# Install packaging tool
$ export GO111MODULE=on
$ go get github.com/markbates/pkger/cmd/pkger

# Build the executable
$ go generate
$ go build -o wakapi

# Run it
Expand Down
61 changes: 8 additions & 53 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"

"github.com/emvi/logbuch"
"github.com/getsentry/sentry-go"
"github.com/gorilla/securecookie"
"github.com/jinzhu/configor"
"github.com/markbates/pkger"
"github.com/muety/wakapi/data"
"github.com/muety/wakapi/models"
migrate "github.com/rubenv/sql-migrate"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"io/ioutil"
"net/http"
"os"
"strings"
)

const (
Expand Down Expand Up @@ -188,24 +187,6 @@ func (c *Config) GetMigrationFunc(dbDialect string) models.MigrationFunc {
}
}

func (c *Config) GetFixturesFunc(dbDialect string) models.MigrationFunc {
return func(db *gorm.DB) error {
migrations := &migrate.HttpFileSystemMigrationSource{
FileSystem: pkger.Dir("/migrations"),
}

migrate.SetIgnoreUnknown(true)
sqlDb, _ := db.DB()
n, err := migrate.Exec(sqlDb, dbDialect, migrations, migrate.Up)
if err != nil {
return err
}

logbuch.Info("applied %d fixtures", n)
return nil
}
}

func (c *dbConfig) GetDialector() gorm.Dialector {
switch c.Dialect {
case SQLDialectMysql:
Expand Down Expand Up @@ -284,21 +265,6 @@ func IsDev(env string) bool {
return env == "dev" || env == "development"
}

func readVersion() string {
file, err := pkger.Open("/version.txt")
if err != nil {
logbuch.Fatal(err.Error())
}
defer file.Close()

bytes, err := ioutil.ReadAll(file)
if err != nil {
logbuch.Fatal(err.Error())
}

return strings.TrimSpace(string(bytes))
}

func readColors() map[string]map[string]string {
// Read language colors
// Source:
Expand All @@ -309,18 +275,7 @@ func readColors() map[string]map[string]string {
// – $x('//span[@class="editor-icon tip"]/@data-original-title').map(e => e.nodeValue)
// – $x('//span[@class="editor-icon tip"]/div[1]/text()').map(e => e.nodeValue)
var colors = make(map[string]map[string]string)

file, err := pkger.Open("/data/colors.json")
if err != nil {
logbuch.Fatal(err.Error())
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
logbuch.Fatal(err.Error())
}

if err := json.Unmarshal(bytes, &colors); err != nil {
if err := json.Unmarshal(data.ColorsFile, &colors); err != nil {
logbuch.Fatal(err.Error())
}

Expand Down Expand Up @@ -396,7 +351,7 @@ func Get() *Config {
return cfg
}

func Load() *Config {
func Load(version string) *Config {
config := &Config{}

flag.Parse()
Expand All @@ -405,7 +360,7 @@ func Load() *Config {
logbuch.Fatal("failed to read config: %v", err)
}

config.Version = readVersion()
config.Version = strings.TrimSpace(version)
config.App.Colors = readColors()
config.Db.Dialect = resolveDbDialect(config.Db.Type)
config.Security.SecureCookie = securecookie.New(
Expand Down
6 changes: 6 additions & 0 deletions data/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package data

import _ "embed"

//go:embed colors.json
var ColorsFile []byte
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ services:
- 3000:3000
restart: always
environment:
# See README.md and config.default.yml for all config options
WAKAPI_DB_TYPE: "postgres"
WAKAPI_DB_NAME: "wakapi"
WAKAPI_DB_USER: "wakapi"
WAKAPI_DB_PASSWORD: "CHANGE_ME!!!"
WAKAPI_DB_PASSWORD: "choose-a-password"
WAKAPI_DB_HOST: "db"
WAKAPI_DB_PORT: "5432"
ENVIRONMENT: "prod"
Expand All @@ -19,5 +20,5 @@ services:
image: postgres:12.3
environment:
POSTGRES_USER: "wakapi"
POSTGRES_PASSWORD: "CHANGE_ME!!!"
POSTGRES_PASSWORD: "choose-a-password"
POSTGRES_DB: "wakapi"
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/muety/wakapi

go 1.13
go 1.16

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
Expand All @@ -16,11 +16,9 @@ require (
github.com/gorilla/securecookie v1.1.1
github.com/jinzhu/configor v1.2.0
github.com/mailru/easyjson v0.7.7 // indirect
github.com/markbates/pkger v0.17.1
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/mitchellh/hashstructure/v2 v2.0.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/rubenv/sql-migrate v0.0.0-20200402132117-435005d389bc
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.6.1
github.com/swaggo/swag v1.7.0
Expand Down
Loading

0 comments on commit 6256c8e

Please sign in to comment.