diff --git a/Dockerfile b/Dockerfile index b4058d2d..bcb19b9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,8 +12,7 @@ COPY go.mod . COPY go.sum . RUN go mod download COPY . . -RUN ./_do_gen_assets -RUN ./_do_build_server +RUN GOOS=linux go build -o gonic cmd/gonic/gonic.go FROM alpine:3.13.1 RUN apk add -U --no-cache \ @@ -21,6 +20,7 @@ RUN apk add -U --no-cache \ ca-certificates \ tzdata \ tini + COPY --from=builder \ /usr/lib/libgcc_s.so.1 \ /usr/lib/libstdc++.so.6 \ diff --git a/Dockerfile.debug b/Dockerfile.debug index c7c49950..d5f31be6 100644 --- a/Dockerfile.debug +++ b/Dockerfile.debug @@ -1,6 +1,6 @@ # syntax=docker/dockerfile:experimental -FROM golang:1.14-alpine AS builder +FROM golang:1.16-alpine AS builder RUN apk add -U --no-cache \ build-base \ ca-certificates \ diff --git a/Dockerfile.dev b/Dockerfile.dev index e32230cc..0f7a6e85 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,6 +1,6 @@ # syntax=docker/dockerfile:experimental -FROM golang:1.15-alpine AS builder +FROM golang:1.16-alpine AS builder RUN apk add -U --no-cache \ build-base \ ca-certificates \ diff --git a/TODO b/TODO deleted file mode 100644 index cc28b88e..00000000 --- a/TODO +++ /dev/null @@ -1,7 +0,0 @@ -- gorm v2 - - preload joins - - insert or ignore - - bulk inserts (eg. track_genres, album_genres, playlist stuff?) -- db package with queries encapsulated -- add newlines -- scanner tests and refactor diff --git a/_do_build_server b/_do_build_server deleted file mode 100755 index 3cf8d07c..00000000 --- a/_do_build_server +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# using sh, is run from alpine - -go build -o gonic cmd/gonic/main.go diff --git a/_do_bump_version b/_do_bump_version deleted file mode 100755 index 4771a613..00000000 --- a/_do_bump_version +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env bash - -# safety checks -if [[ $# -ne 1 ]]; then - echo "usage: $0 " >&2 - exit 1 -fi -if [[ "$(git rev-parse --abbrev-ref HEAD)" != 'master' ]]; then - echo "not on the master branch" >&2 - exit 1 -fi - -# get latest and check if clean -git pull -if [[ -n "$(git status --porcelain)" ]]; then - echo "working directory is dirty" >&2 - exit 1 -fi - -# get the current verison from last git tag into array and -# inc the provided part -semver_expression='s/^v([0-9]+)\.([0-9]+)\.([0-9]+).*$/\1 \2 \3/' -version=( $(git describe --tags | sed -E -e "$semver_expression" ) ) -case "$1" in - major) - ((version[0]++)) - version[1]=0 - version[2]=0 - ;; - minor) - ((version[1]++)) - version[2]=0 - ;; - patch) - ((version[2]++)) - ;; - *) - echo 'please provide a valid version in increment' >&2 - exit 1 -esac -new_version="v${version[0]}.${version[1]}.${version[2]}" - -# write version to go -mkdir version >/dev/null 2>&1 -cat > version/version.go << EOL -// generated by \`_do_bump_version\` script in project root -// $(date) -// DO NOT EDIT - -package version - -const NAME = "gonic" -const NAME_UPPER = "GONIC" -const NAME_EMBED = "gonicembed" -const VERSION = "$new_version" -EOL - -./_do_gen_handler_tests - -# create and tag single commit with a change to the version file -git commit --all --file - << EOL -bump to $new_version - -generated by \`_do_bump_version\` script in project root -EOL -git tag "$new_version" - -echo "commited and tagged" -echo "remember to push tags" diff --git a/_do_gen_assets b/_do_gen_assets deleted file mode 100755 index b6173869..00000000 --- a/_do_gen_assets +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -# using sh, is run from alpine - -embed_bin_path=/tmp/gonicembed -assets_path=server/assets - -# only build once for some slightly faster live page reloading -test ! -e "$embed_bin_path" && go build \ - -o "$embed_bin_path" \ - cmd/gonicembed/main.go - -find $assets_path/ \ - -type f \ - ! -name '*.go' \ - -exec "$embed_bin_path" \ - -out-path $assets_path/assets.gen.go \ - -package-name assets \ - -assets-var-name Bytes \ - -asset-path-prefix $assets_path/ \ - {} + diff --git a/_do_run_server b/_do_run_server deleted file mode 100755 index c9a435d4..00000000 --- a/_do_run_server +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -./_do_gen_assets -go run cmd/gonic/main.go $@ diff --git a/_do_run_server_live b/_do_run_server_live deleted file mode 100755 index 728b63c0..00000000 --- a/_do_run_server_live +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -if ! command -v 'entr' > /dev/null; then - echo 'please install `entr`' >&2 - echo 'http://eradman.com/entrproject/' >&2 - exit 1 -fi - -find server/assets/ -not -name '*_gen.go' | entr -r ./_do_run_server $@ diff --git a/cmd/gonic/main.go b/cmd/gonic/gonic.go similarity index 92% rename from cmd/gonic/main.go rename to cmd/gonic/gonic.go index 4df174cd..97920e30 100644 --- a/cmd/gonic/main.go +++ b/cmd/gonic/gonic.go @@ -15,9 +15,9 @@ import ( "github.com/oklog/run" "github.com/peterbourgon/ff" + "go.senan.xyz/gonic" "go.senan.xyz/gonic/server" "go.senan.xyz/gonic/server/db" - "go.senan.xyz/gonic/version" ) const ( @@ -27,7 +27,7 @@ const ( ) func main() { - set := flag.NewFlagSet(version.NAME, flag.ExitOnError) + set := flag.NewFlagSet(gonic.Version, flag.ExitOnError) confListenAddr := set.String("listen-addr", "0.0.0.0:4747", "listen address (optional)") confMusicPath := set.String("music-path", "", "path to music") confPodcastPath := set.String("podcast-path", "", "path to podcasts") @@ -44,17 +44,17 @@ func main() { if err := ff.Parse(set, os.Args[1:], ff.WithConfigFileFlag("config-path"), ff.WithConfigFileParser(ff.PlainParser), - ff.WithEnvVarPrefix(version.NAME_UPPER), + ff.WithEnvVarPrefix(gonic.NameUpper), ); err != nil { log.Fatalf("error parsing args: %v\n", err) } if *confShowVersion { - fmt.Println(version.VERSION) + fmt.Println(gonic.Version) os.Exit(0) } - log.Printf("starting gonic %s\n", version.VERSION) + log.Printf("starting gonic %s\n", gonic.Version) log.Printf("provided config\n") set.VisitAll(func(f *flag.Flag) { log.Printf(" %-15s %s\n", f.Name, f.Value) @@ -92,7 +92,7 @@ func main() { proxyPrefixExpr := regexp.MustCompile(`^\/*(.*?)\/*$`) *confProxyPrefix = proxyPrefixExpr.ReplaceAllString(*confProxyPrefix, `/$1`) - server := server.New(server.Options{ + server, err := server.New(server.Options{ DB: db, MusicPath: *confMusicPath, CachePath: cacheDirAudio, @@ -103,6 +103,9 @@ func main() { HTTPLog: *confHTTPLog, JukeboxEnabled: *confJukeboxEnabled, }) + if err != nil { + log.Fatalf("error creating server: %v\n", err) + } var g run.Group g.Add(server.StartHTTP(*confListenAddr)) diff --git a/cmd/gonicembed/main.go b/cmd/gonicembed/main.go deleted file mode 100644 index 440371b4..00000000 --- a/cmd/gonicembed/main.go +++ /dev/null @@ -1,156 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io" - "log" - "os" - "path/filepath" - "strings" - - "github.com/peterbourgon/ff" - - "go.senan.xyz/gonic/version" -) - -type errWriter struct { - w io.Writer - err error -} - -func (ew *errWriter) write(buf []byte) { - if ew.err != nil { - return - } - _, ew.err = ew.w.Write(buf) -} - -func (ew *errWriter) printf(format string, a ...interface{}) { - if ew.err != nil { - return - } - _, ew.err = fmt.Fprintf(ew.w, format, a...) -} - -func (ew *errWriter) println(a ...interface{}) { - if ew.err != nil { - return - } - _, ew.err = fmt.Fprintln(ew.w, a...) -} - -// once i had this written with ~100% text/template but it was very -// slow. now this thing is not nice on the eyes or easy to change -// but it's pretty fast. which i needed it to for live reloading stuff - -const ( - byteCols = 24 - // ** begin file template - fileHeader = `// file generated with embed tool; DO NOT EDIT. -// %s -package %s -import "time" -type EmbeddedAsset struct { - ModTime time.Time - Bytes []byte -} -var %s = map[string]*EmbeddedAsset{` - fileFooter = ` -}` - // ** begin asset template - assetHeader = ` -%q: &EmbeddedAsset{ - ModTime: time.Unix(%d, 0), - Bytes: []byte{ -` - assetFooter = `}},` -) - -type config struct { - packageName string - outPath string - tagList string - assetsVarName string - assetPathPrefix string -} - -func processAsset(c *config, ew *errWriter, path string) error { - info, err := os.Stat(path) - if err != nil { - return fmt.Errorf("stating asset: %w", err) - } - if info.IsDir() { - return nil - } - data, err := os.Open(filepath.Clean(path)) - if err != nil { - return fmt.Errorf("opening asset: %w", err) - } - defer data.Close() - ew.write([]byte(fmt.Sprintf(assetHeader, - strings.TrimPrefix(path, c.assetPathPrefix), - info.ModTime().Unix(), - ))) - buffer := make([]byte, byteCols) - for { - read, err := data.Read(buffer) - for i := 0; i < read; i++ { - ew.printf("0x%02x,", buffer[i]) - } - if err != nil { - break - } - ew.println() - } - ew.write([]byte(assetFooter)) - return ew.err -} - -func processAssets(c *config, files []string) error { - out, err := os.Create(c.outPath) - if err != nil { - return fmt.Errorf("creating out path: %w", err) - } - ew := &errWriter{w: out} - if c.tagList != "" { - c.tagList = fmt.Sprintf("+build %s", c.tagList) - } - ew.write([]byte(fmt.Sprintf(fileHeader, - c.tagList, - c.packageName, - c.assetsVarName, - ))) - defer ew.write([]byte(fileFooter)) - for _, path := range files { - if err := processAsset(c, ew, path); err != nil { - return fmt.Errorf("processing asset: %w", err) - } - } - return ew.err -} - -func main() { - set := flag.NewFlagSet(version.NAME_EMBED, flag.ExitOnError) - outPath := set.String("out-path", "", "generated file's path (required)") - pkgName := set.String("package-name", "assets", "generated file's package name") - tagList := set.String("tag-list", "", "generated file's build tag list") - assetsVarName := set.String("assets-var-name", "Assets", "generated assets var name") - assetPathPrefix := set.String("asset-path-prefix", "", "generated assets map key prefix") - if err := ff.Parse(set, os.Args[1:]); err != nil { - log.Fatalf("error parsing args: %v\n", err) - } - if *outPath == "" { - log.Fatalln("invalid arguments. see -h") - } - c := &config{ - packageName: *pkgName, - outPath: *outPath, - tagList: *tagList, - assetsVarName: *assetsVarName, - assetPathPrefix: *assetPathPrefix, - } - if err := processAssets(c, set.Args()); err != nil { - log.Fatalf("error processing files: %v\n", err) - } -} diff --git a/_do_gen_handler_tests b/gen_handler_tests similarity index 99% rename from _do_gen_handler_tests rename to gen_handler_tests index 8b8c6e5d..a2951383 100755 --- a/_do_gen_handler_tests +++ b/gen_handler_tests @@ -10,7 +10,7 @@ test_cache_path="$(mktemp -d)" mkdir "$test_music_path" 2>/dev/null echo "waiting for server to start" -./_do_run_server \ +go run cmd/gonic/gonic.go \ -music-path "$test_music_path" \ -podcast-path "$test_podcast_path" \ -cache-path "$test_cache_path" \ diff --git a/go.mod b/go.mod index 9427ea5e..f336c448 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module go.senan.xyz/gonic +go 1.16 + require ( github.com/Masterminds/goutils v1.1.0 // indirect github.com/Masterminds/semver v1.5.0 // indirect @@ -39,5 +41,3 @@ require ( gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/gormigrate.v1 v1.6.0 ) - -go 1.15 diff --git a/version.go b/version.go new file mode 100644 index 00000000..423f8b18 --- /dev/null +++ b/version.go @@ -0,0 +1,9 @@ +//nolint:gochecknoglobals,golint,stylecheck +package gonic + +import ( + _ "embed" +) + +//go:embed version.txt +var Version string diff --git a/version.txt b/version.txt new file mode 100644 index 00000000..26acbf08 --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.12.2 diff --git a/version/version.go b/version/version.go deleted file mode 100644 index 3556eee5..00000000 --- a/version/version.go +++ /dev/null @@ -1,10 +0,0 @@ -// generated by `_do_bump_version` script in project root -// Tue 20 Apr 2021 23:16:59 IST -// DO NOT EDIT - -package version - -const NAME = "gonic" -const NAME_UPPER = "GONIC" -const NAME_EMBED = "gonicembed" -const VERSION = "v0.12.3"