Skip to content

Commit

Permalink
Add Lucky framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew McGarvey committed Oct 11, 2020
1 parent 6e243ac commit c5c3c35
Show file tree
Hide file tree
Showing 43 changed files with 468 additions and 0 deletions.
6 changes: 6 additions & 0 deletions frameworks/Crystal/lucky/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[![github banner-short](https://user-images.githubusercontent.com/22394/26989908-dd99cc2c-4d22-11e7-9576-c6aeada2bd63.png)](http://luckyframework.org)

This is the [Lucky](https://luckyframework.org/) test of the Framework Benchmarks.

Lucky is a web framework written in Crystal. It helps you work quickly, catch bugs at compile time, and deliver blazing fast responses.

30 changes: 30 additions & 0 deletions frameworks/Crystal/lucky/benchmark_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"framework": "lucky",
"tests": [
{
"default": {
"json_url": "/json",
"plaintext_url": "/plaintext",
"db_url": "/db",
"query_url": "/queries?queries=",
"fortune_url": "/fortunes",
"update_url": "/updates?queries=",
"port": 8080,
"approach": "Realistic",
"classification": "Fullstack",
"database": "postgres",
"framework": "Lucky",
"language": "Crystal",
"flavor": "None",
"orm": "Full",
"platform": "None",
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "Lucky",
"notes": "",
"versus": "None"
}
}
]
}
4 changes: 4 additions & 0 deletions frameworks/Crystal/lucky/config/colors.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This enables the color output when in development or test
# Check out the Colorize docs for more information
# https://crystal-lang.org/api/Colorize.html
Colorize.enabled = false
19 changes: 19 additions & 0 deletions frameworks/Crystal/lucky/config/cookies.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "./server"

Lucky::Session.configure do |settings|
settings.key = "_bench_session"
end

Lucky::CookieJar.configure do |settings|
settings.on_set = ->(cookie : HTTP::Cookie) {
# If ForceSSLHandler is enabled, only send cookies over HTTPS
cookie.secure(Lucky::ForceSSLHandler.settings.enabled)

# By default, don't allow reading cookies with JavaScript
cookie.http_only(true)

# You can set other defaults for cookies here. For example:
#
# cookie.expires(1.year.from_now).domain("mydomain.com")
}
end
11 changes: 11 additions & 0 deletions frameworks/Crystal/lucky/config/database.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
AppDatabase.configure do |settings|
settings.credentials = Avram::Credentials.parse(ENV["DATABASE_URL"])
end

Avram.configure do |settings|
settings.database_to_migrate = AppDatabase

# In production, allow lazy loading (N+1).
# In development and test, raise an error if you forget to preload associations
settings.lazy_load_enabled = Lucky::Env.production?
end
13 changes: 13 additions & 0 deletions frameworks/Crystal/lucky/config/env.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Lucky::Env
extend self

{% for env in [:development, :test, :production] %}
def {{ env.id }}?
name == {{ env.id.stringify }}
end
{% end %}

def name
ENV["LUCKY_ENV"]? || "development"
end
end
3 changes: 3 additions & 0 deletions frameworks/Crystal/lucky/config/error_handler.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lucky::ErrorHandler.configure do |settings|
settings.show_debug_output = false
end
3 changes: 3 additions & 0 deletions frameworks/Crystal/lucky/config/html_page.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lucky::HTMLPage.configure do |settings|
settings.render_component_comments = false
end
16 changes: 16 additions & 0 deletions frameworks/Crystal/lucky/config/log.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "file_utils"

backend = Log::IOBackend.new
backend.formatter = Dexter::JSONLogFormatter.proc
Log.dexter.configure(:none, backend)

# Lucky only logs when before/after pipes halt by redirecting, or rendering a
# response. Pipes that run without halting are not logged.
#
# If you want to log every pipe that runs, set the log level to ':info'
Lucky::ContinuedPipeLog.dexter.configure(:none)

# Lucky only logs failed queries by default.
#
# Set the log to ':info' to log all queries
Avram::QueryLog.dexter.configure(:none)
4 changes: 4 additions & 0 deletions frameworks/Crystal/lucky/config/route_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is used when generating URLs for your application
Lucky::RouteHelper.configure do |settings|
settings.base_uri = "http://localhost:8080"
end
15 changes: 15 additions & 0 deletions frameworks/Crystal/lucky/config/server.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Here is where you configure the Lucky server
#
# Look at config/route_helper.cr if you want to change the domain used when
# generating links with `Action.url`.
Lucky::Server.configure do |settings|
settings.secret_key_base = "u4PWnhZfOFXdTOtoiSBF+6jn0zHbYS6/yumo3WXYNSw"
settings.host = "0.0.0.0"
settings.port = 8080
settings.gzip_enabled = true
settings.asset_host = ""
end

Lucky::ForceSSLHandler.configure do |settings|
settings.enabled = false
end
17 changes: 17 additions & 0 deletions frameworks/Crystal/lucky/lucky.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM crystallang/crystal:0.35.1

WORKDIR /lucky
COPY config config
COPY src src
COPY run.sh run.sh
COPY shard.lock shard.lock
COPY shard.yml shard.yml

ENV GC_MARKERS 1
ENV LUCKY_ENV production
ENV DATABASE_URL postgres://benchmarkdbuser:benchmarkdbpass@tfb-database:5432/hello_world?initial_pool_size=56&max_idle_pool_size=56

RUN apt-get install -yqq libyaml-dev
RUN shards build bench --release --no-debug

CMD bash run.sh
5 changes: 5 additions & 0 deletions frameworks/Crystal/lucky/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

./bin/bench &

wait
66 changes: 66 additions & 0 deletions frameworks/Crystal/lucky/shard.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: 2.0
shards:
avram:
git: https://github.com/luckyframework/avram.git
version: 0.17.0

blank:
git: https://github.com/kostya/blank.git
version: 0.1.0

cry:
git: https://github.com/luckyframework/cry.git
version: 0.4.2

db:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.9.0

dexter:
git: https://github.com/luckyframework/dexter.git
version: 0.3.1

exception_page:
git: https://github.com/crystal-loot/exception_page.git
version: 0.1.4

future:
git: https://github.com/crystal-community/future.cr.git
version: 0.1.0

habitat:
git: https://github.com/luckyframework/habitat.git
version: 0.4.4

lucky:
git: https://github.com/luckyframework/lucky.git
version: 0.24.0

lucky_cli:
git: https://github.com/luckyframework/lucky_cli.git
version: 0.24.0

lucky_router:
git: https://github.com/luckyframework/lucky_router.git
version: 0.3.1

pg:
git: https://github.com/will/crystal-pg.git
version: 0.21.1

pulsar:
git: https://github.com/luckyframework/pulsar.git
version: 0.2.1

shell-table:
git: https://github.com/luckyframework/shell-table.cr.git
version: 0.9.2+git.commit.078a04ea58ead5203bb435a3b5fff448ddabaeea

teeplate:
git: https://github.com/luckyframework/teeplate.git
version: 0.8.2

wordsmith:
git: https://github.com/luckyframework/wordsmith.git
version: 0.2.1

16 changes: 16 additions & 0 deletions frameworks/Crystal/lucky/shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: bench
version: 0.1.0

authors:
- your-name-here <your-email-here>

targets:
bench:
main: src/bench.cr

crystal: 0.35.1

dependencies:
lucky:
github: luckyframework/lucky
version: ~> 0.24.0
8 changes: 8 additions & 0 deletions frameworks/Crystal/lucky/src/actions/base_action.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
abstract class BaseAction < Lucky::Action
ID_MAXIMUM = 10_000

include AddRequiredHeaders

disable_cookies
accepted_formats [:json, :html, :plain_text], default: :json
end
6 changes: 6 additions & 0 deletions frameworks/Crystal/lucky/src/actions/db/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Db::Index < BaseAction
get "/db" do
world = WorldQuery.find(rand(1..ID_MAXIMUM))
json WorldSerializer.new(world)
end
end
8 changes: 8 additions & 0 deletions frameworks/Crystal/lucky/src/actions/fortunes/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Fortunes::Index < BaseAction
get "/fortunes" do
fortunes = FortuneQuery.all.results
fortunes << Fortune.new(id: 0, message: "Additional fortune added at request time.")
fortunes.sort_by!(&.message)
html Fortunes::IndexPage, fortunes: fortunes
end
end
5 changes: 5 additions & 0 deletions frameworks/Crystal/lucky/src/actions/home/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Home::Index < BaseAction
get "/" do
html Lucky::WelcomePage
end
end
5 changes: 5 additions & 0 deletions frameworks/Crystal/lucky/src/actions/json/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Json::Index < BaseAction
get "/json" do
json({message: "Hello, World!"})
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module AddRequiredHeaders
macro included
after add_required_headers
end

def add_required_headers
response.headers["Server"] = "Lucky"
response.headers["Date"] = HTTP.format_time(Time.local)
continue
end
end
7 changes: 7 additions & 0 deletions frameworks/Crystal/lucky/src/actions/mixins/queries_parser.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module QueriesParser
def queries_param : Int32
queries = params.get?("queries") || "1"
queries = queries.to_i? || 1
queries.clamp(1..500)
end
end
5 changes: 5 additions & 0 deletions frameworks/Crystal/lucky/src/actions/plaintext/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Plaintext::Index < BaseAction
get "/plaintext" do
plain_text "Hello, World!"
end
end
12 changes: 12 additions & 0 deletions frameworks/Crystal/lucky/src/actions/queries/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Queries::Index < BaseAction
include QueriesParser

get "/queries" do
results = (1..queries_param).map do
world = WorldQuery.find(rand(1..ID_MAXIMUM))
WorldSerializer.new(world)
end

json results
end
end
13 changes: 13 additions & 0 deletions frameworks/Crystal/lucky/src/actions/updates/index.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Updates::Index < BaseAction
include QueriesParser

get "/updates" do
results = (1..queries_param).map do
world = WorldQuery.find(rand(1..ID_MAXIMUM))
world = SaveWorld.update!(world, randomnumber: rand(1..ID_MAXIMUM))
WorldSerializer.new(world)
end

json results
end
end
16 changes: 16 additions & 0 deletions frameworks/Crystal/lucky/src/app.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "./shards"

require "./app_database"
require "./models/base_model"
require "./models/**"
require "./queries/**"
require "./operations/**"
require "./serializers/base_serializer"
require "./serializers/**"
require "./actions/mixins/**"
require "./actions/**"
require "./handlers/**"
require "./pages/**"
require "../config/env"
require "../config/**"
require "./app_server"
2 changes: 2 additions & 0 deletions frameworks/Crystal/lucky/src/app_database.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class AppDatabase < Avram::Database
end
20 changes: 20 additions & 0 deletions frameworks/Crystal/lucky/src/app_server.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class AppServer < Lucky::BaseAppServer
# Learn about middleware with HTTP::Handlers:
# https://luckyframework.org/guides/http-and-routing/http-handlers
def middleware : Array(HTTP::Handler)
[
CharsetHandler.new,
Lucky::RouteHandler.new,
Lucky::RouteNotFoundHandler.new,
] of HTTP::Handler
end

def protocol
"http"
end

def listen
server.bind_tcp(host, port, reuse_port: true)
server.listen
end
end
6 changes: 6 additions & 0 deletions frameworks/Crystal/lucky/src/bench.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Typically you will not use or modify this file. 'shards build' and some
# other crystal tools will sometimes use this.
#
# When this file is compiled/run it will require and run 'start_server',
# which as its name implies will start the server for you app.
require "./start_server"
16 changes: 16 additions & 0 deletions frameworks/Crystal/lucky/src/handlers/charset_handler.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CharsetHandler
include HTTP::Handler

def call(context : HTTP::Server::Context)
# Go to the next handler in the stack
call_next(context)

if html_content_type?(context.response)
context.response.content_type = "text/html; charset=utf-8"
end
end

private def html_content_type?(response)
response.headers["Content-Type"]? == "text/html"
end
end
Loading

0 comments on commit c5c3c35

Please sign in to comment.