Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/official-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ Add `theme=light` or `theme=dark` to a component reference URL to preview its
live examples using SQLPage's stock theme. For example, visit
`/component.sql?component=form&theme=light`.

## Function reference

Edit each function in [`sqlpage/content/functions/`](sqlpage/content/functions/).
The filename matches the function name and determines its reference URL.
Use YAML front matter for the catalog summary, return type, and ordered
`parameters` list. Set `required: true` on required arguments.
Write the overview and examples in Markdown below the front matter.

## Component reference

Document Handlebars component parameters in the first comment of `sqlpage/templates/<component>.handlebars`. Start with a short summary paragraph, then give the complete overview, followed by `## Top-level parameters` and `## Row-level parameters`. Omit empty sections. For the nine components implemented in Rust, put the overview and parameter list in that component's `README.md` under `reference/components/` using the same structure.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
icon: key
since: 0.7.2
category: identity-and-security
summary: Read the password sent with HTTP Basic authentication.
return_type: TEXT
---

Returns the password from the [Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) header of the request.
If the header is absent, SQLPage sends an HTTP Basic challenge. Browsers normally respond by prompting for credentials.

### Example

```sql
SELECT 'authentication' AS component,
(SELECT password_hash from users where name = sqlpage.basic_auth_username()) AS password_hash,
sqlpage.basic_auth_password() AS password;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
icon: user
since: 0.7.2
category: identity-and-security
summary: Read the username sent with HTTP Basic authentication.
return_type: TEXT
---

Returns the username from the [Basic Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) header of the request.
If the header is not present, this function raises an authorization error that will prompt the user to enter their credentials.

### Example

```sql
SELECT 'authentication' AS component,
(SELECT password_hash from users where name = sqlpage.basic_auth_username()) AS password_hash,
sqlpage.basic_auth_password() AS password;
```
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
INSERT INTO
sqlpage_functions (
"name",
"introduced_in_version",
"icon",
"description_md"
)
VALUES
(
'client_ip',
'0.33.0',
'network',
'Returns the IP address of the client making the HTTP request.
---
icon: network
since: 0.33.0
category: request-data
summary: Get the IP address of the client making the request.
---

Returns the IP address of the client making the HTTP request.

### Example

Expand All @@ -28,19 +23,17 @@ The function returns:

When [running behind a reverse proxy](/your-first-sql-website/nginx.sql) (e.g., Nginx, Apache, Cloudflare):
- This function will return the IP address of the reverse proxy, not the actual client
- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header(''x-forwarded-for'')` or `sqlpage.header(''x-real-ip'')`
- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header('x-forwarded-for')` or `sqlpage.header('x-real-ip')`
- The exact header name depends on your reverse proxy configuration

Example with reverse proxy:
```sql
-- Choose the appropriate header based on your setup
select coalesce(
sqlpage.header(''x-forwarded-for''),
sqlpage.header(''x-real-ip''),
sqlpage.header('x-forwarded-for'),
sqlpage.header('x-real-ip'),
sqlpage.client_ip()
) as real_client_ip;
```

For security-critical applications, ensure your reverse proxy is properly configured to set and validate these headers.
'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
icon: folder-cog
since: 0.42.0
category: server-and-runtime
summary: Locate the directory for SQLPage configuration and templates.
---

Returns the configuration directory where SQLPage looks for `sqlpage.json`, templates, and migrations.

### Example

```sql
SELECT 'text' AS component;
SELECT 'Configuration files are in ' AS contents;
SELECT sqlpage.configuration_directory() as contents, true as code;
```

#### Result

Configuration files are in `/home/user/my_sqlpage_website/sqlpage`

#### Notes

The configuration directory is where SQLPage looks for:
- `sqlpage.json` (the configuration file)
- `templates/` (custom component templates)
- `migrations/` (database migration files)

By default, it is `./sqlpage` relative to the current working directory, but it can be changed using:
- the `--config-dir` command line argument
- the `SQLPAGE_CONFIGURATION_DIRECTORY` or `CONFIGURATION_DIRECTORY` environment variable

This function is useful when you need to reference configuration-related files in your SQL code.
39 changes: 39 additions & 0 deletions examples/official-site/sqlpage/content/functions/cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
icon: cookie
since: 0.7.1
category: request-data
summary: Read a named cookie from the incoming request.
return_type: TEXT
parameters:
- name: name
type: TEXT
required: true
description: The name of the cookie to read.
---

Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request.
Returns the value of the cookie as text, or NULL if the cookie is not present.

Cookies can be set using the [cookie component](documentation.sql?component=cookie#component).

### Example

#### Set a cookie

Set a cookie called `username` to greet the user by name every time they visit the page:

```sql
select 'cookie' as component, 'username' as name, :username as value;

SELECT 'form' as component;
SELECT 'username' as name, 'text' as type;
```

#### Read a cookie

Read a cookie called `username` and greet the user by name:

```sql
SELECT 'text' as component,
'Hello, ' || sqlpage.cookie('username') || '!' as contents;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
icon: folder-question
since: 0.11.0
category: server-and-runtime
summary: Get the working directory of the SQLPage server process.
---

Returns the [current working directory](https://en.wikipedia.org/wiki/Working_directory) of the SQLPage server process.

### Example

```sql
SELECT 'text' AS component;
SELECT 'Currently running from ' AS contents;
SELECT sqlpage.current_working_directory() as contents, true as code;
```

#### Result

Currently running from `/home/user/my_sqlpage_website`

#### Notes

The current working directory is the directory from which the SQLPage server process was started.
By default, this is also the directory from which `.sql` files are loaded and served.
Setting [`web_root`](https://github.com/sqlpage/SQLPage/blob/main/configuration.md) changes where SQLPage serves files. It does not change the process working directory. Use [`sqlpage.web_root()`](?function=web_root) to get the directory actually being served.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
icon: variable
since: 0.11.0
category: server-and-runtime
summary: Read a named environment variable from the server.
parameters:
- name: name
type: TEXT
required: true
description: The name of the environment variable to read. Must be a literal string.
---

Returns the value of the given [environment variable](https://en.wikipedia.org/wiki/Environment_variable).

### Example

```sql
SELECT 'text' AS component;
SELECT 'The value of the HOME environment variable is ' AS contents;
SELECT sqlpage.environment_variable('HOME') as contents, true as code;
```
42 changes: 42 additions & 0 deletions examples/official-site/sqlpage/content/functions/exec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
icon: terminal-2
since: 0.12.0
category: server-and-runtime
summary: Run a program on the server and capture its output.
parameters:
- name: program
type: TEXT
required: true
description: The name of the program to execute. Must be a literal string.
- name: arguments...
type: TEXT
description: The arguments to pass to the program.
---

Executes a shell command and returns its output as text.

### Example

#### Fetch data from a remote API using curl

```sql
select 'card' as component;
select value->>'name' as title, value->>'email' as description
from json_each(sqlpage.exec('curl', 'https://jsonplaceholder.typicode.com/users'));
```

#### Notes

- This function is disabled by default for security reasons. You can enable it by setting `"allow_exec" : true` in `sqlpage/sqlpage.json`. Enable it only if you trust all the users that can access your SQLPage server files (both locally and on the database).
- Be careful when using this function, as it can be used to execute arbitrary shell commands on your server. Do not use it with untrusted input.
- The command is executed in the current working directory of the SQLPage server process.
- The command is executed with the same user as the SQLPage server process.
- The environment variables of the SQLPage server process are passed to the command, including potentially sensitive variables such as `DATABASE_URL`.
- The command is executed asynchronously, but the SQLPage server has to wait for it to finish before sending the result to the client.
This means that the SQLPage server will not be blocked while the command is running, it will be able to serve other requests, but it will not be able to serve the current request until the command has finished.
You should generally avoid long running commands.
- If the program name is NULL, the result will be NULL.
- If any argument is NULL, it will be passed to the command as an empty string.
- If the command exits with a non-zero exit code, the function will raise an error.
- Arbitrary SQL operations are not allowed as sqlpage function arguments. Use `SET` to assign the result of a SQL query to a variable, and then use that variable as an argument to `sqlpage.exec`.
- When `sqlpage.exec(...)` is a standalone selected column, the command runs once per returned row. Use `SET command_result = sqlpage.exec(...)` first if the command should run only once for the page.
Loading
Loading