Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements #33

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- checkout
- docker/build:
image: pboehm/ddns
dockerfile: docker/ddns/Dockerfile
dockerfile: Dockerfile

docker-build-and-push:
executor: docker/docker
Expand All @@ -28,7 +28,7 @@ jobs:
- docker/check
- docker/build:
image: pboehm/ddns
dockerfile: docker/ddns/Dockerfile
dockerfile: Dockerfile
tag: $CIRCLE_SHA1,latest
- docker/push:
image: pboehm/ddns
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/docker/docker-compose.*.yml
/ddns
dump.rdb
index.html
/docker/.caddy_mount/
/docker/.redis_mount/
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:alpine AS builder

RUN apk add --no-cache git

WORKDIR /go/src/github.com/pboehm/ddns
COPY . .

RUN GO111MODULE=on go get -d -v ./...
RUN export CGO_ENABLED=0 && GO111MODULE=on go install -v ./...

ENV GIN_MODE release

FROM scratch

COPY --from=builder /go/bin/ddns /go/bin/ddns

ENTRYPOINT ["/go/bin/ddns"]
20 changes: 15 additions & 5 deletions backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package backend

import (
"github.com/Depado/ginprom"
"github.com/gin-gonic/gin"
"github.com/pboehm/ddns/shared"
"github.com/prometheus/client_golang/prometheus"
"log"
"strings"
)

type Backend struct {
config *shared.Config
lookup *HostLookup
config *shared.Config
lookup *HostLookup
registry *prometheus.Registry
}

func NewBackend(config *shared.Config, lookup *HostLookup) *Backend {
func NewBackend(config *shared.Config, lookup *HostLookup, registry *prometheus.Registry) *Backend {
return &Backend{
config: config,
lookup: lookup,
config: config,
lookup: lookup,
registry: registry,
}
}

func (b *Backend) Run() error {
prom := ginprom.New(ginprom.Namespace("ddns"),
ginprom.Subsystem("backend"), ginprom.Registry(b.registry))

r := gin.New()
r.Use(prom.Instrument())
prom.Use(r)

r.Use(gin.Recovery())

if b.config.Verbose {
Expand Down
11 changes: 9 additions & 2 deletions ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/pboehm/ddns/backend"
"github.com/pboehm/ddns/frontend"
"github.com/pboehm/ddns/shared"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
"log"
)
Expand All @@ -17,18 +18,24 @@ func init() {
func main() {
serviceConfig.Validate()

if serviceConfig.Verbose {
log.Printf("Loaded config: %#v", serviceConfig)
}

redis := shared.NewRedisBackend(serviceConfig)
defer redis.Close()

registry := prometheus.NewRegistry()

var group errgroup.Group

group.Go(func() error {
lookup := backend.NewHostLookup(serviceConfig, redis)
return backend.NewBackend(serviceConfig, lookup).Run()
return backend.NewBackend(serviceConfig, lookup, registry).Run()
})

group.Go(func() error {
return frontend.NewFrontend(serviceConfig, redis).Run()
return frontend.NewFrontend(serviceConfig, redis, registry).Run()
})

if err := group.Wait(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docker/ddns/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ ENV DDNS_EXPIRATION_DAYS 10
CMD /go/bin/ddns \
--domain=${DDNS_DOMAIN} \
--soa_fqdn=${DDNS_SOA_DOMAIN} \
--redis=${DDNS_REDIS_HOST} \
--redis-host=${DDNS_REDIS_HOST} \
--expiration-days=${DDNS_EXPIRATION_DAYS}
36 changes: 27 additions & 9 deletions frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package frontend

import (
"fmt"
"github.com/Depado/ginprom"
"github.com/gin-gonic/gin"
"github.com/pboehm/ddns/shared"
"github.com/prometheus/client_golang/prometheus"
"html/template"
"log"
"net"
Expand All @@ -12,26 +14,34 @@ import (
)

type Frontend struct {
config *shared.Config
hosts shared.HostBackend
config *shared.Config
hosts shared.HostBackend
registry *prometheus.Registry
}

func NewFrontend(config *shared.Config, hosts shared.HostBackend) *Frontend {
func NewFrontend(config *shared.Config, hosts shared.HostBackend, registry *prometheus.Registry) *Frontend {
return &Frontend{
config: config,
hosts: hosts,
config: config,
hosts: hosts,
registry: registry,
}
}

func (f *Frontend) Run() error {
prom := ginprom.New(ginprom.Namespace("ddns"),
ginprom.Subsystem("frontend"), ginprom.Registry(f.registry))

r := gin.New()
r.Use(prom.Instrument())
prom.Engine = r // we don't want to expose the metrics on the frontend, so we are not using `prom.Use(r)`

r.Use(gin.Recovery())

if f.config.Verbose {
r.Use(gin.Logger())
}

r.SetHTMLTemplate(buildTemplate())
r.SetHTMLTemplate(buildTemplate(f.config.CustomTemplatePath))

r.GET("/", func(g *gin.Context) {
g.HTML(200, "index.html", gin.H{"domain": f.config.Domain})
Expand Down Expand Up @@ -143,10 +153,18 @@ func extractRemoteAddr(req *http.Request) (string, error) {
}

// Get index template from bindata
func buildTemplate() *template.Template {
html, err := template.New("index.html").Parse(indexTemplate)
func buildTemplate(customTemplatePath string) *template.Template {
var html *template.Template
var err error

if customTemplatePath != "" {
html, err = template.ParseFiles(customTemplatePath)
} else {
html, err = template.New("index.html").Parse(indexTemplate)
}

if err != nil {
log.Fatal(err)
log.Fatalf("Error parsing frontend template: %v", err)
}

return html
Expand Down
24 changes: 20 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ module github.com/pboehm/ddns
go 1.12

require (
github.com/garyburd/redigo v1.6.0
github.com/gin-gonic/gin v1.4.0
github.com/stretchr/testify v1.3.0
golang.org/x/sync v0.0.0-20190423024810-112230192c58
github.com/Depado/ginprom v1.5.0
github.com/garyburd/redigo v1.6.2
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/jnovack/flag v1.15.0
github.com/json-iterator/go v1.1.10 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/common v0.17.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/stretchr/testify v1.6.1
github.com/ugorji/go v1.2.4 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading