Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Update go to 1.19 and use go embed for static assets
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenWeathers committed Mar 7, 2023
1 parent a2bfc10 commit 5fad201
Show file tree
Hide file tree
Showing 9 changed files with 14,298 additions and 71 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Go parameters
GOCMD=go
STATICPACKCMD=pkger
NPMCMD=npm
NPMBUILD=$(NPMCMD) run build
GOBUILD=$(GOCMD) build
Expand All @@ -18,11 +17,9 @@ DOCKER_BUILD_VERSION=--build-arg BUILD_VERSION=${VERSION_TAG}
all: build
build-deps:
$(NPMBUILD)
$(STATICPACKCMD)

build:
$(NPMBUILD)
$(STATICPACKCMD)
$(GOBUILD) -o $(BINARY_NAME) -v

clean:
Expand Down Expand Up @@ -52,12 +49,10 @@ build-windows:

dev:
$(NPMBUILD)
$(STATICPACKCMD)
$(GOBUILD) -o $(BINARY_NAME) -v

./$(BINARY_NAME)
dev-go:
$(STATICPACKCMD)
dev-go:
$(GOBUILD) -o $(BINARY_NAME) -v

./$(BINARY_NAME)
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ docker run --publish 8080:8080 --name gddu gddu:latest
### Install dependencies
```
go get
go go install github.com/markbates/pkger/cmd/pkger
npm install
```

Expand All @@ -107,11 +106,6 @@ make build
npm run build
```

### bundle up static assets
```
pkger
```

### Build for current OS
```
go build
Expand Down
7 changes: 2 additions & 5 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
############################
# STEP 1 build the ui
############################
FROM node:12-alpine as builderNode
FROM node:18-alpine as builderNode

RUN mkdir /webapp
COPY ./web/ /webapp/web/
Expand All @@ -17,7 +17,7 @@ RUN npm run build
############################
# STEP 2 build executable binary
############################
FROM golang:1.13-alpine as builderGo
FROM golang:1.19-alpine as builderGo
# Install git + SSL ca certificates.
# Git is required for fetching the dependencies.
# Ca-certificates is required to call HTTPS endpoints.
Expand All @@ -35,10 +35,7 @@ COPY --from=builderNode /webapp/dist $GOPATH/src/mypackage/myapp/dist
# Set working dir
WORKDIR $GOPATH/src/mypackage/myapp/
# Fetch dependencies.
RUN go install -v github.com/markbates/pkger/cmd/pkger
RUN go mod download
# Bundle the static assets
RUN pkger
# Build the binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-w -s" -ldflags "-X main.version=$BUILD_VERSION" -o /go/bin/gddu
############################
Expand Down
23 changes: 20 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
module github.com/StevenWeathers/google-domains-ddns-updater

go 1.13
go 1.19

require (
github.com/gorilla/mux v1.7.4
github.com/jpillora/backoff v1.0.0 // indirect
github.com/markbates/pkger v0.15.0
github.com/rdegges/go-ipify v0.0.0-20150526035502-2d94a6a86c40
github.com/robfig/cron v1.2.0
github.com/spf13/viper v1.7.1
)

require (
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.4.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -115,8 +113,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/markbates/pkger v0.15.0 h1:rSXoKLBWBgYG7j/h6Be7kggju23ie1Gx3/va9xl5aUw=
github.com/markbates/pkger v0.15.0/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down
52 changes: 30 additions & 22 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,39 @@ import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"io/fs"
"log"
"net/http"

"github.com/gorilla/mux"
"github.com/markbates/pkger"
"github.com/spf13/viper"
)

// get the index template from embedded filesystem
func (s *server) getIndexTemplate(FSS fs.FS) *template.Template {
// get the html template from dist, have it ready for requests
tmplContent, ioErr := fs.ReadFile(FSS, "index.html")
if ioErr != nil {
log.Println("Error opening index template")
if !embedUseOS {
log.Fatal(ioErr.Error())
}
}

tmplString := string(tmplContent)
tmpl, tmplErr := template.New("index").Parse(tmplString)
if tmplErr != nil {
log.Println("Error parsing index template")
if !embedUseOS {
log.Fatal(tmplErr.Error())
}
}

return tmpl
}

// handleIndex parses the index html file, injecting any relevant data
func (s *server) handleIndex() http.HandlerFunc {
func (s *server) handleIndex(FSS fs.FS) http.HandlerFunc {
type AppConfig struct {
ToastTimeout int
DefaultLocale string
Expand All @@ -25,25 +47,7 @@ func (s *server) handleIndex() http.HandlerFunc {
AppConfig AppConfig
}

// get the html template from dist, have it ready for requests
indexFile, ioErr := pkger.Open("/dist/index.html")
if ioErr != nil {
log.Println("Error opening index template")
log.Fatal(ioErr)
}
tmplContent, ioReadErr := ioutil.ReadAll(indexFile)
if ioReadErr != nil {
// this will hopefully only possibly panic during development as the file is already in memory otherwise
log.Println("Error reading index template")
log.Fatal(ioReadErr)
}

tmplString := string(tmplContent)
tmpl, tmplErr := template.New("index").Parse(tmplString)
if tmplErr != nil {
log.Println("Error parsing index template")
log.Fatal(tmplErr)
}
tmpl := s.getIndexTemplate(FSS)

appConfig := AppConfig{
ToastTimeout: viper.GetInt("config.toast_timeout"),
Expand All @@ -57,6 +61,10 @@ func (s *server) handleIndex() http.HandlerFunc {
}

return func(w http.ResponseWriter, r *http.Request) {
if embedUseOS {
tmpl = s.getIndexTemplate(FSS)
}

tmpl.Execute(w, data)
}
}
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
Expand All @@ -15,7 +16,8 @@ import (
)

var (
version = "dev"
version = "dev"
embedUseOS = true
)

// ServerConfig holds server global config values
Expand Down Expand Up @@ -85,7 +87,7 @@ func attemptIPAddressUpdates() {
// respondWithJSON(w, code, map[string]string{"error": message})
// }

// responsdWithJSON takes a payload and writes the response
// respondWithJSON takes a payload and writes the response
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)

Expand All @@ -97,6 +99,8 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
func main() {
log.Println("Google Domains DDNS Updater version " + version)

embedUseOS = len(os.Args) > 1 && os.Args[1] == "live"

InitConfig()

pathPrefix := viper.GetString("http.path_prefix")
Expand Down
Loading

0 comments on commit 5fad201

Please sign in to comment.