change configuration method

This commit is contained in:
Denis Lehmann 2022-04-25 20:18:16 +02:00
parent 69348fa45a
commit ab6dbb374b
5 changed files with 24 additions and 43 deletions

View file

@ -52,7 +52,7 @@
A WSGI server like [[https://gunicorn.org/][Gunicorn]] can then be used to serve the app for example like this: A WSGI server like [[https://gunicorn.org/][Gunicorn]] can then be used to serve the app for example like this:
: $ gunicorn "raincloud:app()" : $ gunicorn "raincloud:create_app(base_path='public', secret_key='i_am_a_key')"
*Note* that currently only one worker is supported due to server side session caching. *Note* that currently only one worker is supported due to server side session caching.
@ -66,33 +66,32 @@
services.raincloud = { services.raincloud = {
enable = true; enable = true;
basePath = "/var/lib/raincloud"; basePath = "/var/lib/raincloud";
secretKey = "i_am_a_key";
}; };
} }
#+end_src #+end_src
All configuration options are: All configuration options are:
| Option | Description | Type | Default value | | Option | Description | Type | Default value |
|-----------+-----------------------------------+-------+---------------| |-----------+-----------------------------------+--------+---------------|
| address | Bind address of the server | =str= | =127.0.0.1= | | address | Bind address of the server | =str= | =127.0.0.1= |
| port | Port on which the server listens | =int= | =8000= | | port | Port on which the server listens | =int= | =8000= |
| user | User under which the server runs | =str= | =raincloud= | | user | User under which the server runs | =str= | =raincloud= |
| group | Group under which the server runs | =str= | =raincloud= | | group | Group under which the server runs | =str= | =raincloud= |
| cloudName | Name of the raincloud | =str= | =raincloud= | | cloudName | Name of the raincloud | =str= | =raincloud= |
| basePath | Base path of the raincloud | =str= | | | basePath | Base path of the raincloud | =path= | |
| secretKey | Flask secret key | =str= | | | secretKey | Flask secret key | =str= | |
** Configuration ** Configuration
/raincloud/ provides two configuration options: /raincloud/ provides three configuration options which can be passed to =raincloud.create_app()=:
- CLOUD_NAME :: Name of the raincloud - =base_path= :: Base path of the raincloud
- SECRET_KEY :: Flask secret key - =secret_key= :: Flask secret key
- BASE_PATH :: Base path of the raincloud - =cloud_name= :: Cloud name (default: =raincloud=)
Those can e.g. be stored in a =raincloud_settings.py= file. Look at the file =run.py= to see how to set them.
The filepath can directly be passed to =raincloud.app(/path/to/raincloud_settings.py)= or stored in an environment variable =$RAINCLOUD_SETTINGS=.
Look at the file =raincloud/default_settings.py= to see how to define them.
*** =rc.toml= *** =rc.toml=

View file

@ -20,11 +20,6 @@
cfg = config.services.raincloud; cfg = config.services.raincloud;
raincloud_config = nixpkgs.legacyPackages.${system}.writeText "raincloud_config.py" ''
CLOUD_NAME = "${cfg.cloudName}"
SECRET_KEY = "${cfg.secretKey}"
BASE_PATH = "${cfg.basePath}"
'';
in in
{ {
options.services.raincloud = { options.services.raincloud = {
@ -64,7 +59,7 @@
}; };
basePath = mkOption { basePath = mkOption {
type = types.str; type = types.path;
description = "Base path of the raincloud"; description = "Base path of the raincloud";
}; };
@ -101,7 +96,7 @@
PermissionsStartOnly = true; PermissionsStartOnly = true;
ExecStart = '' ExecStart = ''
${gunicorn}/bin/gunicorn "raincloud:app('${raincloud_config}')" \ ${gunicorn}/bin/gunicorn "raincloud:create_app('${cfg.basePath}', '${cfg.secretKey}', '${cfg.cloudName}')" \
--bind=${cfg.address}:${toString cfg.port} --bind=${cfg.address}:${toString cfg.port}
''; '';
}; };

View file

@ -18,24 +18,14 @@ import crypt
import werkzeug import werkzeug
def app(settings_file=None): def create_app(base_path, secret_key, cloud_name="raincloud"):
# Create app # Create app
app = Flask(__name__) app = Flask(__name__)
app.config["SECRET_KEY"] = secret_key
# Load config
app.config.from_object("raincloud.default_settings")
if settings_file:
app.config.from_pyfile(settings_file)
else:
app.config.from_envvar("RAINCLOUD_SETTINGS", silent=True)
if not app.config["BASE_PATH"] or app.config["BASE_PATH"] == "":
print("No BASE_PATH defined")
exit(1)
# Create handlers # Create handlers
dh = DirectoryHandler(app.config["BASE_PATH"]) dh = DirectoryHandler(base_path)
sh = SessionHandler() sh = SessionHandler()
@app.route("/<directory>", methods=["GET", "POST"]) @app.route("/<directory>", methods=["GET", "POST"])
@ -77,7 +67,7 @@ def app(settings_file=None):
else: else:
return render_template( return render_template(
"authenticate.html", "authenticate.html",
cloud_name=app.config["CLOUD_NAME"], cloud_name=cloud_name,
config=config, config=config,
) )
@ -87,7 +77,7 @@ def app(settings_file=None):
files = dh.get_files(directory) files = dh.get_files(directory)
return render_template( return render_template(
"directory.html", "directory.html",
cloud_name=app.config["CLOUD_NAME"], cloud_name=cloud_name,
config=config, config=config,
files=files, files=files,
) )

View file

@ -1,3 +0,0 @@
CLOUD_NAME = "raincloud"
SECRET_KEY = "dev"
BASE_PATH = "public"

2
run.py
View file

@ -3,5 +3,5 @@
import raincloud import raincloud
if __name__ == "__main__": if __name__ == "__main__":
app = raincloud.app() app = raincloud.create_app("public", "dev")
app.run() app.run()