mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-25 10:31:30 +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.
86 lines
1.7 KiB
Go
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)
|
|
}
|