mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-23 23:28:17 +03:00
1d6d85cff4
Squashed commit of the following: commit586b0eb180
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Nov 12 19:58:56 2024 +0300 next: upd more commitd729aa150f
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Nov 12 16:53:15 2024 +0300 next/websvc: upd more commit0c64e6cfc6
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Nov 11 21:08:51 2024 +0300 next: upd more commit05eec75222
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Nov 8 19:20:02 2024 +0300 next: upd code
122 lines
3.4 KiB
Go
122 lines
3.4 KiB
Go
package websvc
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
)
|
|
|
|
// ReqPatchSettingsHTTP describes the request to the PATCH /api/v1/settings/http
|
|
// HTTP API.
|
|
type ReqPatchSettingsHTTP struct {
|
|
// TODO(a.garipov): Add more as we go.
|
|
//
|
|
// TODO(a.garipov): Add wait time.
|
|
|
|
Addresses []netip.AddrPort `json:"addresses"`
|
|
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
|
|
Timeout aghhttp.JSONDuration `json:"timeout"`
|
|
}
|
|
|
|
// HTTPAPIHTTPSettings are the HTTP settings as used by the HTTP API. See the
|
|
// HttpSettings object in the OpenAPI specification.
|
|
type HTTPAPIHTTPSettings struct {
|
|
// TODO(a.garipov): Add more as we go.
|
|
|
|
Addresses []netip.AddrPort `json:"addresses"`
|
|
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
|
|
Timeout aghhttp.JSONDuration `json:"timeout"`
|
|
ForceHTTPS bool `json:"force_https"`
|
|
}
|
|
|
|
// handlePatchSettingsHTTP is the handler for the PATCH /api/v1/settings/http
|
|
// HTTP API.
|
|
func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Request) {
|
|
req := &ReqPatchSettingsHTTP{}
|
|
|
|
// TODO(a.garipov): Validate nulls and proper JSON patch.
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&req)
|
|
if err != nil {
|
|
aghhttp.WriteJSONResponseError(w, r, fmt.Errorf("decoding: %w", err))
|
|
|
|
return
|
|
}
|
|
|
|
newConf := &Config{
|
|
Logger: svc.logger,
|
|
Pprof: &PprofConfig{
|
|
Port: svc.pprofPort,
|
|
Enabled: svc.pprof != nil,
|
|
},
|
|
ConfigManager: svc.confMgr,
|
|
Frontend: svc.frontend,
|
|
TLS: svc.tls,
|
|
Addresses: req.Addresses,
|
|
SecureAddresses: req.SecureAddresses,
|
|
Timeout: time.Duration(req.Timeout),
|
|
ForceHTTPS: svc.forceHTTPS,
|
|
}
|
|
|
|
aghhttp.WriteJSONResponseOK(w, r, &HTTPAPIHTTPSettings{
|
|
Addresses: newConf.Addresses,
|
|
SecureAddresses: newConf.SecureAddresses,
|
|
Timeout: aghhttp.JSONDuration(newConf.Timeout),
|
|
ForceHTTPS: newConf.ForceHTTPS,
|
|
})
|
|
|
|
cancelUpd := func() {}
|
|
updCtx := context.Background()
|
|
|
|
ctx := r.Context()
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
updCtx, cancelUpd = context.WithDeadline(updCtx, deadline)
|
|
}
|
|
|
|
// Launch the new HTTP service in a separate goroutine to let this handler
|
|
// finish and thus, this server to shutdown.
|
|
go svc.relaunch(updCtx, cancelUpd, newConf)
|
|
}
|
|
|
|
// relaunch updates the web service in the configuration manager and starts it.
|
|
// It is intended to be used as a goroutine.
|
|
func (svc *Service) relaunch(ctx context.Context, cancel context.CancelFunc, newConf *Config) {
|
|
defer slogutil.RecoverAndLog(ctx, svc.logger)
|
|
|
|
defer cancel()
|
|
|
|
err := svc.confMgr.UpdateWeb(ctx, newConf)
|
|
if err != nil {
|
|
svc.logger.ErrorContext(ctx, "updating web", slogutil.KeyError, err)
|
|
|
|
return
|
|
}
|
|
|
|
// TODO(a.garipov): Consider better ways to do this.
|
|
const maxUpdDur = 5 * time.Second
|
|
updStart := time.Now()
|
|
var newSvc agh.ServiceWithConfig[*Config]
|
|
for newSvc = svc.confMgr.Web(); newSvc == svc; {
|
|
if time.Since(updStart) >= maxUpdDur {
|
|
svc.logger.ErrorContext(ctx, "failed to update service on time", "duration", maxUpdDur)
|
|
|
|
return
|
|
}
|
|
|
|
svc.logger.DebugContext(ctx, "waiting for new service")
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
|
|
err = newSvc.Start(ctx)
|
|
if err != nil {
|
|
svc.logger.ErrorContext(ctx, "new service failed", slogutil.KeyError, err)
|
|
}
|
|
}
|