Commit 13d6f3f1 authored by jan.koester's avatar jan.koester
Browse files

blogi editor: setup wizard, server administration and config helper

The blogi editor is a product of its own that shares the code with
blogi, so it gets its own policy "Blogi Editor: Server Administration",
its own authdb client blogi-editor-<host>, pages, texts and log names.

- editor/src/editorconfig.*: the WEBEDIT section; HTTP, DATABASE,
  PREFIX, LANGUAGE and AUTHDB are managed, certificate, plugin, web
  page and widget directories stay file-only, other keys (MEDIADB)
  are kept.
- editor/src/editor_admin.*: config helper handlers, the /_server/
  profile and the wizard product (listener, prefix, language, tested
  database, authdb client and policy).
- main.cpp: the editor runs under the config helper (rollback,
  maintenance mode, SIGHUP restart), binds before it reports ready;
  without a config file the wizard starts on port 8090.
- /_server/ also behind the editor's PREFIX (relative API paths, cookie
  path <prefix>/_server).
- run.sh starts the wizard instead of copying docker-config.yaml, which
  had a BLOGI root the editor cannot start with.

Shared code generalised for this: helper::setConfigHandlers (with the
product name), serveradmin::Profile (page, prefix, policy, sources,
listener, name, user), setup::Product with checkAuthdbLogin and
createAuthdbClient(Policy), serverconfig::patchBlocks, writeChecked and
yamlQuote. blogi behaves as before. German texts added.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 6a2819d9
Loading
Loading
Loading
Loading
+18 −0
Changes for README.md: 18 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -50,3 +50,21 @@ domain.
  (`BLOGI_MAINTENANCE_RETRY` seconds, `0` = never).
- **Signals:** SIGHUP restarts blogi; SIGTERM stops it together with the domain workers.
- **`--no-helper`:** runs without all of this.

## blogi editor

The blogi editor (`blogi-editor`, `editor/`) is a product of its own that shares
the code above with blogi:

- **Without its config file** (`/etc/blogi-editor/config.yaml`) it starts a setup
  wizard on port 8090 (`--setup-port`). The wizard asks for:
  - the listener and the URL prefix
  - the language
  - the database (with a test)
  - authdb, where it creates the client `blogi-editor-<host>` and the policy
    **Blogi Editor: Server Administration**, which is not blogi's policy
- **Server administration** at `<prefix>/_server/` covers HTTP, the prefix, the
  language, the database and the authdb logins (`WEBEDIT/AUTHDB`). Saving
  replaces only those blocks; other keys such as `MEDIADB` stay.
- **File-only fields:** certificate, plugin, web page and widget directories.
- **Config helper:** rollback and a maintenance mode, as with blogi.
+14 −0
Changes for editor/CMakeLists.txt: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -46,6 +46,20 @@ add_executable(blogi-editor
    src/htmlimport.cpp
)

# setup wizard, server administration (/_server/) and config helper: the editor's own
# parts here, the machinery shared with blogi (POSIX only)
if(NOT WIN32)
    target_sources(blogi-editor PRIVATE src/editorconfig.cpp src/editor_admin.cpp)
    if(NOT TARGET blogidev)
        target_sources(blogi-editor PRIVATE
            ${CMAKE_CURRENT_SOURCE_DIR}/../src/helper.cpp
            ${CMAKE_CURRENT_SOURCE_DIR}/../src/serverconfig.cpp
            ${CMAKE_CURRENT_SOURCE_DIR}/../src/serveradmin.cpp
            ${CMAKE_CURRENT_SOURCE_DIR}/../src/setup.cpp
        )
    endif()
endif()

if(TARGET LibArchive::LibArchive)
    target_link_libraries(blogi-editor PRIVATE LibArchive::LibArchive)
else()
+5 −11
Changes for editor/run.sh: 5 added lines, 11 removed lines.
Original line number Diff line number Diff line
#!/bin/sh

if [ ! -f /etc/blogi-editor/config.yaml ]; then
    if [ ! -d /etc/blogi-editor ]; then
        mkdir /etc/blogi-editor
    fi
    if [ -f /usr/local/share/blogi-editor/config.yaml ]; then
        cp /usr/local/share/blogi-editor/config.yaml /etc/blogi-editor/config.yaml
    fi
fi

exec /usr/local/bin/blogi-editor -F true -c "yaml:/etc/blogi-editor/config.yaml"
# Without /etc/blogi-editor/config.yaml the editor starts its setup wizard on port 8090
# (setup code in the container log and in /etc/blogi-editor/setup-code); afterwards its
# server administration at /_server/ changes the configuration.
mkdir -p /etc/blogi-editor /var/lib/blogi-editor
exec /usr/local/bin/blogi-editor -c "yaml:/etc/blogi-editor/config.yaml" --setup-port 8090
+160 −0
Changes for editor/src/editor_admin.cpp: 160 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2026, Jan Koester jan.koester@gmx.net
 * All rights reserved. (BSD-3-Clause, see LICENSE.txt)
 *******************************************************************************/
#include "editor_admin.h"

#include "editor_admin_pages.h"
#include "editorconfig.h"
#include "helper.h"
#include "i18n.h"

#include <json-c/json.h>

#include <filesystem>
#include <iostream>
#include <map>
#include <mutex>

namespace webedit::admin {

namespace ec = webedit::editorconfig;

namespace {

std::string jstr(json_object *o, const char *k, const std::string &def = "") {
    json_object *v = nullptr;
    if (!o || !json_object_object_get_ex(o, k, &v) || !json_object_is_type(v, json_type_string)) return def;
    return json_object_get_string(v);
}
int jint(json_object *o, const char *k, int def) {
    json_object *v = nullptr;
    if (!o || !json_object_object_get_ex(o, k, &v) || !json_object_is_type(v, json_type_int)) return def;
    return json_object_get_int(v);
}
bool jbool(json_object *o, const char *k, bool def) {
    json_object *v = nullptr;
    if (!o || !json_object_object_get_ex(o, k, &v) || !json_object_is_type(v, json_type_boolean)) return def;
    return json_object_get_boolean(v);
}
json_object *jget(json_object *o, const char *k) {
    json_object *v = nullptr;
    return o && json_object_object_get_ex(o, k, &v) ? v : nullptr;
}
std::string trim(std::string s) {
    s.erase(0, s.find_first_not_of(" \t\r\n"));
    s.erase(s.find_last_not_of(" \t\r\n") + 1);
    return s;
}

blogi::setup::Policy policy() {
    return {ec::POLICY, ec::POLICY_NAME, ec::POLICY_DESCRIPTION};
}

// the editor's part of "finish": listener, database, authdb -> config.yaml
std::string finish(const blogi::setup::Context &ctx, json_object *b, const std::string &lang,
                   std::vector<std::string> &warnings, json_object *out) {
    ec::Settings s = ec::defaults();
    json_object *jh = jget(b, "http");
    s.httpBind = trim(jstr(jh, "bind", "0.0.0.0"));
    s.httpPort = jint(jh, "port", 8090);
    if (jbool(jh, "tls", true)) {
        s.sslCert = ctx.certFile;
        s.sslKey = ctx.keyFile;
    }
    s.prefix = trim(jstr(b, "prefix"));
    if (s.prefix == "/") s.prefix.clear();
    s.language = jstr(b, "language", "EN");
    json_object *jd = jget(b, "database");
    s.dbDriver = trim(jstr(jd, "driver", "sqlite"));
    s.dbConnection = trim(jstr(jd, "connection"));

    json_object *ja = jget(b, "authdb");
    const std::string url = trim(jstr(ja, "url")), user = trim(jstr(ja, "username")), pw = jstr(ja, "password");
    const bool verify = jbool(ja, "tls_verify", true);
    s.authdb.push_back({url, "-", "-", "", verify});
    std::string err = ec::validate(s);
    if (!err.empty()) return err;

    if (s.dbDriver == "sqlite") {
        std::error_code e;
        std::filesystem::path p(s.dbConnection);
        if (p.has_parent_path()) std::filesystem::create_directories(p.parent_path(), e);
    }
    err = blogi::serverconfig::testDatabase(s.dbDriver, s.dbConnection);
    if (!err.empty()) return blogi::tr(lang, "Database:") + " " + err;
    if (!blogi::setup::checkAuthdbLogin(lang, url, verify, user, pw, err)) return err;
    std::string client, secret;
    if (!blogi::setup::createAuthdbClient(lang, url, verify, user, pw, "blogi-editor-", ctx.hostname, policy(), client,
                                          secret, warnings, err))
        return err;
    s.authdb[0].clientName = client;
    s.authdb[0].clientSecret = secret;
    if (!ec::writeFile(ctx.configPath, ec::patch("", s), err)) return err;
    json_object_object_add(out, "prefix", json_object_new_string(s.prefix.c_str()));
    return "";
}

} // namespace

namespace {
std::string gConfigFile;
}

void setConfigFile(const std::string &path) { gConfigFile = path; }
const std::string &configFile() { return gConfigFile; }

void installConfigHandlers() {
    blogi::helper::ConfigHandlers h;
    h.toJson = [](const std::string &path) { return ec::toJson(ec::Settings::load(path), false); };
    h.apply = [](const std::string &path, const std::string &previous, json_object *changes,
                 std::string &content) -> std::string {
        ec::Settings s = ec::Settings::load(path);
        std::string err = ec::applyJson(s, changes, /*allowReadOnly=*/false);
        if (err.empty()) err = ec::validate(s);
        if (err.empty()) content = ec::patch(previous, s);
        return err;
    };
    h.write = [](const std::string &path, const std::string &content, std::string &err) {
        return ec::writeFile(path, content, err);
    };
    h.readOnly = ec::readOnlyFields();
    h.product = "blogi-editor";
    blogi::helper::setConfigHandlers(std::move(h));
}

const blogi::serveradmin::Profile &profile(const std::string &prefix) {
    static std::mutex mtx;
    static std::map<std::string, blogi::serveradmin::Profile> profiles;
    std::lock_guard<std::mutex> lk(mtx);
    auto it = profiles.find(prefix);
    if (it != profiles.end()) return it->second;
    blogi::serveradmin::Profile p;
    p.page = SETTINGS_PAGE;
    p.prefix = prefix;
    p.name = "blogi-editor";
    p.runUser = "";   // the editor runs as the user it was started as
    p.policyId = ec::POLICY;
    p.policyName = ec::POLICY_NAME;
    p.policyDescription = ec::POLICY_DESCRIPTION;
    p.sources = [](const std::string &path) { return ec::Settings::load(path).authdb; };
    p.listener = [](const std::string &path) {
        ec::Settings s = ec::Settings::load(path);
        return blogi::serveradmin::Listener{s.httpBind, s.httpPort, s.sslCert, s.sslKey, s.sslPassword};
    };
    return profiles.emplace(prefix, std::move(p)).first->second;
}

const blogi::setup::Product &product() {
    static const blogi::setup::Product p = [] {
        blogi::setup::Product e;
        e.name = "blogi-editor";
        e.page = SETUP_PAGE;
        e.script = SETUP_JS;
        e.finish = finish;
        return e;
    }();
    return p;
}

} // namespace webedit::admin
+32 −0
Changes for editor/src/editor_admin.h: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2026, Jan Koester jan.koester@gmx.net
 * All rights reserved. (BSD-3-Clause, see LICENSE.txt)
 *
 * The blogi editor's setup wizard, server administration (/_server/) and config
 * helper. The machinery is shared with blogi (../src/setup.h, serveradmin.h,
 * helper.h); what is the editor's own -- its config file (editorconfig.h), its
 * pages, its authdb client blogi-editor-<host> and its policy "Blogi Editor:
 * Server Administration" -- lives here.
 *******************************************************************************/
#pragma once

#include "serveradmin.h"
#include "setup.h"

#include <string>

namespace webedit::admin {

    // makes the config helper read and write the editor's config file
    void installConfigHandlers();

    // the config file itself (set by main before anything else)
    void setConfigFile(const std::string &path);
    const std::string &configFile();

    // the /_server/ profile; prefix: the editor's URL prefix (WEBEDIT/PREFIX)
    const blogi::serveradmin::Profile &profile(const std::string &prefix);

    // the setup wizard's editor part
    const blogi::setup::Product &product();
}
Loading