mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-22 01:20:09 +01:00
* Add comprehensive post-request rules support for response phase * Enable response body, status, and header manipulation via set commands * Refactor command handlers to support both request and response phases * Implement response modifier system for post-request template execution * Support response-based rule matching with status and header checks * Add comprehensive benchmarks for matcher performance * Refactor authentication and proxying commands for unified error handling * Support negated conditions with ! * Enhance error handling, error formatting and validation * Routes: add `rule_file` field with rule preset support * Environment variable substitution: now supports variables without `GODOXY_` prefix * new conditions: * `on resp_header <key> [<value>]` * `on status <status>` * new commands: * `require_auth` * `set resp_header <key> <template>` * `set resp_body <template>` * `set status <code>` * `log <level> <path> <template>` * `notify <level> <provider> <title_template> <body_template>`
106 lines
2.1 KiB
Go
106 lines
2.1 KiB
Go
package accesslog
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/yusing/godoxy/internal/utils"
|
|
)
|
|
|
|
type File struct {
|
|
f *os.File
|
|
|
|
// os.File.Name() may not equal to key of `openedFiles`.
|
|
// Store it for later delete from `openedFiles`.
|
|
path string
|
|
|
|
refCount *utils.RefCount
|
|
}
|
|
|
|
var (
|
|
openedFiles = make(map[string]*File)
|
|
openedFilesMu sync.Mutex
|
|
)
|
|
|
|
// NewFileIO creates a new file writer with cleaned path.
|
|
//
|
|
// If the file is already opened, it will be returned.
|
|
func NewFileIO(path string) (WriterWithName, error) {
|
|
openedFilesMu.Lock()
|
|
defer openedFilesMu.Unlock()
|
|
|
|
var file *File
|
|
path = filepath.Clean(path)
|
|
if opened, ok := openedFiles[path]; ok {
|
|
opened.refCount.Add()
|
|
return opened, nil
|
|
}
|
|
|
|
// cannot open as O_APPEND as we need Seek and WriteAt
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("access log open error: %w", err)
|
|
}
|
|
if _, err := f.Seek(0, io.SeekEnd); err != nil {
|
|
return nil, fmt.Errorf("access log seek error: %w", err)
|
|
}
|
|
file = &File{f: f, path: path, refCount: utils.NewRefCounter()}
|
|
openedFiles[path] = file
|
|
go file.closeOnZero()
|
|
return file, nil
|
|
}
|
|
|
|
func (f *File) Name() string {
|
|
return f.f.Name()
|
|
}
|
|
|
|
func (f *File) Write(p []byte) (n int, err error) {
|
|
return f.f.Write(p)
|
|
}
|
|
|
|
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
|
|
return f.f.ReadAt(p, off)
|
|
}
|
|
|
|
func (f *File) WriteAt(p []byte, off int64) (n int, err error) {
|
|
return f.f.WriteAt(p, off)
|
|
}
|
|
|
|
func (f *File) Seek(offset int64, whence int) (int64, error) {
|
|
return f.f.Seek(offset, whence)
|
|
}
|
|
|
|
func (f *File) Size() (int64, error) {
|
|
stat, err := f.f.Stat()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return stat.Size(), nil
|
|
}
|
|
|
|
func (f *File) Truncate(size int64) error {
|
|
return f.f.Truncate(size)
|
|
}
|
|
|
|
func (f *File) Close() error {
|
|
f.refCount.Sub()
|
|
return nil
|
|
}
|
|
|
|
func (f *File) closeOnZero() {
|
|
defer log.Debug().
|
|
Str("path", f.path).
|
|
Msg("access log closed")
|
|
|
|
<-f.refCount.Zero()
|
|
|
|
openedFilesMu.Lock()
|
|
delete(openedFiles, f.path)
|
|
openedFilesMu.Unlock()
|
|
f.f.Close()
|
|
}
|