feat(example): add example app and blog app with demos

This commit is contained in:
dualfroz committed 2026-08-28 19:40:00 +02:00
1 parent 28e6bc3cf2
commit 7d2312b94b
13 files changed
+234

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
$LOAD_PATH.unshift(File.expand_path('lib', __dir__))
require 'prism'
require 'net/http'
require_relative 'examples/blog/app'
server = Prism::Server.new(Blog, port: 0).start
port = server.port
puts "blog demo server on port #{port}"
puts "database file: #{File.join(__dir__, 'examples/blog/db/development.pstore')}"
puts '-' * 40
Net::HTTP.start('127.0.0.1', port) do |http|
create = http.post(
'/posts',
URI.encode_www_form(title: 'Hello from Prism', body: 'This record was written through the ORM-lite file store.'),
'Content-Type' => 'application/x-www-form-urlencoded'
)
puts "POST /posts -> #{create.code} Location=#{create['location']}"
listing = http.get('/posts')
puts "GET /posts -> #{listing.code}"
puts listing.body
end
server.stop
puts '-' * 40
puts 'blog demo done (process about to exit; data stays on disk for the restart check)'
+40
View File
@@ -0,0 +1,40 @@
$LOAD_PATH.unshift(File.expand_path('lib', __dir__))
require 'prism'
require 'net/http'
require_relative 'examples/app'
server = Prism::Server.new(ExampleApp, port: 0).start
port = server.port
puts "demo server on port #{port}"
puts '-' * 40
Net::HTTP.start('127.0.0.1', port) do |http|
hello = http.get('/hello/Ruby')
puts "GET /hello/Ruby -> #{hello.code}"
puts hello.body
search = http.get('/search?q=prism&page=2')
puts "GET /search?q=prism&page=2 -> #{search.code} #{search.body}"
echo = http.post('/echo', JSON.generate(name: 'foksiu', role: 'admin'), 'Content-Type' => 'application/json')
puts "POST /echo -> #{echo.code} #{echo.body}"
files = http.get('/files/docs/readme.txt')
puts "GET /files/docs/readme.txt -> #{files.code} #{files.body}"
status = http.get('/api/status')
puts "GET /api/status -> #{status.code} #{status.body}"
first = http.get('/count')
cookie = first['set-cookie'].split(';').first
puts "GET /count -> #{first.body} (cookie issued: #{!first['set-cookie'].nil?})"
second = http.get('/count', 'Cookie' => cookie)
puts "GET /count (with cookie) -> #{second.body}"
redirect = http.get('/old')
puts "GET /old -> #{redirect.code} Location=#{redirect['location']}"
end
server.stop
puts '-' * 40
puts 'demo done'
+63
View File
@@ -0,0 +1,63 @@
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
require 'prism'
class ExampleApp < Prism::Application
enable_sessions(secret: 'prism-demo-secret')
use Prism::Middleware::Logger
helpers do
def layout(title, inner)
erb(<<~TEMPLATE, locals: { title: title, inner: inner })
<!doctype html>
<html><head><title><%= title %></title></head>
<body><%== inner %></body></html>
TEMPLATE
end
end
get '/' do
html('<h1>Prism</h1>')
end
get '/hello/:name' do
greeting = erb('<p>Hello, <%= name %>!</p>', locals: { name: params['name'] })
layout('Hello', greeting)
end
get '/search' do
json(query: params['q'], page: params['page'])
end
post '/echo' do
json(received: params.to_h)
end
get '/files/*' do
json(path: params['splat'])
end
group '/api' do
get '/status' do
json(ok: true)
end
end
get '/count' do
session['count'] = (session['count'] || 0) + 1
json(count: session['count'])
end
get '/old' do
redirect '/', 301
end
end
if __FILE__ == $PROGRAM_NAME
server = Prism::Server.new(ExampleApp, port: (ARGV[0] || 0).to_i).start
puts "Prism example listening on #{server.url}"
trap('INT') do
server.stop
exit
end
sleep
end
+23
View File
@@ -0,0 +1,23 @@
require 'prism'
APP_ROOT = __dir__
Prism::Orm.connect(File.join(APP_ROOT, 'db', 'development.pstore'))
Prism::Orm.migrator(File.join(APP_ROOT, 'db', 'migrate')).migrate!
Prism::Controller.views_root(File.join(APP_ROOT, 'app', 'views'))
Prism::Controller.layout(:application)
Dir[File.join(APP_ROOT, 'app', 'models', '*.rb')].sort.each { |file| require file }
Dir[File.join(APP_ROOT, 'app', 'controllers', '*.rb')].sort.each { |file| require file }
class Blog < Prism::Application
set :views, File.join(APP_ROOT, 'app', 'views')
enable_sessions(secret: 'blog-demo-secret')
use Prism::Middleware::Logger
use Prism::Middleware::ParamsParser
use Prism::Middleware::Static, root: File.join(APP_ROOT, 'public')
root(to: 'posts#index')
resources :posts, only: %i[index show create]
end
@@ -0,0 +1,24 @@
class PostsController < Prism::Controller
layout :application
def index
render(:index, locals: { posts: Post.order(:created_at, :desc), notice: flash[:notice], errors: [] })
end
def show
render(:show, locals: { post: Post.find(params['id']) })
rescue Prism::Orm::RecordNotFound
halt(404, 'Post not found')
end
def create
post = Post.new(title: params['title'], body: params['body'])
if post.save
flash[:notice] = "Post ##{post.id} created"
redirect url_for(:posts)
else
status(422)
render(:index, locals: { posts: Post.order(:created_at, :desc), notice: nil, errors: post.errors.full_messages })
end
end
end
+3
View File
@@ -0,0 +1,3 @@
class Post < Prism::Orm::Model
validates :title, presence: true, length: { minimum: 3 }
end
@@ -0,0 +1,5 @@
<!doctype html>
<html>
<head><title><%= defined?(title) ? title : 'Prism App' %></title></head>
<body><%== content %></body>
</html>
+4
View File
@@ -0,0 +1,4 @@
<li class="post" id="post-<%= post.id %>">
<h2><%= post.title %></h2>
<p><%= post.body %></p>
</li>
+21
View File
@@ -0,0 +1,21 @@
<h1>Blog</h1>
<% if notice %>
<p class="notice"><%= notice %></p>
<% end %>
<% unless errors.empty? %>
<ul class="errors">
<% errors.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<form method="post" action="/posts">
<input type="text" name="title" placeholder="Title">
<textarea name="body" placeholder="Body"></textarea>
<button type="submit">Create post</button>
</form>
<ul class="posts">
<% posts.each do |post| %>
<%== partial('post', locals: { post: post }) %>
<% end %>
</ul>
+2
View File
@@ -0,0 +1,2 @@
<h1><%= post.title %></h1>
<p><%= post.body %></p>
+6
View File
@@ -0,0 +1,6 @@
require_relative '../app'
server = Prism::Server.new(Blog, port: (ARGV[0] || 3000).to_i).start
puts "listening on #{server.url}"
trap('INT') { server.stop; exit }
sleep
+3
View File
@@ -0,0 +1,3 @@
require_relative 'app'
run Blog
@@ -0,0 +1,13 @@
class CreatePosts < Prism::Orm::Migration
def up
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
def down
drop_table :posts
end
end