mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-26 18:58:58 +02:00
v0.26.0
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
config "github.com/yusing/godoxy/internal/config/types"
|
||||
"github.com/rs/zerolog/log"
|
||||
entrypoint "github.com/yusing/godoxy/internal/entrypoint/types"
|
||||
"github.com/yusing/godoxy/internal/health/monitor"
|
||||
"github.com/yusing/godoxy/internal/logging/accesslog"
|
||||
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/middleware"
|
||||
"github.com/yusing/godoxy/internal/route/routes"
|
||||
"github.com/yusing/godoxy/internal/types"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
"github.com/yusing/goutils/task"
|
||||
@@ -50,12 +51,12 @@ func handler(root string, spa bool, index string) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func NewFileServer(base *Route) (*FileServer, gperr.Error) {
|
||||
func NewFileServer(base *Route) (*FileServer, error) {
|
||||
s := &FileServer{Route: base}
|
||||
|
||||
s.Root = filepath.Clean(s.Root)
|
||||
if !path.IsAbs(s.Root) {
|
||||
return nil, gperr.New("`root` must be an absolute path")
|
||||
if !filepath.IsAbs(s.Root) {
|
||||
return nil, errors.New("`root` must be an absolute path")
|
||||
}
|
||||
|
||||
if s.Index == "" {
|
||||
@@ -77,8 +78,9 @@ func NewFileServer(base *Route) (*FileServer, gperr.Error) {
|
||||
}
|
||||
|
||||
// Start implements task.TaskStarter.
|
||||
func (s *FileServer) Start(parent task.Parent) gperr.Error {
|
||||
func (s *FileServer) Start(parent task.Parent) error {
|
||||
s.task = parent.Subtask("fileserver."+s.Name(), false)
|
||||
s.task.SetValue(monitor.DisplayNameKey{}, s.DisplayName())
|
||||
|
||||
pathPatterns := s.PathPatterns
|
||||
switch {
|
||||
@@ -109,7 +111,7 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
|
||||
s.accessLogger, err = accesslog.NewAccessLogger(s.task, s.AccessLog)
|
||||
if err != nil {
|
||||
s.task.Finish(err)
|
||||
return gperr.Wrap(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,20 +122,22 @@ func (s *FileServer) Start(parent task.Parent) gperr.Error {
|
||||
if s.UseHealthCheck() {
|
||||
s.HealthMon = monitor.NewMonitor(s)
|
||||
if err := s.HealthMon.Start(s.task); err != nil {
|
||||
return err
|
||||
log.Warn().EmbedObject(s).Err(err).Msg("health monitor error")
|
||||
s.HealthMon = nil
|
||||
}
|
||||
}
|
||||
|
||||
routes.HTTP.Add(s)
|
||||
if state := config.WorkingState.Load(); state != nil {
|
||||
state.ShortLinkMatcher().AddRoute(s.Alias)
|
||||
ep := entrypoint.FromCtx(parent.Context())
|
||||
if ep == nil {
|
||||
err := errors.New("entrypoint not initialized")
|
||||
s.task.Finish(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ep.StartAddRoute(s); err != nil {
|
||||
s.task.Finish(err)
|
||||
return err
|
||||
}
|
||||
s.task.OnFinished("remove_route_from_http", func() {
|
||||
routes.HTTP.Del(s)
|
||||
if state := config.WorkingState.Load(); state != nil {
|
||||
state.ShortLinkMatcher().DelRoute(s.Alias)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user