mirror of
https://github.com/WGDashboard/WGDashboard-PRW.git
synced 2026-12-01 17:07:41 +00:00
feat: try to build out
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
---
|
||||
description: "Use when writing or editing Python in the WGDashboard rewrite: readability-first coding style, naming, typing, logging, error handling, subprocess and SQLAlchemy rules."
|
||||
applyTo: "src/**/*.py, tests/**/*.py, tools/**/*.py"
|
||||
---
|
||||
# Python style – readability first
|
||||
|
||||
The legacy code (`reference/`) is compact and dense. The rewrite deliberately is not.
|
||||
|
||||
## Rules
|
||||
|
||||
1. No `lambda`, `map()` or `filter()`. Use `for` loops, or a single-level comprehension with at most one simple `if`.
|
||||
2. No nested ternaries. A ternary must fit on one short line.
|
||||
3. One action per line. Store intermediate results in descriptively named variables instead of chaining calls.
|
||||
4. Return early instead of nesting `if/else`. Max ~3 indentation levels inside a function body.
|
||||
5. Descriptive names: `peer`, `interface_name`, `allowed_ips` – never `p`, `x`, `ca`, `iph`.
|
||||
6. Type hints on every parameter and return value.
|
||||
7. Functions return `tuple[bool, value]` to signal success (as in `src/modules/config/config.py`). Do not mix with raising for control flow.
|
||||
8. Utility functions ALWAYS log at DEBUG. Services log INFO on state changes and ERROR on failure. Use `import logging as log`.
|
||||
9. Functions ≲ 50 lines, files ≲ 400 lines. Split by responsibility.
|
||||
10. Enums or module constants instead of magic strings (`PeerStatus`, `Protocol`, `JobAction`).
|
||||
11. Subprocess: list arguments only, never `shell=True`, always a `timeout`, only inside `src/modules/wireguard/cli.py`.
|
||||
12. SQLAlchemy 2.0 typed models (`Mapped[...]`, `mapped_column`). No SQL string formatting.
|
||||
13. Request bodies are validated with pydantic models; respond with `make_resp_obj(status, message, data, http_code)`.
|
||||
14. Keep existing conventions: `#!/bin/env python3` header, snake_case, `'''one-line docstrings'''`.
|
||||
|
||||
## Example
|
||||
|
||||
Legacy:
|
||||
```python
|
||||
"ConnectedPeers": len(list(filter(lambda x: x.status == "running", self.Peers))),
|
||||
```
|
||||
|
||||
Rewrite:
|
||||
```python
|
||||
connected_peer_count = 0
|
||||
|
||||
for peer in peers:
|
||||
if peer.status == PeerStatus.RUNNING:
|
||||
connected_peer_count += 1
|
||||
```
|
||||
|
||||
Helper style:
|
||||
```python
|
||||
def find_available_addresses(network: ipaddress.IPv4Network, used_addresses: set[str], limit: int) -> list[str]:
|
||||
'''Return up to `limit` host addresses in `network` that are not used yet.'''
|
||||
log.debug(f"searching available addresses in {network} (limit {limit})")
|
||||
|
||||
available_addresses: list[str] = []
|
||||
|
||||
for host in network.hosts():
|
||||
address = str(host)
|
||||
|
||||
if address in used_addresses:
|
||||
continue
|
||||
|
||||
available_addresses.append(address)
|
||||
|
||||
if len(available_addresses) >= limit:
|
||||
break
|
||||
|
||||
log.debug(f"found {len(available_addresses)} available addresses")
|
||||
return available_addresses
|
||||
```
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
description: "Use when writing or editing Vue, JavaScript or TypeScript in the WGDashboard admin/client apps: Composition API, component size, API layer, stores, locale usage."
|
||||
applyTo: "src/web/**/*.vue, src/web/**/*.js, src/web/**/*.ts"
|
||||
---
|
||||
# Vue style – readability first
|
||||
|
||||
1. `<script setup>` Composition API only. Pinia stores use the setup syntax of `defineStore`.
|
||||
2. Templates contain no logic beyond property access. Everything else becomes a named `computed`.
|
||||
3. Components ≲ 250 lines. Extract composables (`usePeerSorting`, `usePeerFilter`, `usePeerModals`).
|
||||
4. All HTTP goes through the shared API layer in `src/web/shared` (e.g. `api.peers.update(...)`). No raw `fetch`/`axios` or URL strings in components.
|
||||
5. `async/await` only. No callback-style helpers.
|
||||
6. Explicit `defineProps` (with defaults) and `defineEmits`.
|
||||
7. Descriptive names; no single-letter variables outside trivial `v-for` indexes.
|
||||
8. Keep component **names and folder structure** aligned with `reference/src/static/app` and `reference/src/static/client`, so features can be compared one-to-one.
|
||||
9. User-facing text goes through the locale helper with English fallback.
|
||||
|
||||
## Example
|
||||
|
||||
Legacy pattern:
|
||||
```vue
|
||||
<span>{{ peers.filter(p => p.status === 'running').length }} / {{ peers.length }}</span>
|
||||
```
|
||||
|
||||
Rewrite:
|
||||
```vue
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
|
||||
const props = defineProps({
|
||||
peers: { type: Array, default: () => [] },
|
||||
})
|
||||
|
||||
const connectedPeerCount = computed(() => {
|
||||
let count = 0
|
||||
|
||||
for (const peer of props.peers) {
|
||||
if (peer.status === 'running') {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span>{{ connectedPeerCount }} / {{ props.peers.length }}</span>
|
||||
</template>
|
||||
```
|
||||
Reference in New Issue
Block a user