9292 // Request validation
9393 query {
9494 message: str,
95- model? : str = "gpt-3.5-turbo"
95+ model: str = "gpt-3.5-turbo"
9696 }
9797
98- // Response handling
99- response {
100- content: str,
101- tokens: int
102- }
103-
104- // Route handler
105- fn handle(req) -> response {
106- let answer = prompt req.query.message => req.query.model;
107- return {
108- content: answer,
109- tokens: len(answer)
110- };
111- }
98+ let answer = prompt query.message => {
99+ model: query.model
100+ };
101+ return { answer };
112102 }
113103 filename: routes.ai
114104 - title : Validator
115105 description : " Built-in request validation with type checking and custom validation rules for secure API endpoints."
116106 codeDemos :
117107 - code : |
118- route POST " /api/user" {
108+ post /api/user {
119109 // Request body validation
110+ @json
120111 body {
121- username: str {
122- min_length: 3,
123- max_length: 20,
124- pattern: "^[a-zA-Z0-9_]+$"
125- },
126- email: str {
127- format: "email"
128- },
129- age: int {
130- minimum: 18
131- }
112+ @string(min_length: 3, max_length: 20, pattern: "^[a-zA-Z0-9_]+$")
113+ username: str,
114+ @format(type="email")
115+ email: str,
116+ @number(min=18)
117+ age: int,
132118 }
133119
134- fn handle(req) -> str {
135- // Body is already validated
136- db.users.create(req.body);
137- return "User created successfully";
138- }
120+ // Body is already validated
121+ db.users.create(req.body);
122+ return "User created successfully";
139123 }
140124 filename: validator.ai
141125 - title : OpenAPI
142126 description : " Automatic OpenAPI documentation generation from your route definitions with full type information and examples."
143127 codeDemos :
144128 - code : |
145- // OpenAPI configuration
146- openapi {
147- title: "Chat API",
148- version: "1.0.0",
149- description: "AI-powered chat API"
150- }
151-
152- route POST "/api/chat" {
129+ post /api/chat {
153130 // This will be included in OpenAPI docs
154131 @doc "Create a new chat message"
155132 @tag "Chat"
@@ -168,34 +145,82 @@ web:
168145 }
169146 }
170147 filename: openapi.ai
171- - title : Effortless Social Login
148+ - title : Effortless Auth and Social Login
172149 description : " Integrate social login providers with minimal configuration and type-safe authentication flows."
173150 codeDemos :
174151 - code : |
175- // Configure auth providers
176- auth {
177- providers: {
178- github: {
179- client_id: env.GITHUB_CLIENT_ID,
180- client_secret: env.GITHUB_CLIENT_SECRET
181- },
182- google: {
183- client_id: env.GOOGLE_CLIENT_ID,
184- client_secret: env.GOOGLE_CLIENT_SECRET
185- }
186- },
187- callbacks: {
188- async on_success(user) {
189- await db.users.upsert(user);
190- return redirect("/dashboard");
152+ // JWT auth
153+ @auth
154+ get /ask {
155+ query {
156+ @string(min_len=5, max_len=50)
157+ question: str
158+ }
159+
160+ ai fn ask_llm(question: str) -> str {
161+ return prompt question;
162+ }
163+ let answer = ask_llm(query.question);
164+ return answer;
165+ }
166+ filename: jwt_auth.ai
167+ - code : |
168+ // Username/Password auth
169+ @basic_auth
170+ post /guess {
171+ body {
172+ @number(min=1, max=100)
173+ magic: int = 1
174+ }
175+
176+ header.path = "/guess";
177+ let msg = "Try again!";
178+ if body.magic == 42 {
179+ msg = "You guessed it!";
180+ }
181+ return {
182+ body: msg,
183+ headers: {
184+ path: "/guess",
191185 }
186+ };
187+ }
188+ filename: basic_auth.ai
189+ - code : |
190+ @sso(provider="google")
191+ get /auth/google {
192+ let url = sso.authority_url();
193+ print(url);
194+ return temporary_redirect(target=url);
195+ }
196+
197+ @sso(provider="google")
198+ get /auth/google/callback {
199+ query {
200+ state: str,
201+ code: str,
192202 }
203+
204+ let user_info = sso.verify(state, code);
205+ print(user_info);
206+ return user_info;
193207 }
208+ filename: google_login.ai
209+ - code : |
210+ [auth.jwt]
211+ secret = "$JWT_SECRET"
212+ expiration = 3600
213+
214+ [auth.basic]
215+ username = "admin"
216+ password = "123456"
194217
195- // Auth routes are automatically created
196- // /auth/github
197- // /auth/google
198- filename: auth.ai
218+ [sso.google]
219+ client_id = "123"
220+ client_secret = "abc"
221+ redirect_url = "http://localhost:8080/auth/google/callback"
222+ scopes = ["email"]
223+ filename: project.toml
199224
200225language :
201226 - title : Enum
0 commit comments