feat(server/portforward): change route from /v1/openvpn/portforwarded to /v1/portforward
- This route has nothing to do with openvpn specifically - Remove the `ed` in `portforwarded` to accomodate future routes such as changing the state of port forwarding - maintaining retrocompatibility with `/v1/openvpn/portforwarded` - maintaining retrocompatibility with `/openvpn/portforwarded` - Moved to its own handler `/v1/portforward` instead of `/v1/vpn/portforward` to reduce the complexity of the vpn handler
This commit is contained in:
52
internal/server/portforward.go
Normal file
52
internal/server/portforward.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func newPortForwardHandler(ctx context.Context,
|
||||
portForward PortForwardedGetter, warner warner,
|
||||
) http.Handler {
|
||||
return &portForwardHandler{
|
||||
ctx: ctx,
|
||||
portForward: portForward,
|
||||
warner: warner,
|
||||
}
|
||||
}
|
||||
|
||||
type portForwardHandler struct {
|
||||
ctx context.Context //nolint:containedctx
|
||||
portForward PortForwardedGetter
|
||||
warner warner
|
||||
}
|
||||
|
||||
func (h *portForwardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
h.getPortForwarded(w)
|
||||
default:
|
||||
errMethodNotSupported(w, r.Method)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *portForwardHandler) getPortForwarded(w http.ResponseWriter) {
|
||||
ports := h.portForward.GetPortsForwarded()
|
||||
encoder := json.NewEncoder(w)
|
||||
var data any
|
||||
switch len(ports) {
|
||||
case 0:
|
||||
data = portWrapper{Port: 0} // TODO v4 change to portsWrapper
|
||||
case 1:
|
||||
data = portWrapper{Port: ports[0]} // TODO v4 change to portsWrapper
|
||||
default:
|
||||
data = portsWrapper{Ports: ports}
|
||||
}
|
||||
|
||||
err := encoder.Encode(data)
|
||||
if err != nil {
|
||||
h.warner.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user