Skip to content

Commit 167d049

Browse files
committed
update
1 parent 05a20af commit 167d049

37 files changed

+4411
-1
lines changed

.env.example

Whitespace-only changes.

.eslintrc.js

Whitespace-only changes.

.github/workflows/ci.yml

Whitespace-only changes.

.gitignore

Whitespace-only changes.

.prettierrc

Whitespace-only changes.

README.md

Lines changed: 221 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,221 @@
1-
# cloudpanel-api
1+
# CloudPanel API Structure
2+
3+
```plaintext
4+
cloudpanel-api/
5+
├── .github/ # GitHub specific files
6+
│ └── workflows/ # GitHub Actions workflows
7+
│ └── ci.yml
8+
9+
├── src/ # Source code
10+
│ ├── config/ # Configuration files
11+
│ │ ├── config.js # Main configuration
12+
│ │ └── database.js # Database configuration
13+
│ │
14+
│ ├── controllers/ # Route controllers
15+
│ │ ├── siteController.js
16+
│ │ ├── databaseController.js
17+
│ │ ├── userController.js
18+
│ │ ├── certificateController.js
19+
│ │ └── monitoringController.js
20+
│ │
21+
│ ├── middleware/ # Express middleware
22+
│ │ ├── auth.js # API key authentication
23+
│ │ ├── validation.js # Request validation
24+
│ │ ├── security.js # Security middleware
25+
│ │ ├── logging.js # Logging middleware
26+
│ │ └── errorHandler.js # Error handling
27+
│ │
28+
│ ├── models/ # Database models
29+
│ │ ├── Site.js
30+
│ │ ├── Database.js
31+
│ │ ├── User.js
32+
│ │ ├── Certificate.js
33+
│ │ └── ApiKey.js
34+
│ │
35+
│ ├── routes/ # API routes
36+
│ │ ├── v1/ # API version 1
37+
│ │ │ ├── sites.js
38+
│ │ │ ├── databases.js
39+
│ │ │ ├── users.js
40+
│ │ │ ├── certificates.js
41+
│ │ │ └── monitoring.js
42+
│ │ └── index.js # Route aggregator
43+
│ │
44+
│ ├── services/ # Business logic
45+
│ │ ├── siteService.js
46+
│ │ ├── databaseService.js
47+
│ │ ├── userService.js
48+
│ │ └── monitoringService.js
49+
│ │
50+
│ ├── utils/ # Utility functions
51+
│ │ ├── logger.js # Logging utility
52+
│ │ ├── validation.js # Input validation
53+
│ │ └── helpers.js # Helper functions
54+
│ │
55+
│ ├── metrics/ # Monitoring metrics
56+
│ │ ├── prometheus.js
57+
│ │ └── collectors.js
58+
│ │
59+
│ └── app.js # Express app setup
60+
61+
├── docker/ # Docker related files
62+
│ ├── Dockerfile
63+
│ └── docker-compose.yml
64+
65+
├── config/ # Configuration files
66+
│ ├── prometheus/
67+
│ │ └── prometheus.yml
68+
│ ├── grafana/
69+
│ │ └── datasources.yml
70+
│ └── loki/
71+
│ └── loki-config.yml
72+
73+
├── scripts/ # Utility scripts
74+
│ ├── setup.sh
75+
│ └── backup.sh
76+
77+
├── tests/ # Test files
78+
│ ├── unit/
79+
│ │ ├── controllers/
80+
│ │ ├── services/
81+
│ │ └── models/
82+
│ ├── integration/
83+
│ └── setup.js
84+
85+
├── docs/ # Documentation
86+
│ ├── api/
87+
│ │ └── swagger.yaml
88+
│ ├── setup.md
89+
│ └── monitoring.md
90+
91+
├── .env.example # Example environment variables
92+
├── .eslintrc.js # ESLint configuration
93+
├── .prettierrc # Prettier configuration
94+
├── .gitignore
95+
├── package.json
96+
└── README.md
97+
```
98+
99+
## Key Explanations
100+
101+
### `/src`
102+
Contains all source code for the API. Organized by feature and responsibility.
103+
104+
### `/src/controllers`
105+
Handle HTTP requests and responses. They use services for business logic.
106+
107+
### `/src/services`
108+
Contains business logic and database interactions.
109+
110+
### `/src/middleware`
111+
Express middleware for authentication, logging, etc.
112+
113+
### `/src/models`
114+
Database models and schema definitions.
115+
116+
### `/src/routes`
117+
API route definitions, versioned in `/v1` directory.
118+
119+
### `/config`
120+
External service configurations (Prometheus, Grafana, etc.).
121+
122+
### `/docker`
123+
Docker-related files for containerization.
124+
125+
### `/tests`
126+
Test files organized by type (unit, integration).
127+
128+
### `/docs`
129+
API documentation and setup guides.
130+
131+
# CloudPanel API Documentation
132+
133+
## Authentication
134+
All API requests require an API key passed in the header:
135+
```
136+
X-API-Key: cp_your_api_key_here
137+
```
138+
139+
## Endpoints
140+
141+
### Sites
142+
143+
#### GET /api/v1/sites
144+
Lists all sites.
145+
146+
Response:
147+
```json
148+
{
149+
"sites": [
150+
{
151+
"id": 1,
152+
"domainName": "example.com",
153+
"type": "php",
154+
"rootDirectory": "/home/user/htdocs/example.com"
155+
}
156+
]
157+
}
158+
```
159+
160+
#### POST /api/v1/sites
161+
Create a new site.
162+
163+
Request:
164+
```json
165+
{
166+
"domainName": "newsite.com",
167+
"type": "php",
168+
"rootDirectory": "/home/user/htdocs/newsite.com",
169+
"phpVersion": "8.2"
170+
}
171+
```
172+
173+
### Databases
174+
175+
#### GET /api/v1/databases
176+
Lists all databases.
177+
178+
#### POST /api/v1/databases
179+
Create a new database.
180+
181+
Request:
182+
```json
183+
{
184+
"name": "mydb",
185+
"siteId": 1,
186+
"user": {
187+
"username": "dbuser",
188+
"password": "securepass"
189+
}
190+
}
191+
```
192+
193+
### Users
194+
195+
#### GET /api/v1/users
196+
Lists all users (requires admin API key).
197+
198+
#### POST /api/v1/users
199+
Create a new user (requires admin API key).
200+
201+
Request:
202+
```json
203+
{
204+
"username": "newuser",
205+
"email": "[email protected]",
206+
"role": "user"
207+
}
208+
```
209+
210+
## Rate Limiting
211+
- 100 requests per 15 minutes per IP address
212+
- Status 429 returned when exceeded
213+
214+
## Error Responses
215+
```json
216+
{
217+
"error": "Error message here",
218+
"code": "ERROR_CODE",
219+
"details": {} // Optional additional information
220+
}
221+
```

config/grafana/datasources.yml

Whitespace-only changes.

config/loki/loki-config.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# loki-config.yaml
2+
---
3+
auth_enabled: false
4+
5+
server:
6+
http_listen_port: 3100
7+
8+
ingester:
9+
lifecycler:
10+
address: 127.0.0.1
11+
ring:
12+
kvstore:
13+
store: inmemory
14+
replication_factor: 1
15+
final_sleep: 0s
16+
chunk_idle_period: 5m
17+
chunk_retain_period: 30s
18+
19+
schema_config:
20+
configs:
21+
- from: 2020-05-15
22+
store: boltdb
23+
object_store: filesystem
24+
schema: v11
25+
index:
26+
prefix: index_
27+
period: 24h
28+
29+
storage_config:
30+
boltdb:
31+
directory: /tmp/loki/index
32+
33+
filesystem:
34+
directory: /tmp/loki/chunks
35+
36+
limits_config:
37+
enforce_metric_name: false
38+
reject_old_samples: true
39+
reject_old_samples_max_age: 168h

config/prometheus/prometheus.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# prometheus.yml
2+
---
3+
global:
4+
scrape_interval: 15s
5+
evaluation_interval: 15s
6+
7+
scrape_configs:
8+
- job_name: 'cloudpanel-api'
9+
static_configs:
10+
- targets: ['api:3000']
11+
metrics_path: '/metrics'

docker/Dockerfile

Whitespace-only changes.

docker/docker-compose.yml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
version: '3.8'
2+
3+
services:
4+
api:
5+
build: .
6+
ports:
7+
- "3000:3000"
8+
volumes:
9+
- /home/clp/htdocs/app/data:/data:ro
10+
environment:
11+
- NODE_ENV=production
12+
- DB_PATH=/data/db.sq3
13+
- API_KEY_SALT=${API_KEY_SALT}
14+
restart: unless-stopped
15+
logging:
16+
driver: "json-file"
17+
options:
18+
max-size: "10m"
19+
max-file: "3"
20+
healthcheck:
21+
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
22+
interval: 30s
23+
timeout: 10s
24+
retries: 3
25+
26+
prometheus:
27+
image: prom/prometheus:latest
28+
volumes:
29+
- ./prometheus.yml:/etc/prometheus/prometheus.yml
30+
- prometheus_data:/prometheus
31+
command:
32+
- '--config.file=/etc/prometheus/prometheus.yml'
33+
- '--storage.tsdb.path=/prometheus'
34+
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
35+
- '--web.console.templates=/usr/share/prometheus/consoles'
36+
ports:
37+
- "9090:9090"
38+
restart: unless-stopped
39+
40+
grafana:
41+
image: grafana/grafana:latest
42+
depends_on:
43+
- prometheus
44+
ports:
45+
- "3001:3000"
46+
volumes:
47+
- grafana_data:/var/lib/grafana
48+
environment:
49+
- GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
50+
restart: unless-stopped
51+
52+
loki:
53+
image: grafana/loki:latest
54+
ports:
55+
- "3100:3100"
56+
command: -config.file=/etc/loki/local-config.yaml
57+
volumes:
58+
- ./loki-config.yaml:/etc/loki/local-config.yaml
59+
restart: unless-stopped
60+
61+
promtail:
62+
image: grafana/promtail:latest
63+
volumes:
64+
- /var/log:/var/log
65+
- ./promtail-config.yaml:/etc/promtail/config.yaml
66+
command: -config.file=/etc/promtail/config.yaml
67+
depends_on:
68+
- loki
69+
restart: unless-stopped
70+
71+
volumes:
72+
prometheus_data:
73+
grafana_data:

docs/api/swagger.yaml

Whitespace-only changes.

docs/monitoring.md

Whitespace-only changes.

docs/setup.md

Whitespace-only changes.

package.json

Whitespace-only changes.

scripts/backup.sh

Whitespace-only changes.

scripts/setup.sh

Whitespace-only changes.

src/app.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// src/app.js
2+
import express from 'express';
3+
import { securityMiddleware } from './middleware/security.js';
4+
import { loggingMiddleware } from './middleware/logging.js';
5+
import apiRoutes from './routes/api.js';
6+
import { register } from './metrics/prometheus.js';
7+
8+
const app = express();
9+
10+
// Apply middleware
11+
app.use(securityMiddleware);
12+
app.use(loggingMiddleware);
13+
app.use(express.json());
14+
15+
// API routes
16+
app.use('/api/v1', apiRoutes);
17+
18+
// Metrics endpoint
19+
app.get('/metrics', async (req, res) => {
20+
res.setHeader('Content-Type', register.contentType);
21+
res.send(await register.metrics());
22+
});
23+
24+
export default app;

0 commit comments

Comments
 (0)