mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Merge pull request #163 from safing/feature/export-improvements
Export Improvements
This commit is contained in:
commit
d32e46aca3
6 changed files with 76 additions and 13 deletions
|
@ -3,9 +3,11 @@ package api
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -226,13 +228,34 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
|
||||||
w.Header().Add("Vary", "Origin")
|
w.Header().Add("Vary", "Origin")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle request.
|
// Check if we have a handler.
|
||||||
if handler != nil {
|
if handler == nil {
|
||||||
handler.ServeHTTP(lrw, r)
|
|
||||||
} else {
|
|
||||||
http.Error(lrw, "Not found.", http.StatusNotFound)
|
http.Error(lrw, "Not found.", http.StatusNotFound)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format panics in handler.
|
||||||
|
defer func() {
|
||||||
|
if panicValue := recover(); panicValue != nil {
|
||||||
|
if devMode() {
|
||||||
|
http.Error(
|
||||||
|
lrw,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"Internal Server Error: %s\n\n%s",
|
||||||
|
panicValue,
|
||||||
|
debug.Stack(),
|
||||||
|
),
|
||||||
|
http.StatusInternalServerError,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
http.Error(lrw, "Internal Server Error.", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Handle with registered handler.
|
||||||
|
handler.ServeHTTP(lrw, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,3 +112,22 @@ func AddToDebugInfo(di *debug.Info) {
|
||||||
lines...,
|
lines...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetActiveConfigValues returns a map with the active config values.
|
||||||
|
func GetActiveConfigValues() map[string]interface{} {
|
||||||
|
values := make(map[string]interface{})
|
||||||
|
|
||||||
|
// Collect active values from options.
|
||||||
|
_ = ForEachOption(func(opt *Option) error {
|
||||||
|
opt.Lock()
|
||||||
|
defer opt.Unlock()
|
||||||
|
|
||||||
|
if opt.ReleaseLevel <= getReleaseLevel() && opt.activeValue != nil {
|
||||||
|
values[opt.Key] = opt.activeValue.getData(opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
|
@ -76,12 +76,12 @@ func (reg *Registry) Migrate(ctx context.Context) (err error) {
|
||||||
defer reg.lock.Unlock()
|
defer reg.lock.Unlock()
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
log.Infof("[migration] migration of %s started", reg.key)
|
log.Infof("migration: migration of %s started", reg.key)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("[migration] migration of %s failed after %s: %s", reg.key, time.Since(start), err)
|
log.Errorf("migration: migration of %s failed after %s: %s", reg.key, time.Since(start), err)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("[migration] migration of %s finished after %s", reg.key, time.Since(start))
|
log.Infof("migration: migration of %s finished after %s", reg.key, time.Since(start))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ func (reg *Registry) Migrate(ctx context.Context) (err error) {
|
||||||
if err := m.MigrateFunc(migrationCtx, lastAppliedMigration, target, db); err != nil {
|
if err := m.MigrateFunc(migrationCtx, lastAppliedMigration, target, db); err != nil {
|
||||||
diag.Wrapped = err
|
diag.Wrapped = err
|
||||||
diag.FailedMigration = m.Description
|
diag.FailedMigration = m.Description
|
||||||
tracer.Infof("[migration] applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
|
tracer.Infof("migration: applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
|
||||||
tracer.Submit()
|
tracer.Submit()
|
||||||
return diag
|
return diag
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ func (reg *Registry) Migrate(ctx context.Context) (err error) {
|
||||||
diag.Wrapped = err
|
diag.Wrapped = err
|
||||||
diag.FailedMigration = m.Description
|
diag.FailedMigration = m.Description
|
||||||
}
|
}
|
||||||
tracer.Infof("[migration] applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
|
tracer.Infof("migration: applied migration for %s: %s - %s", reg.key, target.String(), m.Description)
|
||||||
tracer.Submit()
|
tracer.Submit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -180,7 +180,7 @@ func (m *Module) Resolve(failureID string) {
|
||||||
|
|
||||||
// Propagate failure status.
|
// Propagate failure status.
|
||||||
if failureUpdateNotifyFuncReady.IsSet() {
|
if failureUpdateNotifyFuncReady.IsSet() {
|
||||||
_ = m.RunWorker("failure status updater", func(context.Context) error {
|
m.StartWorker("failure status updater", func(context.Context) error {
|
||||||
// Only use data in worker that won't change anymore.
|
// Only use data in worker that won't change anymore.
|
||||||
failureUpdateNotifyFunc(FailureNone, resolveFailureID, "", "")
|
failureUpdateNotifyFunc(FailureNone, resolveFailureID, "", "")
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package updater
|
package updater
|
||||||
|
|
||||||
// Export exports the list of resources. All resources must be
|
// Export exports the list of resources.
|
||||||
// locked when accessed.
|
|
||||||
func (reg *ResourceRegistry) Export() map[string]*Resource {
|
func (reg *ResourceRegistry) Export() map[string]*Resource {
|
||||||
reg.RLock()
|
reg.RLock()
|
||||||
defer reg.RUnlock()
|
defer reg.RUnlock()
|
||||||
|
@ -9,7 +8,7 @@ func (reg *ResourceRegistry) Export() map[string]*Resource {
|
||||||
// copy the map
|
// copy the map
|
||||||
copiedResources := make(map[string]*Resource)
|
copiedResources := make(map[string]*Resource)
|
||||||
for key, val := range reg.resources {
|
for key, val := range reg.resources {
|
||||||
copiedResources[key] = val
|
copiedResources[key] = val.Export()
|
||||||
}
|
}
|
||||||
|
|
||||||
return copiedResources
|
return copiedResources
|
||||||
|
|
|
@ -117,6 +117,28 @@ func (rv *ResourceVersion) isBetaVersionNumber() bool { //nolint:unused
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Export makes a copy of the resource with only the exposed information.
|
||||||
|
// Attributes are copied and safe to access.
|
||||||
|
// Any ResourceVersion must not be modified.
|
||||||
|
func (res *Resource) Export() *Resource {
|
||||||
|
res.Lock()
|
||||||
|
defer res.Unlock()
|
||||||
|
|
||||||
|
// Copy attibutes.
|
||||||
|
export := &Resource{
|
||||||
|
Identifier: res.Identifier,
|
||||||
|
Versions: make([]*ResourceVersion, len(res.Versions)),
|
||||||
|
ActiveVersion: res.ActiveVersion,
|
||||||
|
SelectedVersion: res.SelectedVersion,
|
||||||
|
}
|
||||||
|
// Copy Versions slice.
|
||||||
|
for i := 0; i < len(res.Versions); i++ {
|
||||||
|
export.Versions[i] = res.Versions[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return export
|
||||||
|
}
|
||||||
|
|
||||||
// Len is the number of elements in the collection.
|
// Len is the number of elements in the collection.
|
||||||
// It implements sort.Interface for ResourceVersion.
|
// It implements sort.Interface for ResourceVersion.
|
||||||
func (res *Resource) Len() int {
|
func (res *Resource) Len() int {
|
||||||
|
|
Loading…
Add table
Reference in a new issue