mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-17 14:09:44 +02:00
allow multiple docker providers, added file provider support
This commit is contained in:
@@ -1,66 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
type Routes struct {
|
||||
HTTPRoutes *SafeMap[string, pathPoolMap] // id -> (path -> routes)
|
||||
StreamRoutes *SafeMap[string, StreamRoute] // id -> target
|
||||
HTTPRoutes SafeMap[string, pathPoolMap] // alias -> (path -> routes)
|
||||
StreamRoutes SafeMap[string, StreamRoute] // id -> target
|
||||
Mutex sync.Mutex
|
||||
}
|
||||
|
||||
var routes = Routes{}
|
||||
type Route interface {
|
||||
SetupListen()
|
||||
Listen()
|
||||
StopListening()
|
||||
RemoveFromRoutes()
|
||||
}
|
||||
|
||||
func isValidScheme(scheme string) bool {
|
||||
var routes = initRoutes()
|
||||
|
||||
func isValidScheme(s string) bool {
|
||||
for _, v := range ValidSchemes {
|
||||
if v == scheme {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isStreamScheme(scheme string) bool {
|
||||
func isStreamScheme(s string) bool {
|
||||
for _, v := range StreamSchemes {
|
||||
if v == scheme {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func InitRoutes() {
|
||||
utils.resetPortsInUse()
|
||||
routes.HTTPRoutes = NewSafeMap[string](newPathPoolMap)
|
||||
routes.StreamRoutes = NewSafeMap[string, StreamRoute]()
|
||||
func initRoutes() *Routes {
|
||||
r := Routes{}
|
||||
r.HTTPRoutes = NewSafeMap[string](newPathPoolMap)
|
||||
r.StreamRoutes = NewSafeMap[string, StreamRoute]()
|
||||
return &r
|
||||
}
|
||||
|
||||
func CountRoutes() int {
|
||||
return routes.HTTPRoutes.Size() + routes.StreamRoutes.Size()
|
||||
}
|
||||
|
||||
func CreateRoute(config *ProxyConfig) {
|
||||
if isStreamScheme(config.Scheme) {
|
||||
if routes.StreamRoutes.Contains(config.id) {
|
||||
glog.Infof("[Build] Duplicated %s stream %s, ignoring", config.Scheme, config.id)
|
||||
return
|
||||
func NewRoute(cfg *ProxyConfig) (Route, error) {
|
||||
if isStreamScheme(cfg.Scheme) {
|
||||
id := cfg.GetID()
|
||||
if routes.StreamRoutes.Contains(id) {
|
||||
return nil, fmt.Errorf("duplicated %s stream %s, ignoring", cfg.Scheme, id)
|
||||
}
|
||||
route, err := NewStreamRoute(config)
|
||||
route, err := NewStreamRoute(cfg)
|
||||
if err != nil {
|
||||
glog.Infoln(err)
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
routes.StreamRoutes.Set(config.id, route)
|
||||
routes.StreamRoutes.Set(id, route)
|
||||
return route, nil
|
||||
} else {
|
||||
routes.HTTPRoutes.Ensure(config.Alias)
|
||||
route, err := NewHTTPRoute(config)
|
||||
routes.HTTPRoutes.Ensure(cfg.Alias)
|
||||
route, err := NewHTTPRoute(cfg)
|
||||
if err != nil {
|
||||
glog.Infoln(err)
|
||||
return
|
||||
return nil, err
|
||||
}
|
||||
routes.HTTPRoutes.Get(config.Alias).Add(config.Path, route)
|
||||
routes.HTTPRoutes.Get(cfg.Alias).Add(cfg.Path, route)
|
||||
return route, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user