Skip to content

Commit

Permalink
Go: Update golang version to 1.16 (#53)
Browse files Browse the repository at this point in the history
* bump go version to 1.16

* switch from embedfiles to go embed native package

* build long-season binaries for darwin/arm64

* update Dockerfile
  • Loading branch information
thinkofher authored May 14, 2021
2 parents 9a1b281 + c559fea commit ad963dd
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 103 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.15", "1.14", "1.13"]
go: ["1.16"]

steps:

Expand All @@ -33,11 +33,6 @@ jobs:
run: |
if [ $(gofmt -l -s . | wc -l) -ne 0 ]; then echo "gofmt failed"; exit 1; fi
- name: Embedding Files
run: |
env GO111MODULE=off go get 4d63.com/embedfiles
embedfiles -out=pkg/static/files.gen.go -pkg=static web
- name: Build
run: |
go build -v ./cmd/server/main.go
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.16

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
9 changes: 0 additions & 9 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ env:
- GO111MODULE=on
- CGO_ENABLED=0

before:
hooks:
- env GO111MODULE=off go get 4d63.com/embedfiles
- embedfiles -out=pkg/static/files.gen.go -pkg=static web

builds:
- main: ./cmd/cli/main.go
id: "cli"
Expand All @@ -27,8 +22,6 @@ builds:
goarch: arm
- goos: windows
goarch: arm64
- goos: darwin
goarch: arm64
- goos: darwin
goarch: arm

Expand All @@ -51,7 +44,5 @@ builds:
goarch: arm
- goos: windows
goarch: arm64
- goos: darwin
goarch: arm64
- goos: darwin
goarch: arm
11 changes: 2 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
FROM golang:1.15-alpine
FROM golang:1.16-alpine

WORKDIR $GOPATH/bin

RUN apk add --no-cache git \
&& export GO111MODULE=off \
&& go get 4d63.com/embedfiles \
&& export GO111MODULE=on

RUN apk add --no-cache git

WORKDIR /opt/db
ENV LS_BOLT_DB /opt/db/bolt.db
Expand All @@ -16,8 +12,6 @@ WORKDIR $SRC
COPY . .

RUN go get -d -v ./...
RUN embedfiles -out=pkg/static/files.gen.go -pkg=static web


RUN go build -o "long-season" ./cmd/server/main.go \
&& mv "long-season" $GOPATH/bin/
Expand All @@ -26,4 +20,3 @@ RUN go build -o "long-season-cli" ./cmd/cli/main.go \
&& mv "long-season-cli" $GOPATH/bin/

WORKDIR /
RUN rm -rf $SRC $GOPATH/bin/embedfiles
20 changes: 2 additions & 18 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ tasks:
default:
deps: [server, cli]

tools:
cmds:
- env GO111MODULE=off go get 4d63.com/embedfiles

server:
deps: [gen]
cmds:
- go build -o {{ .NAME }} cmd/server/main.go
sources:
- ./**/*.go
- ./**/*.html
- ./**/*.js
- ./**/*.css
generates:
- ./{{ .NAME }}

Expand All @@ -34,27 +31,14 @@ tasks:
cmds:
- ./{{ .NAME }}

gen:
deps: [tools]
cmds:
- embedfiles -out=pkg/static/files.gen.go -pkg=static web
sources:
- ./**/*.html
- ./**/*.css
- ./**/*.js
generates:
- ./pkg/static/files.gen.go

clean:
cmds:
- rm -f ./{{ .NAME }} ./{{ .NAME_CLI }}
- rm -f ./pkg/static/files.gen.go

lint:
cmds:
- golint ./...

test:
deps: [gen]
cmds:
- go test ./...
21 changes: 16 additions & 5 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/hakierspejs/long-season/pkg/services/status"
"github.com/hakierspejs/long-season/pkg/services/ui"
"github.com/hakierspejs/long-season/pkg/storage/memory"
"github.com/hakierspejs/long-season/web"
)

func main() {
Expand Down Expand Up @@ -52,14 +54,23 @@ func main() {
publicCors.Log = log.New(os.Stdout, "CORS ", 0)
}

var opener handlers.Opener
if config.Debug {
opener = func(path string) ([]byte, error) {
return ioutil.ReadFile("web/" + path)
}
} else {
opener = web.Open
}

r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.RequestID)
r.Use(middleware.NoCache)

r.Get("/", ui.Home(config))
r.With(lsmiddleware.ApiAuth(*config, true), lsmiddleware.RedirectLoggedIn).Get("/login", ui.LoginPage(config))
r.Get("/", ui.Home(opener))
r.With(lsmiddleware.ApiAuth(*config, true), lsmiddleware.RedirectLoggedIn).Get("/login", ui.LoginPage(opener))

r.Route("/api/v1", func(r chi.Router) {
r.Route("/users", func(r chi.Router) {
Expand Down Expand Up @@ -112,11 +123,11 @@ func main() {
})

r.With(lsmiddleware.ApiAuth(*config, false)).Get("/who", handlers.Who())
r.With(lsmiddleware.ApiAuth(*config, false)).Get("/devices", ui.Devices(config))
r.With(lsmiddleware.ApiAuth(*config, false)).Get("/devices", ui.Devices(opener))
r.Get("/logout", ui.Logout())
r.With(lsmiddleware.ApiAuth(*config, true), lsmiddleware.RedirectLoggedIn).Get("/register", ui.Register(config))
r.With(lsmiddleware.ApiAuth(*config, true), lsmiddleware.RedirectLoggedIn).Get("/register", ui.Register(opener))

handlers.FileServer(r, "/static", config)
handlers.FileServer(r, "/static", opener)

// start daemon for updating mac addresses
go macDeamon()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hakierspejs/long-season

go 1.15
go 1.16

require (
github.com/alioygur/gores v1.2.1
Expand Down
16 changes: 0 additions & 16 deletions pkg/services/config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package config

import (
"io/ioutil"
"os"
"strings"

"github.com/hakierspejs/long-season/pkg/models"
"github.com/hakierspejs/long-season/pkg/static"
)

// JWTUserKey is key for JWT claims stored in request context.
Expand Down Expand Up @@ -61,20 +59,6 @@ func DefaultEnv(key, fallback string) string {
return res
}

// Opener is generic function for reading files.
// For example: Opener can open files from filesystem or
// files embedded withing binary. Given string could be a
// path or other indicator.
type Opener func(string) ([]byte, error)

// MakeOpener returns opener depending on given config.
func MakeOpener(c *models.Config) Opener {
if c.Debug {
return ioutil.ReadFile
}
return static.Open
}

func parseBoolEnv(env string) bool {
return !(env == "" || env == "0" || strings.ToLower(env) == "false")
}
17 changes: 9 additions & 8 deletions pkg/services/handlers/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"strings"

"github.com/go-chi/chi"

"github.com/hakierspejs/long-season/pkg/models"
"github.com/hakierspejs/long-season/pkg/services/config"
)

// FileServer sets up handler to serve static files within given URL path.
func FileServer(r chi.Router, path string, c *models.Config) {
opener := config.MakeOpener(c)
// Opener is generic function for reading files.
// For example: Opener can open files from filesystem or
// files embedded withing binary. Given string could be a
// path or other indicator.
type Opener func(string) ([]byte, error)

// FileServer sets up handler to serve static files within given URL path.
func FileServer(r chi.Router, path string, opener Opener) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
Expand All @@ -32,10 +33,10 @@ func FileServer(r chi.Router, path string, c *models.Config) {
}

if strings.HasSuffix(filepath, ".js") {
w.Header().Add("Content-Type", "text/js")
w.Header().Add("Content-Type", "application/javascript")
}

file, err := opener("web/" + filepath)
file, err := opener(filepath)
if err != nil {
http.NotFound(w, r)
return
Expand Down
27 changes: 13 additions & 14 deletions pkg/services/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"strings"
"time"

"github.com/hakierspejs/long-season/pkg/models"
"github.com/hakierspejs/long-season/pkg/services/config"
"github.com/hakierspejs/long-season/pkg/services/handlers"
)

func renderWithOpener(path string, readFunc config.Opener) (*template.Template, error) {
func renderWithOpener(path string, readFunc handlers.Opener) (*template.Template, error) {
str := new(strings.Builder)

b, err := readFunc("web/tmpl/layout.html")
b, err := readFunc("tmpl/layout.html")
if err != nil {
return nil, err
}
Expand All @@ -34,26 +33,26 @@ func renderWithOpener(path string, readFunc config.Opener) (*template.Template,
return template.New("ui").Parse(str.String())
}

func renderTemplate(c *models.Config, path string) (*template.Template, error) {
return renderWithOpener(path, config.MakeOpener(c))
func renderTemplate(opener handlers.Opener, path string) (*template.Template, error) {
return renderWithOpener(path, opener)
}

func Home(c *models.Config) http.HandlerFunc {
tmpl := template.Must(renderTemplate(c, "web/tmpl/home.html"))
func Home(opener handlers.Opener) http.HandlerFunc {
tmpl := template.Must(renderTemplate(opener, "tmpl/home.html"))
return func(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "layout", nil)
}
}

func LoginPage(c *models.Config) http.HandlerFunc {
tmpl := template.Must(renderTemplate(c, "web/tmpl/login.html"))
func LoginPage(opener handlers.Opener) http.HandlerFunc {
tmpl := template.Must(renderTemplate(opener, "tmpl/login.html"))
return func(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "layout", nil)
}
}

func Register(c *models.Config) http.HandlerFunc {
tmpl := template.Must(renderTemplate(c, "web/tmpl/register.html"))
func Register(opener handlers.Opener) http.HandlerFunc {
tmpl := template.Must(renderTemplate(opener, "tmpl/register.html"))
return func(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "layout", nil)
}
Expand All @@ -73,8 +72,8 @@ func Logout() http.HandlerFunc {
}
}

func Devices(c *models.Config) http.HandlerFunc {
tmpl := template.Must(renderTemplate(c, "web/tmpl/devices.html"))
func Devices(opener handlers.Opener) http.HandlerFunc {
tmpl := template.Must(renderTemplate(opener, "tmpl/devices.html"))
return func(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "layout", nil)
}
Expand Down
16 changes: 0 additions & 16 deletions pkg/static/static.go

This file was deleted.

15 changes: 15 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package web

import (
"embed"
)

//go:embed static/*
//go:embed tmpl/*
var content embed.FS

// Open opens file with given path, that is stored in
// long-season binary.
func Open(path string) ([]byte, error) {
return content.ReadFile(path)
}

0 comments on commit ad963dd

Please sign in to comment.