GoDoxy/internal/route/provider/file.go
Yuzerion 1a5f3735cf
Feat/fileserver (#60)
* cleanup code for URL type

* fix makefile for trace mode

* refactor, merge Entry, RawEntry and Route into one. 

* Implement fileserver.

* refactor: rename HTTPRoute to ReverseProxyRoute to avoid confusion

* refactor: move metrics logger to middleware package

- fix prometheus metrics for load balanced routes
  - route will now fail when health monitor fail to start

* fix extra output of ls-* commands by defer initializaing stuff, speed up start time

* add test for path traversal attack, small fix on FileServer.Start method

* rename rule.on.bypass to pass

* refactor and fixed map-to-map  deserialization

* updated route loading logic

* schemas: add "add_prefix" option to modify_request middleware


* updated route JSONMarshalling

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-02-06 18:23:10 +08:00

76 lines
1.6 KiB
Go

package provider
import (
"os"
"path"
"strings"
"github.com/rs/zerolog"
"github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/route"
"github.com/yusing/go-proxy/internal/utils"
W "github.com/yusing/go-proxy/internal/watcher"
)
type FileProvider struct {
fileName string
path string
l zerolog.Logger
}
func FileProviderImpl(filename string) (ProviderImpl, error) {
impl := &FileProvider{
fileName: filename,
path: path.Join(common.ConfigBasePath, filename),
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
}
_, err := os.Stat(impl.path)
if err != nil {
return nil, err
}
return impl, nil
}
func validate(data []byte) (routes route.Routes, err E.Error) {
err = utils.DeserializeYAML(data, &routes)
return
}
func Validate(data []byte) (err E.Error) {
_, err = validate(data)
return
}
func (p *FileProvider) String() string {
return p.fileName
}
func (p *FileProvider) ShortName() string {
return strings.Split(p.fileName, ".")[0]
}
func (p *FileProvider) IsExplicitOnly() bool {
return false
}
func (p *FileProvider) Logger() *zerolog.Logger {
return &p.l
}
func (p *FileProvider) loadRoutesImpl() (route.Routes, E.Error) {
data, err := os.ReadFile(p.path)
if err != nil {
return nil, E.Wrap(err)
}
routes, err := validate(data)
if err != nil && len(routes) == 0 {
return nil, E.Wrap(err)
}
return routes, E.Wrap(err)
}
func (p *FileProvider) NewWatcher() W.Watcher {
return W.NewConfigFileWatcher(p.fileName)
}