Skip to content

Commit 3c405fc

Browse files
committed
document 404.sql
thanks again to @wucke13 who implemented the feature
1 parent f909587 commit 3c405fc

File tree

9 files changed

+133
-0
lines changed

9 files changed

+133
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 0.28.0 (unreleased)
44
- Chart component: fix the labels of pie charts displaying too many decimal places.
5+
- You can now create a `404.sql` file anywhere in your SQLPage project to handle requests to non-existing pages. This allows you to create custom 404 pages, or create [nice URLs](https://sql.datapage.app/your-first-sql-website/nginx.sql) that don't end with `.sql`.
56

67
## 0.27.0 (2024-08-17)
78

configuration.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,14 @@ CREATE TEMPORARY TABLE my_temporary_table(
128128
my_temp_column TEXT
129129
);
130130
```
131+
132+
## Migrations
133+
134+
SQLPage allows you to run SQL scripts when the database schema changes, by creating a `sqlpage/migrations` directory.
135+
We have a guide on [how to create migrations](https://sql.datapage.app/your-first-sql-website/migrations.sql).
136+
137+
## Custom URL routes
138+
139+
By default, SQLPage encourages a simple mapping between the URL and the SQL file that is executed.
140+
You can also create custom URL routes by creating [`404.sql` files](http://localhost:8080/your-first-sql-website/custom_urls.sql).
141+
If you need advanced routing, you can also [add a reverse proxy in front of SQLPage](https://sql.datapage.app/reverse_proxy.sql).

examples/official-site/404.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
select 'redirect' as component, '/404.sql' as link where sqlpage.path() <> '/404.sql';
2+
3+
select 'status_code' as component, 404 as status;
4+
select 'http_header' as component, 'no-store, max-age=0' as "Cache-Control";
5+
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
6+
7+
select 'hero' as component,
8+
'Not Found' as title,
9+
'Sorry, we couldn''t find the page you were looking for.' as description_md,
10+
'/your-first-sql-website/not_found.jpg' as image;

examples/official-site/sqlpage/migrations/11_json.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@ This will return a JSON response that looks like this:
6363
}
6464
```
6565
66+
If you want to handle custom API routes, like `POST /api/users/:id`,
67+
you can use
68+
- the [`404.sql` file](/your-first-sql-website/custom_urls.sql) to handle the request despite the URL not matching any file,
69+
- the [`request_method` function](/functions.sql?function=request_method#function) to differentiate between GET and POST requests,
70+
- and the [`path` function](/functions.sql?function=path#function) to extract the `:id` parameter from the URL.
6671
'
6772
);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Insert the status_code component into the component table
2+
INSERT INTO
3+
component (name, description, icon)
4+
VALUES
5+
(
6+
'status_code',
7+
'A simple component to set the HTTP status code for the response. This can be used to indicate the result of the request, such as 200 for success, 404 for not found, or 500 for server error.
8+
9+
The status code should be set according to the HTTP standard status codes.
10+
11+
This component should be used when you need to explicitly set the status code of the HTTP response.
12+
',
13+
'error-404'
14+
);
15+
16+
-- Insert the parameters for the status_code component into the parameter table
17+
INSERT INTO
18+
parameter (
19+
component,
20+
name,
21+
description,
22+
type,
23+
top_level,
24+
optional
25+
)
26+
VALUES
27+
(
28+
'status_code',
29+
'status',
30+
'The HTTP status code to be set for the response. This should be an integer value representing a valid HTTP status code.',
31+
'INTEGER',
32+
TRUE,
33+
FALSE
34+
);
35+
36+
37+
INSERT INTO example (component, description)
38+
VALUES (
39+
'status_code',
40+
'Set the HTTP status code to 404, indicating that the requested resource was not found.
41+
Useful in combination with [`404.sql` files](/your-first-sql-website/custom_urls.sql):
42+
43+
```sql
44+
select ''status_code'' as component, 404 as status;
45+
```
46+
');
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
select 'http_header' as component,
2+
'public, max-age=300, stale-while-revalidate=3600, stale-if-error=86400' as "Cache-Control";
3+
4+
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
5+
6+
select 'hero' as component,
7+
'Custom URLs' as title,
8+
'SQLPage lets you customize responses to URLs that don''t match any file, using `404.sql`.' as description_md,
9+
'not_found.jpg' as image;
10+
11+
select 'text' as component, '
12+
# Handling custom URLs
13+
14+
By default, SQLPage serves the file that matches the URL requested by the client.
15+
If your users enter `https://example.com/about`, SQLPage will serve the file `about/index.sql` in your project.
16+
If you create a file named `about.sql`, SQLPage will serve it when the user requests `https://example.com/about.sql`.
17+
18+
But what if you want to handle URLs that don''t match any file in your project ?
19+
For example, what if you have a blog, and you want nice urls like `example.com/blog/my-trip-to-rome`,
20+
but you don''t want to create a file for each blog post ?
21+
By default, SQLPage would return a sad 404 error if you don''t have a file named `blog/my-trip-to-rome/index.sql`
22+
in your project''s root directory.
23+
24+
But you can customize this behavior by creating a file named `404.sql` in your project.
25+
26+
## The 404.sql file
27+
28+
When SQLPage doesn''t find a file that matches the URL requested by the client, it will serve the file `404.sql` if it exists.
29+
30+
Since v0.28, when SQLPage receives a request for a URL like `https://example.com/a/b/c`, it will look for the file `a/b/c/index.sql` in your project,
31+
and if it doesn''t find it, it will then search for, in order:
32+
- `/a/b/404.sql`
33+
- `/a/404.sql`
34+
- `/404.sql`
35+
36+
## Basic routing example
37+
38+
So, you have a `blog_posts` table in your database, with columns `name`, and `content`.
39+
You want to serve the content of the blog post with id `:id` when the user requests `example.com/blog/:id`.
40+
You can do this by creating a `404.sql` file in the `blog` directory of your project:
41+
42+
```sql
43+
-- blog/404.sql
44+
45+
-- Get the id from the URL
46+
set name = substr(sqlpage.path(), 1+length(''/blog/''));
47+
48+
-- Get the blog post from the database
49+
select ''text'' as component,
50+
content as contents_md
51+
from blog_posts
52+
where name = $name;
53+
```
54+
55+
Now, when a user requests `example.com/blog/my-trip-to-rome`, SQLPage will serve the content of the blog post with name `my-trip-to-rome` from the `blog_posts` table.
56+
' as contents_md;

examples/official-site/your-first-sql-website/nginx.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Your SQLPage instance is now hosted behind a reverse proxy using NGINX. You can
7070
URL rewriting is a powerful feature that allows you to manipulate URLs to make them more readable, search-engine-friendly, and easy to maintain.
7171
In this section, we will cover how to use URL rewriting with SQLPage.
7272

73+
Note that for basic URL rewriting, you can use a simple [`404.sql`](/your-first-sql-website/custom_urls.sql) file to handle custom URLs.
74+
However, for more complex rewriting rules, you can use NGINX's `rewrite` directive.
75+
7376
#### Example: Rewriting `/products/$id` to `/products.sql?id=$id`
7477

7578
Let's say you want your users to access product details using URLs like `/products/123` instead of `/products.sql?id=123`. This can be achieved using the `rewrite` directive in NGINX.
1.22 MB
Loading

src/webserver/http.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ async fn serve_fallback(
454454
for idx in req_path
455455
.rmatch_indices('/')
456456
.map(|(idx, _)| idx + 1)
457+
.take(128) // Limit the number of iterations because lookups can be expensive
457458
.chain(std::iter::once(0))
458459
{
459460
// Remove the trailing substring behind the current `/`, and append `404.sql`.

0 commit comments

Comments
 (0)