mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-23 09:52:01 +01:00
This is a large-scale refactoring across the codebase that replaces the custom `gperr.Error` type with Go's standard `error` interface. The changes include: - Replacing `gperr.Error` return types with `error` in function signatures - Using `errors.New()` and `fmt.Errorf()` instead of `gperr.New()` and `gperr.Errorf()` - Using `%w` format verb for error wrapping instead of `.With()` method - Replacing `gperr.Subject()` calls with `gperr.PrependSubject()` - Converting error logging from `gperr.Log*()` functions to zerolog's `.Err().Msg()` pattern - Update NewLogger to handle multiline error message - Updating `goutils` submodule to latest commit This refactoring aligns with Go idioms and removes the dependency on custom error handling abstractions in favor of standard library patterns.
105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"github.com/yusing/godoxy/internal/route"
|
|
provider "github.com/yusing/godoxy/internal/route/provider/types"
|
|
"github.com/yusing/godoxy/internal/watcher"
|
|
eventsPkg "github.com/yusing/godoxy/internal/watcher/events"
|
|
gperr "github.com/yusing/goutils/errs"
|
|
"github.com/yusing/goutils/task"
|
|
)
|
|
|
|
type EventHandler struct {
|
|
provider *Provider
|
|
|
|
errs gperr.Builder
|
|
}
|
|
|
|
func (p *Provider) newEventHandler() *EventHandler {
|
|
return &EventHandler{
|
|
provider: p,
|
|
errs: gperr.NewBuilder("event errors"),
|
|
}
|
|
}
|
|
|
|
func (handler *EventHandler) Handle(parent task.Parent, events []watcher.Event) {
|
|
oldRoutes := handler.provider.lockCloneRoutes()
|
|
|
|
isForceReload := false
|
|
for _, event := range events {
|
|
if event.Action == eventsPkg.ActionForceReload {
|
|
isForceReload = true
|
|
break
|
|
}
|
|
}
|
|
|
|
newRoutes, err := handler.provider.loadRoutes()
|
|
if err != nil {
|
|
handler.errs.Add(err)
|
|
if len(newRoutes) == 0 && !isForceReload {
|
|
return
|
|
}
|
|
}
|
|
|
|
for k, oldr := range oldRoutes {
|
|
newr, ok := newRoutes[k]
|
|
switch {
|
|
case !ok:
|
|
handler.Remove(oldr)
|
|
case handler.matchAny(events, newr):
|
|
handler.Update(parent, oldr, newr)
|
|
}
|
|
}
|
|
for k, newr := range newRoutes {
|
|
if _, ok := oldRoutes[k]; !ok {
|
|
handler.Add(parent, newr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (handler *EventHandler) matchAny(events []watcher.Event, route *route.Route) bool {
|
|
for _, event := range events {
|
|
if handler.match(event, route) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (handler *EventHandler) match(event watcher.Event, route *route.Route) bool {
|
|
switch handler.provider.GetType() {
|
|
case provider.ProviderTypeDocker, provider.ProviderTypeAgent:
|
|
return route.Container.ContainerID == event.ActorID ||
|
|
route.Container.ContainerName == event.ActorName
|
|
case provider.ProviderTypeFile:
|
|
return true
|
|
}
|
|
// should never happen
|
|
return false
|
|
}
|
|
|
|
func (handler *EventHandler) Add(parent task.Parent, route *route.Route) {
|
|
err := handler.provider.startRoute(parent, route)
|
|
if err != nil {
|
|
handler.errs.AddSubjectf(err, "add")
|
|
}
|
|
}
|
|
|
|
func (handler *EventHandler) Remove(route *route.Route) {
|
|
route.FinishAndWait("route removed")
|
|
}
|
|
|
|
func (handler *EventHandler) Update(parent task.Parent, oldRoute *route.Route, newRoute *route.Route) {
|
|
oldRoute.FinishAndWait("route update")
|
|
err := handler.provider.startRoute(parent, newRoute)
|
|
if err != nil {
|
|
handler.errs.AddSubjectf(err, "update")
|
|
}
|
|
}
|
|
|
|
func (handler *EventHandler) Log() {
|
|
if err := handler.errs.Error(); err != nil {
|
|
handler.provider.Logger().Info().Msg(err.Error())
|
|
}
|
|
}
|