|
1 | 1 | # Project |
| 2 | + |
| 3 | +Building web applications is rarely a single-file endeavor. AIScript provides a structured project system to help you organize your code efficiently. |
| 4 | + |
| 5 | +## Creating a New Project |
| 6 | + |
| 7 | +Let's create a new project with the AIScript CLI: |
| 8 | + |
| 9 | +```bash |
| 10 | +$ aiscript new my-project |
| 11 | +``` |
| 12 | + |
| 13 | +This command creates a new project named `my-project` in the current directory. |
| 14 | + |
| 15 | +```bash |
| 16 | +$ cd my-project |
| 17 | + |
| 18 | +$ tree |
| 19 | +my-project |
| 20 | +├── lib/ |
| 21 | +├── migrations/ |
| 22 | +├── routes/ |
| 23 | +└── project.toml |
| 24 | +``` |
| 25 | + |
| 26 | +## Project Structure |
| 27 | + |
| 28 | +AIScript follows the **"convention over configuration"** principle. This means that by following standard directory structures, you can minimize the amount of configuration needed. The default project structure is as follows: |
| 29 | + |
| 30 | +### project.toml |
| 31 | + |
| 32 | +The `project.toml` file is a **TOML** configuration file for your project: |
| 33 | + |
| 34 | +```toml |
| 35 | +[project] |
| 36 | +name = "my-project" |
| 37 | +description = "My project" |
| 38 | +version = "0.1.0" |
| 39 | + |
| 40 | +[apidoc] |
| 41 | +enabled = true |
| 42 | +type = "redoc" |
| 43 | +path = "/redoc" |
| 44 | + |
| 45 | +[network] |
| 46 | +host = "0.0.0.0" |
| 47 | +port = 8000 |
| 48 | +``` |
| 49 | + |
| 50 | +You can configure project metadata, API documentation settings, network options, and more. For detailed configuration options, see [Configuration Reference](/reference/configuration/general). |
| 51 | + |
| 52 | +### routes/ |
| 53 | + |
| 54 | +The `routes` directory contains route definition files. Each file in this directory can define multiple routes: |
| 55 | + |
| 56 | +``` |
| 57 | +routes/ |
| 58 | +├── auth.ai # Authentication routes |
| 59 | +├── users.ai # User management routes |
| 60 | +└── products.ai # Product routes |
| 61 | +``` |
| 62 | + |
| 63 | +Example route file (`routes/users.ai`): |
| 64 | + |
| 65 | +```js |
| 66 | +get /api/users { |
| 67 | + // Return all users |
| 68 | +} |
| 69 | + |
| 70 | +get /api/users/<id:int> { |
| 71 | + // Return user by ID |
| 72 | +} |
| 73 | + |
| 74 | +post /api/users { |
| 75 | + // Create a new user |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### lib/ |
| 80 | + |
| 81 | +The `lib` directory contains reusable library code. You can organize your application logic into modules that can be imported by your routes: |
| 82 | + |
| 83 | +``` |
| 84 | +lib/ |
| 85 | +├── auth.ai # Authentication utilities |
| 86 | +├── db.ai # Database helpers |
| 87 | +└── validation.ai # Validation functions |
| 88 | +``` |
| 89 | + |
| 90 | +Example library file (`lib/auth.ai`): |
| 91 | + |
| 92 | +```js |
| 93 | +fn validate_token(token: str) -> bool { |
| 94 | + // Token validation logic |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +fn get_user_from_token(token: str) -> object { |
| 99 | + // Extract user information from token |
| 100 | + return { id: 1, name: "John" }; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Using library code in routes: |
| 105 | + |
| 106 | +```js |
| 107 | +// routes/auth.ai |
| 108 | +use lib.auth; |
| 109 | + |
| 110 | +post /api/login { |
| 111 | + // Login logic |
| 112 | +} |
| 113 | + |
| 114 | +get /api/profile { |
| 115 | + let token = header.Authorization; |
| 116 | + if !auth.validate_token(token) { |
| 117 | + return response(status_code=401, body={"error": "Invalid token"}); |
| 118 | + } |
| 119 | + |
| 120 | + let user = auth.get_user_from_token(token); |
| 121 | + return user; |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### migrations/ |
| 126 | + |
| 127 | +The `migrations` directory contains SQL migration files for database schema changes: |
| 128 | + |
| 129 | +``` |
| 130 | +migrations/ |
| 131 | +├── 001_create_users.sql |
| 132 | +├── 002_create_products.sql |
| 133 | +└── 003_add_user_roles.sql |
| 134 | +``` |
| 135 | + |
| 136 | +Example migration file (`migrations/001_create_users.sql`): |
| 137 | + |
| 138 | +```sql |
| 139 | +-- Up migration |
| 140 | +CREATE TABLE users ( |
| 141 | + id SERIAL PRIMARY KEY, |
| 142 | + name VARCHAR(255) NOT NULL, |
| 143 | + email VARCHAR(255) UNIQUE NOT NULL, |
| 144 | + password VARCHAR(255) NOT NULL, |
| 145 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 146 | +); |
| 147 | +``` |
| 148 | + |
| 149 | +## Project Commands |
| 150 | + |
| 151 | +The AIScript CLI provides several commands to manage your project: |
| 152 | + |
| 153 | +```bash |
| 154 | +# Start the development server |
| 155 | +$ aiscript serve |
| 156 | + |
| 157 | +# Run migrations |
| 158 | +$ aiscript migrate up |
| 159 | + |
| 160 | +# Roll back migrations |
| 161 | +$ aiscript migrate down |
| 162 | + |
| 163 | +``` |
| 164 | + |
| 165 | +For more information, refer to the [Command Reference](/reference/cli). |
| 166 | + |
| 167 | +## Environment Variables |
| 168 | + |
| 169 | +AIScript supports environment variables for configuration. Create a `.env` file in your project root: |
| 170 | + |
| 171 | +``` |
| 172 | +# .env |
| 173 | +DATABASE_URL=postgres://user:password@localhost/mydb |
| 174 | +JWT_SECRET=your-secret-key |
| 175 | +PORT=8080 |
| 176 | +``` |
| 177 | + |
| 178 | +Access environment variables in your code: |
| 179 | + |
| 180 | +```js |
| 181 | +let port = $PORT; |
| 182 | +let db_url = $DATABASE_URL; |
| 183 | +``` |
| 184 | + |
| 185 | +## Project Configuration |
| 186 | + |
| 187 | +The `project.toml` file supports various configuration options: |
| 188 | + |
| 189 | +### General Configuration |
| 190 | + |
| 191 | +```toml |
| 192 | +[project] |
| 193 | +name = "my-project" |
| 194 | +description = "My awesome project" |
| 195 | +version = "1.0.0" |
| 196 | +``` |
| 197 | + |
| 198 | +### Network Configuration |
| 199 | + |
| 200 | +```toml |
| 201 | +[network] |
| 202 | +host = "0.0.0.0" |
| 203 | +port = 8080 |
| 204 | +``` |
| 205 | + |
| 206 | +### Database Configuration |
| 207 | + |
| 208 | +```toml |
| 209 | +[database] |
| 210 | +url = "postgres://user:password@localhost/mydb" |
| 211 | +pool_size = 10 |
| 212 | +``` |
| 213 | + |
| 214 | +### API Documentation |
| 215 | + |
| 216 | +```toml |
| 217 | +[apidoc] |
| 218 | +enabled = true |
| 219 | +type = "swagger" |
| 220 | +path = "/docs" |
| 221 | +title = "My API" |
| 222 | +version = "1.0.0" |
| 223 | +``` |
| 224 | + |
| 225 | +### Middleware Configuration |
| 226 | + |
| 227 | +```toml |
| 228 | +[middleware] |
| 229 | +global = ["lib/middleware.ai:logger", "lib/middleware.ai:error_handler"] |
| 230 | + |
| 231 | +[middleware.cors] |
| 232 | +allowed_origins = ["http://localhost:3000"] |
| 233 | +allowed_methods = ["GET", "POST", "PUT", "DELETE"] |
| 234 | +``` |
| 235 | + |
| 236 | +## Project Structure Best Practices |
| 237 | + |
| 238 | +1. **Route Organization**: Group related routes in the same file (e.g., `users.ai` for all user-related routes). |
| 239 | +2. **Modular Code**: Extract reusable logic to the `lib` directory. |
| 240 | +3. **Environment Configuration**: Use environment variables for configuration that changes between environments. |
| 241 | +4. **Migration Versioning**: Number migrations sequentially to ensure they're applied in the correct order. |
| 242 | +5. **Middleware Separation**: Define middleware in dedicated files for clarity. |
| 243 | + |
| 244 | +By following these conventions and best practices, you can build well-organized, maintainable AIScript applications that scale with your needs. |
0 commit comments