mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 04:42:33 +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) {
|
||||
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) {
|
||||
|
|
|
@ -27,7 +27,7 @@ func fileType(file string) FileType {
|
|||
switch {
|
||||
case strings.HasPrefix(path.Base(file), "config."):
|
||||
return FileTypeConfig
|
||||
case strings.HasPrefix(file, common.MiddlewareComposeBasePath):
|
||||
case strings.HasPrefix(file, common.MiddlewareComposeDir):
|
||||
return FileTypeMiddleware
|
||||
}
|
||||
return FileTypeProvider
|
||||
|
@ -43,9 +43,9 @@ func (t FileType) IsValid() bool {
|
|||
|
||||
func (t FileType) GetPath(filename string) string {
|
||||
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) {
|
||||
|
|
|
@ -94,7 +94,7 @@ func listRoute(which string) any {
|
|||
}
|
||||
|
||||
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 {
|
||||
gphttp.ServerError(w, r, err)
|
||||
return
|
||||
|
@ -107,17 +107,17 @@ func listFiles(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
for _, file := range files {
|
||||
t := fileType(file)
|
||||
file = strings.TrimPrefix(file, common.ConfigBasePath+"/")
|
||||
file = strings.TrimPrefix(file, common.ConfigDir+"/")
|
||||
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 {
|
||||
gphttp.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
for _, mid := range mids {
|
||||
mid = strings.TrimPrefix(mid, common.MiddlewareComposeBasePath+"/")
|
||||
mid = strings.TrimPrefix(mid, common.MiddlewareComposeDir+"/")
|
||||
resp[FileTypeMiddleware] = append(resp[FileTypeMiddleware], mid)
|
||||
}
|
||||
gphttp.RespondJSON(w, r, resp)
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
package autocert
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"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/duckdns"
|
||||
"github.com/go-acme/lego/v4/providers/dns/ovh"
|
||||
"github.com/go-acme/lego/v4/providers/dns/porkbun"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
)
|
||||
|
||||
const (
|
||||
certBasePath = "certs/"
|
||||
CertFileDefault = certBasePath + "cert.crt"
|
||||
KeyFileDefault = certBasePath + "priv.key"
|
||||
ACMEKeyFileDefault = certBasePath + "acme.key"
|
||||
var (
|
||||
CertFileDefault = filepath.Join(common.CertsDir, "cert.crt")
|
||||
KeyFileDefault = filepath.Join(common.CertsDir, "priv.key")
|
||||
ACMEKeyFileDefault = filepath.Join(common.CertsDir, "acme.key")
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
@ -1,46 +1,10 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"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,
|
||||
}
|
||||
import "time"
|
||||
|
||||
const DockerHostFromEnv = "$DOCKER_HOST"
|
||||
|
||||
const (
|
||||
HealthCheckIntervalDefault = 5 * time.Second
|
||||
HealthCheckTimeoutDefault = 5 * time.Second
|
||||
|
||||
WakeTimeoutDefault = "30s"
|
||||
StopTimeoutDefault = "30s"
|
||||
StopMethodDefault = "stop"
|
||||
)
|
||||
|
|
|
@ -19,6 +19,8 @@ var (
|
|||
IsDebug = GetEnvBool("DEBUG", IsTest)
|
||||
IsTrace = GetEnvBool("TRACE", false) && IsDebug
|
||||
|
||||
RootDir = GetEnvString("ROOT_DIR", "./")
|
||||
|
||||
HTTP3Enabled = GetEnvBool("HTTP3_ENABLED", true)
|
||||
|
||||
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()
|
||||
if tt.init != nil {
|
||||
for _, filename := range tt.filenames {
|
||||
filepath := path.Join(common.ConfigBasePath, filename)
|
||||
filepath := path.Join(common.ConfigDir, filename)
|
||||
assert.NoError(t, tt.init(filepath))
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ func TestFileProviderValidate(t *testing.T) {
|
|||
})), cfg)
|
||||
if tt.cleanup != nil {
|
||||
for _, filename := range tt.filenames {
|
||||
filepath := path.Join(common.ConfigBasePath, filename)
|
||||
filepath := path.Join(common.ConfigDir, filename)
|
||||
assert.NoError(t, tt.cleanup(filepath))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,6 @@ import (
|
|||
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||
)
|
||||
|
||||
const errPagesBasePath = common.ErrorPagesBasePath
|
||||
|
||||
var (
|
||||
setupOnce sync.Once
|
||||
dirWatcher W.Watcher
|
||||
|
@ -26,7 +24,7 @@ var (
|
|||
|
||||
func setup() {
|
||||
t := task.RootTask("error_page", false)
|
||||
dirWatcher = W.NewDirectoryWatcher(t, errPagesBasePath)
|
||||
dirWatcher = W.NewDirectoryWatcher(t, common.ErrorPagesDir)
|
||||
loadContent()
|
||||
go watchDir()
|
||||
}
|
||||
|
@ -46,7 +44,7 @@ func GetErrorPageByStatus(statusCode int) (content []byte, ok bool) {
|
|||
}
|
||||
|
||||
func loadContent() {
|
||||
files, err := U.ListFiles(errPagesBasePath, 0)
|
||||
files, err := U.ListFiles(common.ErrorPagesDir, 0)
|
||||
if err != nil {
|
||||
logging.Err(err).Msg("failed to list error page resources")
|
||||
return
|
||||
|
|
|
@ -55,7 +55,7 @@ func All() map[string]*Middleware {
|
|||
|
||||
func LoadComposeFiles() {
|
||||
errs := gperr.NewBuilder("middleware compile errors")
|
||||
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
|
||||
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeDir, 0)
|
||||
if err != nil {
|
||||
logging.Err(err).Msg("failed to list middleware definitions")
|
||||
return
|
||||
|
|
|
@ -23,7 +23,7 @@ type FileProvider struct {
|
|||
func FileProviderImpl(filename string) ProviderImpl {
|
||||
return &FileProvider{
|
||||
fileName: filename,
|
||||
path: path.Join(common.ConfigBasePath, filename),
|
||||
path: path.Join(common.ConfigDir, filename),
|
||||
l: logging.With().Str("type", "file").Str("name", filename).Logger(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func NewConfigFileWatcher(filename string) Watcher {
|
|||
|
||||
if configDirWatcher == nil {
|
||||
t := task.RootTask("config_dir_watcher", false)
|
||||
configDirWatcher = NewDirectoryWatcher(t, common.ConfigBasePath)
|
||||
configDirWatcher = NewDirectoryWatcher(t, common.ConfigDir)
|
||||
}
|
||||
return configDirWatcher.Add(filename)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue