Skip to content

Commit

Permalink
Integrate stuffbin to create a standalone bin and add goreleaser conf
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Aug 26, 2019
1 parent b1ce3f7 commit 06b7327
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 24 deletions.
19 changes: 19 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# .goreleaser.yml
builds:
- binary: dictmaker
goos:
- darwin
- linux
goarch:
- amd64

hooks:
# stuff executables with static assets.
post: make pack-releases

archive:
format: tar.gz
files:
- README.md
- LICENSE
- sample/*
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
STATIC := config.toml.sample schema.sql queries.sql

# Install dependencies needed for building
.PHONY: deps
deps:
go get -u github.com/knadh/stuffbin/...

.PHONY: build
build:
go build -o dictmaker
stuffbin -a stuff -in dictmaker -out dictmaker ${STATIC}

.PHONY: build-tokenizers
build-tokenizers:
# Compile the Kannada tokenizer.
go build -ldflags="-s -w" -buildmode=plugin -o kannada.tk tokenizers/kannada/kannada.go

.PHONY: run
run: build
./dictmaker --site alar
# pack-releases runs stuffbin packing on a given list of
# binaries. This is used with goreleaser for packing
# release builds for cross-build targets.
.PHONY: pack-releases
pack-releases:
$(foreach var,$(RELEASE_BUILDS),stuffbin -a stuff -in ${var} -out ${var} ${STATIC} $(var);)
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ dictmaker has no concept of language or semantics. To make a universal dictionar
| `tags` | `TEXT[]` | Optional tags |
| `notes` | `TEXT` | Optional text notes |

# How to build a dictionary
# Installation
1. Download the latest release [release](https://github.com/knadh/dictmaker/releases) of dictmaker
2. Create a Postgres DB and execute `schema.sql` on it to create the tables
3. Define your dictionary's languages and properties along with other configuration in `config.toml`
4. Populate the `entries` and `relations` tables with your dictionary data. See the "Sample dictionary" section below
5. Run the binary: `./dictmaker`
2. Run `./dictmaker --new` to generate a sample config.toml and DB schema.sql
3. Create a Postgres DB and execute `schema.sql` on it to create the tables
4. Define your dictionary's languages and properties along with other configuration in `config.toml`
5. Populate the `entries` and `relations` tables with your dictionary data. See the "Sample dictionary" section below
6. Run the binary: `./dictmaker`

## Dictionary query API
```shell
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/knadh/knphone v0.0.0-20190624172359-d31f66505cbf
github.com/knadh/koanf v0.4.4
github.com/knadh/paginator v0.0.0-20190812121607-b4fbeb7c977b
github.com/knadh/stuffbin v1.0.0
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v1.2.0
github.com/mattn/go-sqlite3 v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/knadh/koanf v0.4.4 h1:Pg+eR7wuJtCGHLeip31K20eJojjZ3lXE8ILQQGj2PTM=
github.com/knadh/koanf v0.4.4/go.mod h1:Qd5yvXN39ZzjoRJdXMKN2QqHzQKhSx/K8fU5gyn4LPs=
github.com/knadh/paginator v0.0.0-20190812121607-b4fbeb7c977b h1:1nu8rKo3secxUYyD725+fnEMGz38+cgaBS1c+jBI4Hk=
github.com/knadh/paginator v0.0.0-20190812121607-b4fbeb7c977b/go.mod h1:80FK5OPRRQQKEK75ahG+92/MdX/lu4dE8loTzJRVcCQ=
github.com/knadh/stuffbin v1.0.0 h1:NQon6PTpLXies4bRFhS3VpLCf6y+jn6YVXU3i2wPQ+M=
github.com/knadh/stuffbin v1.0.0/go.mod h1:yVCFaWaKPubSNibBsTAJ939q2ABHudJQxRWZWV5yh+4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
57 changes: 57 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package main

import (
"errors"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"plugin"
"strings"

"github.com/go-chi/chi"
"github.com/jmoiron/sqlx"
"github.com/knadh/dictmaker/search"
"github.com/knadh/stuffbin"
)

// connectDB initializes a database connection.
Expand All @@ -25,6 +29,31 @@ func connectDB(host string, port int, user, pwd, dbName string) (*sqlx.DB, error
return db, nil
}

// initFileSystem initializes the stuffbin FileSystem to provide
// access to bunded static assets to the app.
func initFileSystem(binPath string) (stuffbin.FileSystem, error) {
fs, err := stuffbin.UnStuff(os.Args[0])
if err == nil {
return fs, nil
}

// Running in local mode. Load the required static assets into
// the in-memory stuffbin.FileSystem.
logger.Printf("unable to initialize embedded filesystem: %v", err)
logger.Printf("using local filesystem for static assets")
files := []string{
"config.toml.sample",
"queries.sql",
"schema.sql",
}

fs, err = stuffbin.NewLocalFS("/", files...)
if err != nil {
return nil, fmt.Errorf("failed to initialize local file for assets: %v", err)
}
return fs, nil
}

// loadTheme loads a theme from a directory.
func loadSiteTheme(path string, loadPages bool) (*template.Template, error) {
t := template.New("theme")
Expand Down Expand Up @@ -135,3 +164,31 @@ func loadLanguages(app *App) error {
}
return nil
}

func generateNewFiles() error {
if _, err := os.Stat("config.toml"); !os.IsNotExist(err) {
return errors.New("config.toml exists. Remove it to generate a new one")
}

// Initialize the static file system into which all
// required static assets (.sql, .js files etc.) are loaded.
fs, err := initFileSystem(os.Args[0])
if err != nil {
return err
}

// Generate config file.
b, err := fs.Read("config.toml.sample")
if err != nil {
return fmt.Errorf("error reading sample config (is binary stuffed?): %v", err)
}
ioutil.WriteFile("config.toml", b, 0644)

// Generate schema file.
b, err = fs.Read("schema.sql")
if err != nil {
return fmt.Errorf("error reading schema.sql (is binary stuffed?): %v", err)
}
ioutil.WriteFile("schema.sql", b, 0644)
return nil
}
55 changes: 39 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/paginator"
"github.com/knadh/stuffbin"
flag "github.com/spf13/pflag"
)

Expand Down Expand Up @@ -45,6 +46,7 @@ type App struct {
db *sqlx.DB
queries *search.Queries
search *search.Search
fs stuffbin.FileSystem
resultsPg *paginator.Paginator
glossaryPg *paginator.Paginator
logger *log.Logger
Expand All @@ -62,24 +64,36 @@ func init() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
f.Bool("new", false, "generate a new sample config.toml file.")
f.StringSlice("config", []string{"config.toml"},
"path to one or more config files (will be merged in order)")
f.String("site", "", "path to a site theme. If left empty, only the APIs will run.")
f.Bool("install", false, "run first time installation")
f.Bool("version", false, "current version of the build")
f.Parse(os.Args[1:])

// Generate new config file.
if ok, _ := f.GetBool("new"); ok {
if err := generateNewFiles(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("config.toml and schema.sql generated. You can edit the config now.")
os.Exit(0)
}

// Load config files.
cFiles, _ := f.GetStringSlice("config")
for _, f := range cFiles {
log.Printf("reading config: %s", f)
logger.Printf("reading config: %s", f)
if err := ko.Load(file.Provider(f), toml.Parser()); err != nil {
log.Printf("error reading config: %v", err)
fmt.Printf("error reading config: %v", err)
os.Exit(1)
}
}

if err := ko.Load(posflag.Provider(f, ".", ko), nil); err != nil {
log.Fatalf("error loading config: %v", err)
logger.Fatalf("error loading config: %v", err)
}
}

Expand All @@ -95,8 +109,28 @@ func main() {
}
defer db.Close()

fs, err := initFileSystem(os.Args[0])
if err != nil {
logger.Fatal(err)
}

// Initialize the app context that's passed around.
app := &App{
constants: &constants{
Site: ko.String("site"),
},
lang: make(map[string]Lang),
db: db,
fs: fs,
logger: logger,
}

// Load SQL queries.
qMap, err := goyesql.ParseFile("queries.sql")
qB, err := fs.Read("/queries.sql")
if err != nil {
logger.Fatalf("error reading queries.sql: %v", err)
}
qMap, err := goyesql.ParseBytes(qB)
if err != nil {
logger.Fatalf("error loading SQL queries: %v", err)
}
Expand All @@ -106,18 +140,7 @@ func main() {
if err := goyesqlx.ScanToStruct(&q, qMap, db.Unsafe()); err != nil {
logger.Fatalf("no SQL queries loaded: %v", err)
}

// Initialize the app context that's passed around.
app := &App{
constants: &constants{
Site: ko.String("site"),
},
lang: make(map[string]Lang),
search: search.NewSearch(&q),
queries: &q,
db: db,
logger: logger,
}
app.search = search.NewSearch(&q)

// Pagination.
o := paginator.Default()
Expand Down

0 comments on commit 06b7327

Please sign in to comment.