Skip to content

Commit 631de5a

Browse files
authored
fix: config loading local path error (#26)
* fix: config loading local path error * test: add config test case
1 parent 6c10aa1 commit 631de5a

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

apisix/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18+
import os
1819
import click
1920

2021
from apisix.runner.server.server import Server as NewServer
@@ -31,7 +32,7 @@ def runner() -> None:
3132

3233
@runner.command()
3334
def start() -> None:
34-
config = NewConfig()
35+
config = NewConfig(os.path.dirname(os.path.abspath(__file__)))
3536
server = NewServer(config)
3637
server.receive()
3738

apisix/runner/server/config.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@ def level(self, level: str) -> None:
7979

8080
class Config:
8181

82-
def __init__(self, config_name: str = "config.yaml"):
82+
def __init__(self, config_path: str = "", config_name: str = "config.yaml"):
8383
"""
8484
init config
85+
:param config_path:
86+
local config file path
8587
:param config_name:
8688
local config file name
8789
"""
8890
self.socket = _ConfigSocket()
8991
self.logging = _ConfigLogging()
90-
self._loading_config(config_name)
92+
self._loading_config(config_path, config_name)
9193

9294
@staticmethod
9395
def _get_env_config(config: str):
@@ -101,13 +103,17 @@ def _get_env_config(config: str):
101103
return os.getenv(env_name)
102104
return config
103105

104-
def _loading_config(self, config_name: str):
106+
def _loading_config(self, config_path: str, config_name: str):
105107
"""
106108
load local configuration file
109+
:param config_path:
107110
:param config_name:
108111
:return:
109112
"""
110-
abs_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
113+
if len(config_path) and os.path.exists(config_path):
114+
abs_path = config_path
115+
else:
116+
abs_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
111117
cf_path = "%s/%s" % (abs_path, config_name)
112118
if not os.path.exists(cf_path):
113119
print("ERR: config file `%s` not exists" % cf_path)

tests/runner/server/test_config.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
# limitations under the License.
1616
#
1717

18+
import os
1819
import logging
1920
from apisix.runner.server.config import Config as NewServerConfig
2021

2122

22-
def test_config():
23+
def test_config_default():
2324
config = NewServerConfig()
2425

2526
config.logging.level = "INFO"
@@ -36,3 +37,22 @@ def test_config():
3637

3738
config.socket.file = "/test/runner.sock"
3839
assert config.socket.file == "/test/runner.sock"
40+
41+
42+
def test_config_custom():
43+
config = NewServerConfig("%s/apisix" % os.path.abspath(os.path.join(os.getcwd())), "config.yaml")
44+
45+
config.logging.level = "NOTSET"
46+
assert config.logging.level == logging.NOTSET
47+
48+
config.logging.level = "INFO"
49+
assert config.logging.level == logging.INFO
50+
51+
config.logging.level = "ERROR"
52+
assert config.logging.level == logging.ERROR
53+
54+
config.logging.level = "WARN"
55+
assert config.logging.level == logging.WARNING
56+
57+
config.socket.file = "/test/runner.sock"
58+
assert config.socket.file == "/test/runner.sock"

0 commit comments

Comments
 (0)