restructure code into 3 subpackages for backend frontend and shared

This commit is contained in:
Philipp Böhm 2017-12-03 22:42:25 +01:00
parent 0acc5bb1e3
commit 887600f964
8 changed files with 28 additions and 33 deletions

View File

@ -3,8 +3,7 @@ package backend
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/pboehm/ddns/config" "github.com/pboehm/ddns/shared"
"github.com/pboehm/ddns/hosts"
"strings" "strings"
"time" "time"
) )
@ -26,11 +25,11 @@ type Response struct {
} }
type HostLookup struct { type HostLookup struct {
config *config.Config config *shared.Config
hosts hosts.HostBackend hosts shared.HostBackend
} }
func NewHostLookup(config *config.Config, hostsBackend hosts.HostBackend) *HostLookup { func NewHostLookup(config *shared.Config, hostsBackend shared.HostBackend) *HostLookup {
return &HostLookup{config, hostsBackend} return &HostLookup{config, hostsBackend}
} }
@ -52,7 +51,7 @@ func (l *HostLookup) Lookup(request *Request) (*Response, error) {
return nil, err return nil, err
} }
var host *hosts.Host var host *shared.Host
if host, err = l.hosts.GetHost(hostname); err != nil { if host, err = l.hosts.GetHost(hostname); err != nil {
return nil, err return nil, err
} }

View File

@ -2,17 +2,16 @@ package backend
import ( import (
"errors" "errors"
c "github.com/pboehm/ddns/config" "github.com/pboehm/ddns/shared"
h "github.com/pboehm/ddns/hosts"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
type testHostBackend struct { type testHostBackend struct {
hosts map[string]*h.Host hosts map[string]*shared.Host
} }
func (b *testHostBackend) GetHost(hostname string) (*h.Host, error) { func (b *testHostBackend) GetHost(hostname string) (*shared.Host, error) {
host, ok := b.hosts[hostname] host, ok := b.hosts[hostname]
if ok { if ok {
return host, nil return host, nil
@ -21,20 +20,20 @@ func (b *testHostBackend) GetHost(hostname string) (*h.Host, error) {
} }
} }
func (b *testHostBackend) SetHost(host *h.Host) error { func (b *testHostBackend) SetHost(host *shared.Host) error {
b.hosts[host.Hostname] = host b.hosts[host.Hostname] = host
return nil return nil
} }
func buildLookup(domain string) (*c.Config, *testHostBackend, *HostLookup) { func buildLookup(domain string) (*shared.Config, *testHostBackend, *HostLookup) {
config := &c.Config{ config := &shared.Config{
Verbose: false, Verbose: false,
Domain: domain, Domain: domain,
SOAFqdn: "dns" + domain, SOAFqdn: "dns" + domain,
} }
hosts := &testHostBackend{ hosts := &testHostBackend{
hosts: map[string]*h.Host{ hosts: map[string]*shared.Host{
"www": { "www": {
Hostname: "www", Hostname: "www",
Ip: "10.11.12.13", Ip: "10.11.12.13",

11
ddns.go
View File

@ -3,14 +3,13 @@ package main
import ( import (
"flag" "flag"
"github.com/pboehm/ddns/backend" "github.com/pboehm/ddns/backend"
"github.com/pboehm/ddns/config" "github.com/pboehm/ddns/frontend"
"github.com/pboehm/ddns/hosts" "github.com/pboehm/ddns/shared"
"github.com/pboehm/ddns/web"
"log" "log"
"strings" "strings"
) )
var serviceConfig *config.Config = &config.Config{} var serviceConfig *shared.Config = &shared.Config{}
func init() { func init() {
flag.StringVar(&serviceConfig.Domain, "domain", "", flag.StringVar(&serviceConfig.Domain, "domain", "",
@ -49,10 +48,10 @@ func main() {
flag.Parse() flag.Parse()
validateCommandArgs() validateCommandArgs()
redis := hosts.NewRedisBackend(serviceConfig) redis := shared.NewRedisBackend(serviceConfig)
defer redis.Close() defer redis.Close()
lookup := backend.NewHostLookup(serviceConfig, redis) lookup := backend.NewHostLookup(serviceConfig, redis)
web.NewWebService(serviceConfig, redis, lookup).Run() frontend.NewWebService(serviceConfig, redis, lookup).Run()
} }

View File

@ -1,4 +1,4 @@
package web package frontend
const indexTemplate string = ` const indexTemplate string = `
<!DOCTYPE html> <!DOCTYPE html>

View File

@ -1,10 +1,9 @@
package web package frontend
import ( import (
"fmt" "fmt"
"github.com/pboehm/ddns/backend" "github.com/pboehm/ddns/backend"
"github.com/pboehm/ddns/config" "github.com/pboehm/ddns/shared"
"github.com/pboehm/ddns/hosts"
"gopkg.in/gin-gonic/gin.v1" "gopkg.in/gin-gonic/gin.v1"
"html/template" "html/template"
"log" "log"
@ -15,12 +14,12 @@ import (
) )
type WebService struct { type WebService struct {
config *config.Config config *shared.Config
hosts hosts.HostBackend hosts shared.HostBackend
lookup *backend.HostLookup lookup *backend.HostLookup
} }
func NewWebService(config *config.Config, hosts hosts.HostBackend, lookup *backend.HostLookup) *WebService { func NewWebService(config *shared.Config, hosts shared.HostBackend, lookup *backend.HostLookup) *WebService {
return &WebService{ return &WebService{
config: config, config: config,
hosts: hosts, hosts: hosts,
@ -66,7 +65,7 @@ func (w *WebService) Run() {
return return
} }
host := &hosts.Host{Hostname: hostname, Ip: "127.0.0.1"} host := &shared.Host{Hostname: hostname, Ip: "127.0.0.1"}
host.GenerateAndSetToken() host.GenerateAndSetToken()
if err = w.hosts.SetHost(host); err != nil { if err = w.hosts.SetHost(host); err != nil {

View File

@ -1,4 +1,4 @@
package config package shared
type Config struct { type Config struct {
Verbose bool Verbose bool

View File

@ -1,4 +1,4 @@
package hosts package shared
import ( import (
"crypto/sha1" "crypto/sha1"

View File

@ -1,9 +1,8 @@
package hosts package shared
import ( import (
"errors" "errors"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/pboehm/ddns/config"
"time" "time"
) )
@ -12,7 +11,7 @@ type RedisBackend struct {
pool *redis.Pool pool *redis.Pool
} }
func NewRedisBackend(config *config.Config) *RedisBackend { func NewRedisBackend(config *Config) *RedisBackend {
return &RedisBackend{ return &RedisBackend{
expirationSeconds: config.HostExpirationDays * 24 * 60 * 60, expirationSeconds: config.HostExpirationDays * 24 * 60 * 60,
pool: &redis.Pool{ pool: &redis.Pool{