introduce core config file (settings that cannot be edited from the web)

This commit is contained in:
Jakob Ketterl 2021-02-06 16:38:03 +01:00
parent 54fb58755d
commit 3226c01f60
5 changed files with 44 additions and 4 deletions

View File

@ -1,5 +1,6 @@
config_webrx.py etc/openwebrx/
bands.json etc/openwebrx/
bookmarks.json etc/openwebrx/
openwebrx.conf etc/openwebrx/
users.json etc/openwebrx/
systemd/openwebrx.service lib/systemd/system/

View File

@ -15,6 +15,9 @@ fi
if [[ ! -f /etc/openwebrx/users.json ]] ; then
cp users.json /etc/openwebrx/
fi
if [[ ! -f /etc/openwebrx/openwebrx.conf ]] ; then
cp openwebrx.conf /etc/openwebrx/
fi
_term() {

5
openwebrx.conf Normal file
View File

@ -0,0 +1,5 @@
[core]
data_directory = /var/lib/openwebrx
[web]
port = 8073

View File

@ -1,6 +1,6 @@
from http.server import HTTPServer
from owrx.http import RequestHandler
from owrx.config import Config
from owrx.config import Config, CoreConfig
from owrx.feature import FeatureDetector
from owrx.sdr import SdrService
from socketserver import ThreadingMixIn
@ -73,7 +73,7 @@ Support and info: https://groups.io/g/openwebrx
Services.start()
try:
server = ThreadedHttpServer(("0.0.0.0", pm["web_port"]), RequestHandler)
server = ThreadedHttpServer(("0.0.0.0", CoreConfig().get_web_port()), RequestHandler)
server.serve_forever()
except SignalException:
WebSocketConnection.closeAll()

View File

@ -4,6 +4,7 @@ import os
import logging
import json
from abc import ABC, abstractmethod
from configparser import ConfigParser
logger = logging.getLogger(__name__)
@ -56,6 +57,31 @@ class ConfigMigratorVersion2(ConfigMigrator):
return config
class CoreConfig(object):
defaults = {
"core": {
"data_directory": "/var/lib/openwebrx",
},
"web": {
"port": 8073,
},
}
def __init__(self):
config = ConfigParser()
config.read(["./openwebrx.conf", "/etc/openwebrx/openwebrx.conf"])
self.data_directory = config.get(
"core", "data_directory", fallback=CoreConfig.defaults["core"]["data_directory"]
)
self.web_port = config.getint("web", "port", fallback=CoreConfig.defaults["web"]["port"])
def get_web_port(self):
return self.web_port
def get_data_directory(self):
return self.data_directory
class Config:
sharedConfig = None
currentVersion = 3
@ -84,9 +110,14 @@ class Config:
pm[k] = v
return pm
@staticmethod
def _getSettingsFile():
coreConfig = CoreConfig()
return "{data_directory}/settings.json".format(data_directory=coreConfig.get_data_directory())
@staticmethod
def _loadConfig():
for file in ["./settings.json", "/etc/openwebrx/config_webrx.py", "./config_webrx.py"]:
for file in [Config._getSettingsFile(), "/etc/openwebrx/config_webrx.py", "./config_webrx.py"]:
try:
if file.endswith(".py"):
return Config._loadPythonFile(file)
@ -106,7 +137,7 @@ class Config:
@staticmethod
def store():
with open("settings.json", "w") as file:
with open(Config._getSettingsFile(), "w") as file:
json.dump(Config.get().__dict__(), file, indent=4)
@staticmethod