mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-01 09:32:35 +02:00
updated fileserver implmementation and schema
This commit is contained in:
parent
1d4d2a02ed
commit
9e76ebd03e
7 changed files with 117 additions and 21 deletions
|
@ -4,7 +4,12 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
gphttp "github.com/yusing/go-proxy/internal/net/http"
|
||||||
|
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/net/http/middleware"
|
"github.com/yusing/go-proxy/internal/net/http/middleware"
|
||||||
|
metricslogger "github.com/yusing/go-proxy/internal/net/http/middleware/metrics_logger"
|
||||||
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/health/monitor"
|
"github.com/yusing/go-proxy/internal/watcher/health/monitor"
|
||||||
|
@ -16,10 +21,11 @@ type (
|
||||||
FileServer struct {
|
FileServer struct {
|
||||||
*Route
|
*Route
|
||||||
|
|
||||||
task *task.Task
|
task *task.Task
|
||||||
middleware *middleware.Middleware
|
middleware *middleware.Middleware
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
startTime time.Time
|
accessLogger *accesslog.AccessLogger
|
||||||
|
startTime time.Time
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,12 +34,7 @@ func handler(root string) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileServer(base *Route) (*FileServer, E.Error) {
|
func NewFileServer(base *Route) (*FileServer, E.Error) {
|
||||||
s := &FileServer{Route: base}
|
s := &FileServer{Route: base, handler: handler(base.Root)}
|
||||||
s.handler = handler(s.Root)
|
|
||||||
|
|
||||||
if len(s.Rules) > 0 {
|
|
||||||
s.handler = s.Rules.BuildHandler(s.Alias, s.handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(s.Middlewares) > 0 {
|
if len(s.Middlewares) > 0 {
|
||||||
mid, err := middleware.BuildMiddlewareFromMap(s.Alias, s.Middlewares)
|
mid, err := middleware.BuildMiddlewareFromMap(s.Alias, s.Middlewares)
|
||||||
|
@ -50,6 +51,49 @@ func NewFileServer(base *Route) (*FileServer, E.Error) {
|
||||||
func (s *FileServer) Start(parent task.Parent) E.Error {
|
func (s *FileServer) Start(parent task.Parent) E.Error {
|
||||||
s.startTime = time.Now()
|
s.startTime = time.Now()
|
||||||
s.task = parent.Subtask("fileserver."+s.Name(), false)
|
s.task = parent.Subtask("fileserver."+s.Name(), false)
|
||||||
|
|
||||||
|
pathPatterns := s.PathPatterns
|
||||||
|
switch {
|
||||||
|
case len(pathPatterns) == 0:
|
||||||
|
case len(pathPatterns) == 1 && pathPatterns[0] == "/":
|
||||||
|
default:
|
||||||
|
mux := gphttp.NewServeMux()
|
||||||
|
patErrs := E.NewBuilder("invalid path pattern(s)")
|
||||||
|
for _, p := range pathPatterns {
|
||||||
|
patErrs.Add(mux.Handle(p, s.handler))
|
||||||
|
}
|
||||||
|
if err := patErrs.Error(); err != nil {
|
||||||
|
s.task.Finish(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.handler = mux
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.middleware != nil {
|
||||||
|
s.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.middleware.ServeHTTP(s.handler.ServeHTTP, w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.UseAccessLog() {
|
||||||
|
var err error
|
||||||
|
s.accessLogger, err = accesslog.NewFileAccessLogger(s.task, s.AccessLog)
|
||||||
|
if err != nil {
|
||||||
|
s.task.Finish(err)
|
||||||
|
return E.Wrap(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if common.PrometheusEnabled {
|
||||||
|
metricsLogger := metricslogger.NewMetricsLogger(s.TargetName())
|
||||||
|
s.handler = metricsLogger.GetHandler(s.handler)
|
||||||
|
s.task.OnCancel("reset_metrics", metricsLogger.ResetMetrics)
|
||||||
|
}
|
||||||
|
|
||||||
|
routes.SetHTTPRoute(s.TargetName(), s)
|
||||||
|
s.task.OnCancel("entrypoint_remove_route", func() {
|
||||||
|
routes.DeleteHTTPRoute(s.TargetName())
|
||||||
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +108,10 @@ func (s *FileServer) Finish(reason any) {
|
||||||
|
|
||||||
// ServeHTTP implements http.Handler.
|
// ServeHTTP implements http.Handler.
|
||||||
func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (s *FileServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
if s.middleware != nil {
|
|
||||||
s.middleware.ServeHTTP(s.handler.ServeHTTP, w, req)
|
|
||||||
}
|
|
||||||
s.handler.ServeHTTP(w, req)
|
s.handler.ServeHTTP(w, req)
|
||||||
|
if s.accessLogger != nil {
|
||||||
|
s.accessLogger.Log(req, req.Response)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status implements health.HealthMonitor.
|
// Status implements health.HealthMonitor.
|
||||||
|
|
|
@ -17,7 +17,7 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Provider struct {
|
Provider struct {
|
||||||
ProviderImpl `json:"-"`
|
ProviderImpl
|
||||||
|
|
||||||
t types.ProviderType
|
t types.ProviderType
|
||||||
routes route.Routes
|
routes route.Routes
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "godoxy-schemas",
|
"name": "godoxy-schemas",
|
||||||
"version": "0.9.1-1",
|
"version": "0.9.2-1",
|
||||||
"description": "JSON Schema and typescript types for GoDoxy configuration",
|
"description": "JSON Schema and typescript types for GoDoxy configuration",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|
File diff suppressed because one or more lines are too long
29
schemas/providers/routes.d.ts
vendored
29
schemas/providers/routes.d.ts
vendored
|
@ -9,7 +9,7 @@ export declare const PROXY_SCHEMES: readonly ["http", "https"];
|
||||||
export declare const STREAM_SCHEMES: readonly ["tcp", "udp"];
|
export declare const STREAM_SCHEMES: readonly ["tcp", "udp"];
|
||||||
export type ProxyScheme = (typeof PROXY_SCHEMES)[number];
|
export type ProxyScheme = (typeof PROXY_SCHEMES)[number];
|
||||||
export type StreamScheme = (typeof STREAM_SCHEMES)[number];
|
export type StreamScheme = (typeof STREAM_SCHEMES)[number];
|
||||||
export type Route = ReverseProxyRoute | StreamRoute;
|
export type Route = ReverseProxyRoute | FileServerRoute | StreamRoute;
|
||||||
export type Routes = {
|
export type Routes = {
|
||||||
[key: string]: Route;
|
[key: string]: Route;
|
||||||
};
|
};
|
||||||
|
@ -65,6 +65,31 @@ export type ReverseProxyRoute = {
|
||||||
*/
|
*/
|
||||||
access_log?: AccessLogConfig;
|
access_log?: AccessLogConfig;
|
||||||
};
|
};
|
||||||
|
export type FileServerRoute = {
|
||||||
|
/** Alias (subdomain or FDN)
|
||||||
|
* @minLength 1
|
||||||
|
*/
|
||||||
|
alias?: string;
|
||||||
|
scheme: "fileserver";
|
||||||
|
root: string;
|
||||||
|
/** Path patterns (only patterns that match will be proxied).
|
||||||
|
*
|
||||||
|
* See https://pkg.go.dev/net/http#hdr-Patterns-ServeMux
|
||||||
|
*/
|
||||||
|
path_patterns?: PathPattern[];
|
||||||
|
/** Middlewares */
|
||||||
|
middlewares?: MiddlewaresMap;
|
||||||
|
/** Homepage config
|
||||||
|
*
|
||||||
|
* @examples require(".").homepageExamples
|
||||||
|
*/
|
||||||
|
homepage?: HomepageConfig;
|
||||||
|
/** Access log config
|
||||||
|
*
|
||||||
|
* @examples require(".").accessLogExamples
|
||||||
|
*/
|
||||||
|
access_log?: AccessLogConfig;
|
||||||
|
};
|
||||||
export type StreamRoute = {
|
export type StreamRoute = {
|
||||||
/** Alias (subdomain or FDN)
|
/** Alias (subdomain or FDN)
|
||||||
* @minLength 1
|
* @minLength 1
|
||||||
|
@ -74,7 +99,7 @@ export type StreamRoute = {
|
||||||
*
|
*
|
||||||
* @default tcp
|
* @default tcp
|
||||||
*/
|
*/
|
||||||
scheme: StreamScheme;
|
scheme?: StreamScheme;
|
||||||
/** Stream host
|
/** Stream host
|
||||||
*
|
*
|
||||||
* @default localhost
|
* @default localhost
|
||||||
|
|
|
@ -11,7 +11,7 @@ export const STREAM_SCHEMES = ["tcp", "udp"] as const;
|
||||||
export type ProxyScheme = (typeof PROXY_SCHEMES)[number];
|
export type ProxyScheme = (typeof PROXY_SCHEMES)[number];
|
||||||
export type StreamScheme = (typeof STREAM_SCHEMES)[number];
|
export type StreamScheme = (typeof STREAM_SCHEMES)[number];
|
||||||
|
|
||||||
export type Route = ReverseProxyRoute | StreamRoute;
|
export type Route = ReverseProxyRoute | FileServerRoute | StreamRoute;
|
||||||
export type Routes = {
|
export type Routes = {
|
||||||
[key: string]: Route;
|
[key: string]: Route;
|
||||||
};
|
};
|
||||||
|
@ -69,6 +69,33 @@ export type ReverseProxyRoute = {
|
||||||
access_log?: AccessLogConfig;
|
access_log?: AccessLogConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type FileServerRoute = {
|
||||||
|
/** Alias (subdomain or FDN)
|
||||||
|
* @minLength 1
|
||||||
|
*/
|
||||||
|
alias?: string;
|
||||||
|
scheme: "fileserver";
|
||||||
|
/* File server root path */
|
||||||
|
root: string;
|
||||||
|
/** Path patterns (only patterns that match will be proxied).
|
||||||
|
*
|
||||||
|
* See https://pkg.go.dev/net/http#hdr-Patterns-ServeMux
|
||||||
|
*/
|
||||||
|
path_patterns?: PathPattern[];
|
||||||
|
/** Middlewares */
|
||||||
|
middlewares?: MiddlewaresMap;
|
||||||
|
/** Homepage config
|
||||||
|
*
|
||||||
|
* @examples require(".").homepageExamples
|
||||||
|
*/
|
||||||
|
homepage?: HomepageConfig;
|
||||||
|
/** Access log config
|
||||||
|
*
|
||||||
|
* @examples require(".").accessLogExamples
|
||||||
|
*/
|
||||||
|
access_log?: AccessLogConfig;
|
||||||
|
}
|
||||||
|
|
||||||
export type StreamRoute = {
|
export type StreamRoute = {
|
||||||
/** Alias (subdomain or FDN)
|
/** Alias (subdomain or FDN)
|
||||||
* @minLength 1
|
* @minLength 1
|
||||||
|
@ -78,7 +105,7 @@ export type StreamRoute = {
|
||||||
*
|
*
|
||||||
* @default tcp
|
* @default tcp
|
||||||
*/
|
*/
|
||||||
scheme: StreamScheme;
|
scheme?: StreamScheme;
|
||||||
/** Stream host
|
/** Stream host
|
||||||
*
|
*
|
||||||
* @default localhost
|
* @default localhost
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue