chore(all): return concrete types, accept interfaces

- Remove exported interfaces unused locally
- Define interfaces to accept arguments
- Return concrete types, not interfaces
This commit is contained in:
Quentin McGaw
2022-06-11 01:34:30 +00:00
parent 0378fe4a7b
commit 578ef768ab
132 changed files with 594 additions and 935 deletions

View File

@@ -5,22 +5,20 @@ import (
"encoding/json"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/dns"
)
func newDNSHandler(ctx context.Context, looper dns.Looper,
func newDNSHandler(ctx context.Context, loop DNSLoop,
warner warner) http.Handler {
return &dnsHandler{
ctx: ctx,
looper: looper,
loop: loop,
warner: warner,
}
}
type dnsHandler struct {
ctx context.Context //nolint:containedctx
looper dns.Looper
loop DNSLoop
warner warner
}
@@ -42,7 +40,7 @@ func (h *dnsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func (h *dnsHandler) getStatus(w http.ResponseWriter) {
status := h.looper.GetStatus()
status := h.loop.GetStatus()
encoder := json.NewEncoder(w)
data := statusWrapper{Status: string(status)}
if err := encoder.Encode(data); err != nil {
@@ -64,7 +62,7 @@ func (h *dnsHandler) setStatus(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
outcome, err := h.looper.ApplyStatus(h.ctx, status)
outcome, err := h.loop.ApplyStatus(h.ctx, status)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return

View File

@@ -5,20 +5,16 @@ import (
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/vpn"
)
func newHandler(ctx context.Context, logger infoWarner, logging bool,
buildInfo models.BuildInformation,
vpnLooper vpn.Looper,
pfGetter portforward.Getter,
unboundLooper dns.Looper,
vpnLooper VPNLooper,
pfGetter PortForwardedGetter,
unboundLooper DNSLoop,
updaterLooper UpdaterLooper,
publicIPLooper publicip.Looper,
publicIPLooper PublicIPLoop,
) http.Handler {
handler := &handler{}

View File

@@ -5,12 +5,10 @@ import (
"net/http"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/vpn"
)
func newHandlerV0(ctx context.Context, logger infoWarner,
vpn vpn.Looper, dns dns.Looper, updater UpdaterLooper) http.Handler {
vpn VPNLooper, dns DNSLoop, updater UpdaterLooper) http.Handler {
return &handlerV0{
ctx: ctx,
logger: logger,
@@ -23,8 +21,8 @@ func newHandlerV0(ctx context.Context, logger infoWarner,
type handlerV0 struct {
ctx context.Context //nolint:containedctx
logger infoWarner
vpn vpn.Looper
dns dns.Looper
vpn VPNLooper
dns DNSLoop
updater UpdaterLooper
}

View File

@@ -0,0 +1,30 @@
package server
import (
"context"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
publicipmodels "github.com/qdm12/gluetun/internal/publicip/models"
)
type VPNLooper interface {
GetStatus() (status models.LoopStatus)
ApplyStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error)
GetSettings() (settings settings.VPN)
}
type DNSLoop interface {
ApplyStatus(ctx context.Context, status models.LoopStatus) (
outcome string, err error)
GetStatus() (status models.LoopStatus)
}
type PortForwardedGetter interface {
GetPortForwarded() (portForwarded uint16)
}
type PublicIPLoop interface {
GetData() (data publicipmodels.IPInfoData)
}

View File

@@ -5,13 +5,10 @@ import (
"encoding/json"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/vpn"
)
func newOpenvpnHandler(ctx context.Context, looper vpn.Looper,
pfGetter portforward.Getter, w warner) http.Handler {
func newOpenvpnHandler(ctx context.Context, looper VPNLooper,
pfGetter PortForwardedGetter, w warner) http.Handler {
return &openvpnHandler{
ctx: ctx,
looper: looper,
@@ -22,8 +19,8 @@ func newOpenvpnHandler(ctx context.Context, looper vpn.Looper,
type openvpnHandler struct {
ctx context.Context //nolint:containedctx
looper vpn.Looper
pf portforward.Getter
looper VPNLooper
pf PortForwardedGetter
warner warner
}

View File

@@ -4,19 +4,17 @@ import (
"encoding/json"
"net/http"
"strings"
"github.com/qdm12/gluetun/internal/publicip"
)
func newPublicIPHandler(looper publicip.Looper, w warner) http.Handler {
func newPublicIPHandler(loop PublicIPLoop, w warner) http.Handler {
return &publicIPHandler{
looper: looper,
loop: loop,
warner: w,
}
}
type publicIPHandler struct {
looper publicip.Looper
loop PublicIPLoop
warner warner
}
@@ -36,7 +34,7 @@ func (h *publicIPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func (h *publicIPHandler) getPublicIP(w http.ResponseWriter) {
data := h.looper.GetData()
data := h.loop.GetData()
encoder := json.NewEncoder(w)
if err := encoder.Encode(data); err != nil {
h.warner.Warn(err.Error())

View File

@@ -5,18 +5,14 @@ import (
"context"
"fmt"
"github.com/qdm12/gluetun/internal/dns"
"github.com/qdm12/gluetun/internal/httpserver"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/portforward"
"github.com/qdm12/gluetun/internal/publicip"
"github.com/qdm12/gluetun/internal/vpn"
)
func New(ctx context.Context, address string, logEnabled bool, logger Logger,
buildInfo models.BuildInformation, openvpnLooper vpn.Looper,
pfGetter portforward.Getter, unboundLooper dns.Looper,
updaterLooper UpdaterLooper, publicIPLooper publicip.Looper) (server httpserver.Runner, err error) {
buildInfo models.BuildInformation, openvpnLooper VPNLooper,
pfGetter PortForwardedGetter, unboundLooper DNSLoop,
updaterLooper UpdaterLooper, publicIPLooper PublicIPLoop) (server *httpserver.Server, err error) {
handler := newHandler(ctx, logger, logEnabled, buildInfo,
openvpnLooper, pfGetter, unboundLooper, updaterLooper, publicIPLooper)