Files
godoxy-yusing/internal/route/provider/file.go
yusing 6da7227f9b refactor(errs): migrate from gperr.Error to standard Go error interface
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.
2026-02-08 12:07:36 +08:00

86 lines
1.7 KiB
Go

package provider
import (
"os"
"path"
"strings"
"github.com/goccy/go-yaml"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/yusing/godoxy/internal/common"
"github.com/yusing/godoxy/internal/route"
"github.com/yusing/godoxy/internal/serialization"
W "github.com/yusing/godoxy/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: log.With().Str("type", "file").Str("name", filename).Logger(),
}
_, err := os.Stat(impl.path)
if err != nil {
return nil, err
}
return impl, nil
}
func removeXPrefix(m map[string]any) error {
for alias := range m {
if strings.HasPrefix(alias, "x-") {
delete(m, alias)
}
}
return nil
}
func validate(data []byte) (routes route.Routes, err error) {
err = serialization.UnmarshalValidate(data, &routes, yaml.Unmarshal, removeXPrefix)
return routes, err
}
func Validate(data []byte) (err error) {
_, err = validate(data)
return err
}
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, error) {
data, err := os.ReadFile(p.path)
if err != nil {
return nil, err
}
routes, err := validate(data)
if err != nil && len(routes) == 0 {
return nil, err
}
return routes, err
}
func (p *FileProvider) NewWatcher() W.Watcher {
return W.NewConfigFileWatcher(p.fileName)
}