Skip to content

Instantly share code, notes, and snippets.

@Inviz
Last active July 17, 2016 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Inviz/f0af3716e9f2ce0581cab1d2f68a0e33 to your computer and use it in GitHub Desktop.
Save Inviz/f0af3716e9f2ce0581cab1d2f68a0e33 to your computer and use it in GitHub Desktop.
eval $meta {
set_unescape_uri $flash $arg_flash;
postgres_pass database;
postgres_output json;
postgres_query "
SELECT 'abzc' as biggles,
singularize('$resource') as singular,
(SELECT columns from structures where table_name='$resource') as columns,
nullif('$flash', '') as flash";
postgres_rewrite GET no_rows 403;
mustache off;
}
postgres_pass database;
set_form_input_json $params;
# A scaffold of rest interface
location ~* /new$ {
include '/Users/invizko/sites/data/auth.conf';
# New
postgres_query GET "SELECT 'Create new ' || singularize('$resource') as title";
postgres_rewrite GET rows $views_fieldset;
}
location ~* /(?<id>\d+)/edit$ {
include '/Users/invizko/sites/data/auth.conf';
# Edit
postgres_query GET "WITH row as (SELECT * from $resource where id=$id)
SELECT 'Edit ' || singularize('$resource') as title,
'/$resource/$id' as action,
row_to_json(row) as item
FROM row";
postgres_rewrite GET HEAD rows $views_fieldset;
postgres_rewrite GET HEAD no_rows 404;
}
location ~* /(?<id>\d+)/? {
include '/Users/invizko/sites/data/auth.conf';
# Show
postgres_query GET "WITH row as (SELECT * from $resource where id=$id)
SELECT 'View ' || row.email as title,
row_to_json(row) as item
FROM row";
postgres_rewrite GET rows $views_fields;
postgres_rewrite GET no_rows 404;
# Destroy
postgres_query DELETE "DELETE from $resource where id = $id";
postgres_rewrite DELETE changes /$resource/;
postgres_rewrite DELETE no_changes 404;
# Update
postgres_query POST PUT "WITH row as (
SELECT from_json('$resource', -- Update orders
json_extract_path('$params', -- json from order[] form prefix
singularize('$resource')))) -- orders -> order
SELECT row.from_json as item,
'/$resource/$id/' as action,
'Fix the ' || singularize('$resource') as title,
'Update again' as label,
'Updated ' || singularize('$resource')
||' successfully.' as success
FROM row";
postgres_rewrite POST PUT errors $views_fieldset;
postgres_rewrite POST PUT no_errors /$resource/:id/?flash=:success;
}
location ~* / {
include '/Users/invizko/sites/data/auth.conf';
# Index
postgres_query GET "SELECT
'Browse $resource' as title,
json_agg(r) as items FROM(
SELECT *,
singularize('$resource') || ' #' || id as title,
'/$resource/' || id || '/' as url
FROM $resource
LIMIT 10) r";
postgres_rewrite GET rows $views_orders;
# Create
postgres_query POST "WITH row as (
SELECT from_json('$resource', -- INSERT into orders
json_extract_path('$params', -- json from order[] form prefix
singularize('$resource')))) -- orders -> order
SELECT 'Create new ' || singularize('$resource') as title,
'Created ' || singularize('$resource')
||' successfully.' as success,
row.from_json as item
FROM row";
postgres_rewrite POST errors $views_fieldset;
postgres_rewrite POST no_errors /$resource/:id/?flash=:success;
}
server {
include '/Users/invizko/sites/data/views.conf';
listen 80;
root /users/invizko/sites/data/;
set $before "$views_header";
set $html "";
set $after "$views_footer";
location / {
postgres_output json;
eval_subrequest_in_memory off;
eval_escalate on;
mustache on;
location ~* /(?<resource>[a-z0-9_-]+)/ {
include '/Users/invizko/sites/data/resource.conf';
}
}
<head>
<title>Postgres</title>
<link rel="stylesheet" href="/stylesheets/style.css"
</head>
<body>
<header>
<h1>Our site is great </h1>
</header>
{{#flash}}
<div class="message">
<span class="flash">
{{flash}}
</span>
<a href="#" class="undo">Undo</a>
</div>
{{/flash}}
<aside class="aside">
<a href="new">Create new</a>
</aside>
<section>
<h2>
{{title}}
</h2>
<ul>
{{#items}}
<li>
<h3>
<a href="./{{id}}/">{{parent::singular}} #{{id}}</a>
</h3>
<dl>
{{#parent::columns}}
{{#parent:by:name}}
<dt>{{name}}</dt>
<dd>{{parent:by:name}}</dd>
{{/parent:by:name}}
{{/parent::columns}}
</dl>
</li>
{{/items}}
</ul>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment