-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (102 loc) · 3.81 KB
/
Copy pathmain.py
File metadata and controls
123 lines (102 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
NexaNote — Point d'entrée principal
Lance les deux serveurs en parallèle :
- Serveur WebDAV sur le port 8765 (pour Nextcloud, NAS, rclone…)
- API REST sur le port 8766 (pour l'app Flutter)
Usage :
python main.py
python main.py --webdav-port 8765 --api-port 8766
python main.py --host 0.0.0.0 --data-dir ~/nexanote-data
python main.py --api-only
python main.py --webdav-only
"""
import argparse
import logging
import threading
from pathlib import Path
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger("nexanote")
def run_webdav(host, port, data_dir, username, password):
from nexanote.sync.server import run_server
run_server(
host=host,
port=port,
data_dir=data_dir,
username=username,
password=password,
)
def run_api(host, port, data_dir):
import uvicorn
from nexanote.storage import create_store, run_migration
from nexanote.api.routes import create_app
data_dir = Path(data_dir)
data_dir.mkdir(parents=True, exist_ok=True)
# Auto-migrate any pre-v1 SQLite database before opening the file store.
report = run_migration(data_dir)
if report.ran:
logger.info(report.summary())
if report.backup_path:
logger.info(f"Legacy SQLite backup kept at: {report.backup_path}")
# `create_store` reads the on-disk mode marker / NEXANOTE_STORAGE_MODE
# env var so users can opt into the plain-Markdown backend.
db = create_store(data_dir)
app = create_app(db)
logger.info(f"API REST démarrée sur http://{host}:{port}")
uvicorn.run(app, host=host, port=port, log_level="warning")
def main():
parser = argparse.ArgumentParser(
description="NexaNote Backend",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--webdav-port", type=int, default=8765)
parser.add_argument("--api-port", type=int, default=8766)
parser.add_argument("--data-dir", type=Path, default=Path.home() / ".nexanote")
parser.add_argument("--username", default="nexanote")
parser.add_argument("--password", default="nexanote")
parser.add_argument("--api-only", action="store_true")
parser.add_argument("--webdav-only", action="store_true")
args = parser.parse_args()
print("""
╔══════════════════════════════════════════╗
║ NexaNote Backend v1.0.0 ║
║ file-based storage (Markdown) ║
╚══════════════════════════════════════════╝
""")
if not args.api_only:
print(f" WebDAV → http://{args.host}:{args.webdav_port}/")
if not args.webdav_only:
print(f" API → http://{args.host}:{args.api_port}/")
print(f" Data → {args.data_dir}")
print(f" User → {args.username}")
print()
threads = []
if not args.api_only:
t_webdav = threading.Thread(
target=run_webdav,
args=(args.host, args.webdav_port, args.data_dir, args.username, args.password),
daemon=True,
name="webdav",
)
threads.append(t_webdav)
if not args.webdav_only:
t_api = threading.Thread(
target=run_api,
args=(args.host, args.api_port, args.data_dir),
daemon=True,
name="api",
)
threads.append(t_api)
for t in threads:
t.start()
try:
for t in threads:
t.join()
except KeyboardInterrupt:
print("\nArrêt du serveur NexaNote.")
if __name__ == "__main__":
main()