Skip to content

Instantly share code, notes, and snippets.

@pavelsr
Last active May 13, 2020 00:56
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 pavelsr/95666361e8f8c2f32b67c4eb2786a5bc to your computer and use it in GitHub Desktop.
Save pavelsr/95666361e8f8c2f32b67c4eb2786a5bc to your computer and use it in GitHub Desktop.
Demo of Yancy MultiTenant OpenApi problem https://github.com/preaction/Yancy/issues/104
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::SQLite;
helper sqlite => sub {
state $path = app->home->child( 'data.db' );
state $sqlite = Mojo::SQLite->new( 'sqlite:' . $path );
return $sqlite;
};
app->sqlite->auto_migrate(1)->migrations->from_data;
plugin Yancy => {
backend => { Sqlite => app->sqlite },
read_schema => 1
};
app->yancy->plugin('Form::Bootstrap4');
app->yancy->plugin( 'Auth::Password' => {
schema => 'users',
username_field => 'username',
password_field => 'password',
password_digest => { type => 'SHA-1' },
allow_register => 1,
} );
eval {
app->yancy->create( users => { username => "admin", password => "admin" } );
app->yancy->create( users => { username => "user", password => "user" } );
app->yancy->create( notes => { text => "Hello world", user_id => 1 } );
app->yancy->create( notes => { text => "Foo bar", user_id => 2 } );
};
get '/' => sub { my ( $c ) = @_; $c->render( template => 'index' ) };
app->start;
__DATA__
@@ migrations
-- 1 up
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username VARCHAR UNIQUE,
password VARCHAR,
is_admin BOOLEAN
);
CREATE TABLE notes (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
text TEXT,
user_id INTEGER
);
@@ index.html.ep
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@3/swagger-ui.css" >
</head>
<body>
<div class="text-right">
% if ( !$c->yancy->auth->current_user ) {
<a href="<%= url_for('yancy.auth.password.login') %>">Login</a>
% } else {
<span class="text-muted">Current user: <%== session->{yancy}{auth}{password} =%></span>
<a href="<%= url_for('yancy.auth.password.login') %>">Login as another user</a>
<a href="<%= url_for('yancy.auth.password.logout') %>">Logout</a>
</div>
% }
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"> </script>
<script src="https://unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
urls: [ {name: "Yancy API", url: "/yancy/api"} ],
dom_id: '#swagger-ui',
deepLinking: true,
presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ],
plugins: [ SwaggerUIBundle.plugins.DownloadUrl ],
layout: "StandaloneLayout"
})
window.ui = ui
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment