Procházet zdrojové kódy

add config file path param

zu1k před 5 roky
rodič
revize
d92cd20d88
5 změnil soubory, kde provedl 41 přidání a 15 odebrání
  1. 1 2
      api/router.go
  2. 7 2
      app.json
  3. 5 0
      config/config.go
  4. 2 4
      internal/app/task.go
  5. 26 7
      main.go

+ 1 - 2
api/router.go

@@ -5,10 +5,9 @@ import (
 	"net/http"
 	"os"
 
-	"github.com/zu1k/proxypool/config"
-
 	"github.com/gin-gonic/gin"
 	_ "github.com/heroku/x/hmetrics/onload"
+	"github.com/zu1k/proxypool/config"
 	"github.com/zu1k/proxypool/internal/cache"
 	"github.com/zu1k/proxypool/pkg/provider"
 )

+ 7 - 2
app.json

@@ -7,14 +7,19 @@
   "logo": "https://raw.githubusercontent.com/zu1k/proxypool/master/assets/proxy.jpg",
   "keywords": ["golang", "ss", "ssr", "vmess", "shadowsocks", "shadowsocksr"],
   "env": {
+    "CONFIG_FILE": {
+      "description": "Path to config file, could be a url."
+    },
     "DOMAIN": {
       "description": "Domain to use."
     },
     "CF_API_EMAIL": {
-      "description": "Cloudflare Email."
+      "description": "Cloudflare Email.",
+      "required": false
     },
     "CF_API_KEY": {
-      "description": "Cloudflare API key."
+      "description": "Cloudflare API key.",
+      "required": false
     }
   }
 }

+ 5 - 0
config/config.go

@@ -9,6 +9,11 @@ import (
 	"github.com/zu1k/proxypool/pkg/tool"
 )
 
+var (
+	NeedFetch = true
+	Url       = "https://raw.githubusercontent.com/zu1k/proxypool/master/source.yaml"
+)
+
 type Source struct {
 	Type    string       `json:"type" yaml:"type"`
 	Options tool.Options `json:"options" yaml:"options"`

+ 2 - 4
internal/app/task.go

@@ -14,10 +14,8 @@ import (
 	"gopkg.in/yaml.v2"
 )
 
-var NeedFetchNewConfigFile = false
-
 func CrawlGo() {
-	if NeedFetchNewConfigFile {
+	if config.NeedFetch {
 		FetchNewConfigFileThenInit()
 	}
 	wg := &sync.WaitGroup{}
@@ -57,7 +55,7 @@ func CrawlGo() {
 
 func FetchNewConfigFileThenInit() {
 	fmt.Println("fetch new config file...")
-	resp, err := tool.GetHttpClient().Get("https://raw.githubusercontent.com/zu1k/proxypool/master/source.yaml")
+	resp, err := tool.GetHttpClient().Get(config.Url)
 	if err != nil {
 		return
 	}

+ 26 - 7
main.go

@@ -5,6 +5,10 @@ import (
 	"fmt"
 	"net/http"
 	_ "net/http/pprof"
+	"os"
+	"strings"
+
+	"github.com/zu1k/proxypool/config"
 
 	"github.com/zu1k/proxypool/internal/cron"
 
@@ -28,15 +32,17 @@ func main() {
 		go pprof()
 	}
 
-	if configFilePath == "" {
-		app.NeedFetchNewConfigFile = true
-		app.FetchNewConfigFileThenInit()
+	envConfigFilePath := os.Getenv("CONFIG_FILE")
+	if envConfigFilePath == "" {
+		envConfigFilePath = "https://raw.githubusercontent.com/zu1k/proxypool/master/source.yaml"
+	}
+
+	if configFilePath != "" {
+		initConfigFile(configFilePath)
 	} else {
-		err := app.InitConfigAndGetters(configFilePath)
-		if err != nil {
-			fmt.Println(err)
-		}
+		initConfigFile(envConfigFilePath)
 	}
+
 	proxy.InitGeoIpDB()
 
 	go cron.Cron()
@@ -45,6 +51,19 @@ func main() {
 	api.Run()
 }
 
+func initConfigFile(path string) {
+	if strings.HasPrefix(path, "http") {
+		config.Url = path
+		config.NeedFetch = true
+		app.FetchNewConfigFileThenInit()
+	} else {
+		err := app.InitConfigAndGetters(configFilePath)
+		if err != nil {
+			panic(err)
+		}
+	}
+}
+
 func pprof() {
 	ip := "127.0.0.1:6060"
 	if err := http.ListenAndServe(ip, nil); err != nil {