mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
chore: add ROOT_DIR environment variable, refactor
This commit is contained in:
parent
5cdbe81beb
commit
d8eff90acc
12 changed files with 56 additions and 59 deletions
|
@ -59,7 +59,7 @@ func AgentCertsFilepath(host string) (filepathOut string, ok bool) {
|
||||||
if !isValidAgentHost(host) {
|
if !isValidAgentHost(host) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
return filepath.Join(common.AgentCertsBasePath, host+".zip"), true
|
return filepath.Join(common.CertsDir, host+".zip"), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExtractCert(data []byte) (ca, crt, key []byte, err error) {
|
func ExtractCert(data []byte) (ca, crt, key []byte, err error) {
|
||||||
|
|
|
@ -27,7 +27,7 @@ func fileType(file string) FileType {
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(path.Base(file), "config."):
|
case strings.HasPrefix(path.Base(file), "config."):
|
||||||
return FileTypeConfig
|
return FileTypeConfig
|
||||||
case strings.HasPrefix(file, common.MiddlewareComposeBasePath):
|
case strings.HasPrefix(file, common.MiddlewareComposeDir):
|
||||||
return FileTypeMiddleware
|
return FileTypeMiddleware
|
||||||
}
|
}
|
||||||
return FileTypeProvider
|
return FileTypeProvider
|
||||||
|
@ -43,9 +43,9 @@ func (t FileType) IsValid() bool {
|
||||||
|
|
||||||
func (t FileType) GetPath(filename string) string {
|
func (t FileType) GetPath(filename string) string {
|
||||||
if t == FileTypeMiddleware {
|
if t == FileTypeMiddleware {
|
||||||
return path.Join(common.MiddlewareComposeBasePath, filename)
|
return path.Join(common.MiddlewareComposeDir, filename)
|
||||||
}
|
}
|
||||||
return path.Join(common.ConfigBasePath, filename)
|
return path.Join(common.ConfigDir, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getArgs(r *http.Request) (fileType FileType, filename string, err error) {
|
func getArgs(r *http.Request) (fileType FileType, filename string, err error) {
|
||||||
|
|
|
@ -94,7 +94,7 @@ func listRoute(which string) any {
|
||||||
}
|
}
|
||||||
|
|
||||||
func listFiles(w http.ResponseWriter, r *http.Request) {
|
func listFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
files, err := utils.ListFiles(common.ConfigBasePath, 0, true)
|
files, err := utils.ListFiles(common.ConfigDir, 0, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gphttp.ServerError(w, r, err)
|
gphttp.ServerError(w, r, err)
|
||||||
return
|
return
|
||||||
|
@ -107,17 +107,17 @@ func listFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
t := fileType(file)
|
t := fileType(file)
|
||||||
file = strings.TrimPrefix(file, common.ConfigBasePath+"/")
|
file = strings.TrimPrefix(file, common.ConfigDir+"/")
|
||||||
resp[t] = append(resp[t], file)
|
resp[t] = append(resp[t], file)
|
||||||
}
|
}
|
||||||
|
|
||||||
mids, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0, true)
|
mids, err := utils.ListFiles(common.MiddlewareComposeDir, 0, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gphttp.ServerError(w, r, err)
|
gphttp.ServerError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, mid := range mids {
|
for _, mid := range mids {
|
||||||
mid = strings.TrimPrefix(mid, common.MiddlewareComposeBasePath+"/")
|
mid = strings.TrimPrefix(mid, common.MiddlewareComposeDir+"/")
|
||||||
resp[FileTypeMiddleware] = append(resp[FileTypeMiddleware], mid)
|
resp[FileTypeMiddleware] = append(resp[FileTypeMiddleware], mid)
|
||||||
}
|
}
|
||||||
gphttp.RespondJSON(w, r, resp)
|
gphttp.RespondJSON(w, r, resp)
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
package autocert
|
package autocert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/go-acme/lego/v4/providers/dns/clouddns"
|
"github.com/go-acme/lego/v4/providers/dns/clouddns"
|
||||||
"github.com/go-acme/lego/v4/providers/dns/cloudflare"
|
"github.com/go-acme/lego/v4/providers/dns/cloudflare"
|
||||||
"github.com/go-acme/lego/v4/providers/dns/duckdns"
|
"github.com/go-acme/lego/v4/providers/dns/duckdns"
|
||||||
"github.com/go-acme/lego/v4/providers/dns/ovh"
|
"github.com/go-acme/lego/v4/providers/dns/ovh"
|
||||||
"github.com/go-acme/lego/v4/providers/dns/porkbun"
|
"github.com/go-acme/lego/v4/providers/dns/porkbun"
|
||||||
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
certBasePath = "certs/"
|
CertFileDefault = filepath.Join(common.CertsDir, "cert.crt")
|
||||||
CertFileDefault = certBasePath + "cert.crt"
|
KeyFileDefault = filepath.Join(common.CertsDir, "priv.key")
|
||||||
KeyFileDefault = certBasePath + "priv.key"
|
ACMEKeyFileDefault = filepath.Join(common.CertsDir, "acme.key")
|
||||||
ACMEKeyFileDefault = certBasePath + "acme.key"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -1,46 +1,10 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// file, folder structure
|
|
||||||
|
|
||||||
const (
|
|
||||||
DotEnvPath = ".env"
|
|
||||||
DotEnvExamplePath = ".env.example"
|
|
||||||
|
|
||||||
ConfigBasePath = "config"
|
|
||||||
ConfigFileName = "config.yml"
|
|
||||||
ConfigExampleFileName = "config.example.yml"
|
|
||||||
ConfigPath = ConfigBasePath + "/" + ConfigFileName
|
|
||||||
HomepageJSONConfigPath = ConfigBasePath + "/.homepage.json"
|
|
||||||
IconListCachePath = ConfigBasePath + "/.icon_list_cache.json"
|
|
||||||
IconCachePath = ConfigBasePath + "/.icon_cache.json"
|
|
||||||
|
|
||||||
MiddlewareComposeBasePath = ConfigBasePath + "/middlewares"
|
|
||||||
|
|
||||||
ComposeFileName = "compose.yml"
|
|
||||||
ComposeExampleFileName = "compose.example.yml"
|
|
||||||
|
|
||||||
ErrorPagesBasePath = "error_pages"
|
|
||||||
|
|
||||||
AgentCertsBasePath = "certs"
|
|
||||||
)
|
|
||||||
|
|
||||||
var RequiredDirectories = []string{
|
|
||||||
ConfigBasePath,
|
|
||||||
ErrorPagesBasePath,
|
|
||||||
MiddlewareComposeBasePath,
|
|
||||||
}
|
|
||||||
|
|
||||||
const DockerHostFromEnv = "$DOCKER_HOST"
|
const DockerHostFromEnv = "$DOCKER_HOST"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HealthCheckIntervalDefault = 5 * time.Second
|
HealthCheckIntervalDefault = 5 * time.Second
|
||||||
HealthCheckTimeoutDefault = 5 * time.Second
|
HealthCheckTimeoutDefault = 5 * time.Second
|
||||||
|
|
||||||
WakeTimeoutDefault = "30s"
|
|
||||||
StopTimeoutDefault = "30s"
|
|
||||||
StopMethodDefault = "stop"
|
|
||||||
)
|
)
|
||||||
|
|
|
@ -19,6 +19,8 @@ var (
|
||||||
IsDebug = GetEnvBool("DEBUG", IsTest)
|
IsDebug = GetEnvBool("DEBUG", IsTest)
|
||||||
IsTrace = GetEnvBool("TRACE", false) && IsDebug
|
IsTrace = GetEnvBool("TRACE", false) && IsDebug
|
||||||
|
|
||||||
|
RootDir = GetEnvString("ROOT_DIR", "./")
|
||||||
|
|
||||||
HTTP3Enabled = GetEnvBool("HTTP3_ENABLED", true)
|
HTTP3Enabled = GetEnvBool("HTTP3_ENABLED", true)
|
||||||
|
|
||||||
ProxyHTTPAddr,
|
ProxyHTTPAddr,
|
||||||
|
|
31
internal/common/paths.go
Normal file
31
internal/common/paths.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// file, folder structure
|
||||||
|
|
||||||
|
var (
|
||||||
|
ConfigDir = filepath.Join(RootDir, "config")
|
||||||
|
ConfigFileName = "config.yml"
|
||||||
|
ConfigExampleFileName = "config.example.yml"
|
||||||
|
ConfigPath = filepath.Join(ConfigDir, ConfigFileName)
|
||||||
|
|
||||||
|
MiddlewareComposeDir = filepath.Join(ConfigDir, "middlewares")
|
||||||
|
ErrorPagesDir = filepath.Join(RootDir, "error_pages")
|
||||||
|
CertsDir = filepath.Join(RootDir, "certs")
|
||||||
|
|
||||||
|
DataDir = filepath.Join(RootDir, "data")
|
||||||
|
MetricsDataDir = filepath.Join(DataDir, "metrics")
|
||||||
|
|
||||||
|
HomepageJSONConfigPath = filepath.Join(DataDir, "homepage.json")
|
||||||
|
IconListCachePath = filepath.Join(DataDir, "icon_list_cache.json")
|
||||||
|
IconCachePath = filepath.Join(DataDir, "icon_cache.json")
|
||||||
|
)
|
||||||
|
|
||||||
|
var RequiredDirectories = []string{
|
||||||
|
ConfigDir,
|
||||||
|
ErrorPagesDir,
|
||||||
|
MiddlewareComposeDir,
|
||||||
|
}
|
|
@ -56,7 +56,7 @@ func TestFileProviderValidate(t *testing.T) {
|
||||||
cfg := config.DefaultConfig()
|
cfg := config.DefaultConfig()
|
||||||
if tt.init != nil {
|
if tt.init != nil {
|
||||||
for _, filename := range tt.filenames {
|
for _, filename := range tt.filenames {
|
||||||
filepath := path.Join(common.ConfigBasePath, filename)
|
filepath := path.Join(common.ConfigDir, filename)
|
||||||
assert.NoError(t, tt.init(filepath))
|
assert.NoError(t, tt.init(filepath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ func TestFileProviderValidate(t *testing.T) {
|
||||||
})), cfg)
|
})), cfg)
|
||||||
if tt.cleanup != nil {
|
if tt.cleanup != nil {
|
||||||
for _, filename := range tt.filenames {
|
for _, filename := range tt.filenames {
|
||||||
filepath := path.Join(common.ConfigBasePath, filename)
|
filepath := path.Join(common.ConfigDir, filename)
|
||||||
assert.NoError(t, tt.cleanup(filepath))
|
assert.NoError(t, tt.cleanup(filepath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,6 @@ import (
|
||||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||||
)
|
)
|
||||||
|
|
||||||
const errPagesBasePath = common.ErrorPagesBasePath
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
setupOnce sync.Once
|
setupOnce sync.Once
|
||||||
dirWatcher W.Watcher
|
dirWatcher W.Watcher
|
||||||
|
@ -26,7 +24,7 @@ var (
|
||||||
|
|
||||||
func setup() {
|
func setup() {
|
||||||
t := task.RootTask("error_page", false)
|
t := task.RootTask("error_page", false)
|
||||||
dirWatcher = W.NewDirectoryWatcher(t, errPagesBasePath)
|
dirWatcher = W.NewDirectoryWatcher(t, common.ErrorPagesDir)
|
||||||
loadContent()
|
loadContent()
|
||||||
go watchDir()
|
go watchDir()
|
||||||
}
|
}
|
||||||
|
@ -46,7 +44,7 @@ func GetErrorPageByStatus(statusCode int) (content []byte, ok bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadContent() {
|
func loadContent() {
|
||||||
files, err := U.ListFiles(errPagesBasePath, 0)
|
files, err := U.ListFiles(common.ErrorPagesDir, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Err(err).Msg("failed to list error page resources")
|
logging.Err(err).Msg("failed to list error page resources")
|
||||||
return
|
return
|
||||||
|
|
|
@ -55,7 +55,7 @@ func All() map[string]*Middleware {
|
||||||
|
|
||||||
func LoadComposeFiles() {
|
func LoadComposeFiles() {
|
||||||
errs := gperr.NewBuilder("middleware compile errors")
|
errs := gperr.NewBuilder("middleware compile errors")
|
||||||
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
|
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeDir, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Err(err).Msg("failed to list middleware definitions")
|
logging.Err(err).Msg("failed to list middleware definitions")
|
||||||
return
|
return
|
||||||
|
|
|
@ -23,7 +23,7 @@ type FileProvider struct {
|
||||||
func FileProviderImpl(filename string) ProviderImpl {
|
func FileProviderImpl(filename string) ProviderImpl {
|
||||||
return &FileProvider{
|
return &FileProvider{
|
||||||
fileName: filename,
|
fileName: filename,
|
||||||
path: path.Join(common.ConfigBasePath, filename),
|
path: path.Join(common.ConfigDir, filename),
|
||||||
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
|
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ func NewConfigFileWatcher(filename string) Watcher {
|
||||||
|
|
||||||
if configDirWatcher == nil {
|
if configDirWatcher == nil {
|
||||||
t := task.RootTask("config_dir_watcher", false)
|
t := task.RootTask("config_dir_watcher", false)
|
||||||
configDirWatcher = NewDirectoryWatcher(t, common.ConfigBasePath)
|
configDirWatcher = NewDirectoryWatcher(t, common.ConfigDir)
|
||||||
}
|
}
|
||||||
return configDirWatcher.Add(filename)
|
return configDirWatcher.Add(filename)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue