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:
@@ -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
|
||||
|
||||
@@ -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{}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
30
internal/server/interfaces.go
Normal file
30
internal/server/interfaces.go
Normal 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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user