grande 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +21 -0
- data/README.md +51 -0
- data/lib/grande.rb +3 -0
- data/lib/grande/config_loader.rb +73 -0
- data/lib/grande/context.rb +32 -0
- data/lib/grande/environment.rb +5 -0
- data/lib/grande/use_redis.rb +20 -0
- data/lib/grande/use_sequel.rb +6 -0
- data/lib/grande/use_sequel_with_pg.rb +14 -0
- data/lib/grande/use_sinatra.rb +4 -0
- data/lib/grande/version.rb +5 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: de0c0ef55381a0884ab038b4b1405a8b5853304ad02429e546c36b4e43560cfd
|
4
|
+
data.tar.gz: 5d6115b6cd99f6ea98230d9eb24f536c4a1b46906e6daf2b893aa7cdb4d8e8ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d299ba2e4ab4e4c7b623331a1626d7b002d4dd54646340a719cc599b6ff009910ef38b2e43fefd0bb05743989f5d2ece64a6c1a47ee5e90be762140afe16322
|
7
|
+
data.tar.gz: 3130112b479bf4cc0d867a723266a9f0c8da2ac5055fe4c3f9e9b87fab49fc5d70bba237afc8d22b92134193df2fbaa2ca2044e4afcf51c7496408862e06b06d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2021 André Diego Piske
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
|
2
|
+
# Grande
|
3
|
+
|
4
|
+
Grande is a small framework. The world surely needs more frameworks, so I went
|
5
|
+
ahead and created this.
|
6
|
+
|
7
|
+
# Get started
|
8
|
+
|
9
|
+
Clone this repository and copy the _app\_template_ folder somewhere. This is going
|
10
|
+
to be your app root folder. Then rename that folder to something truly awesome.
|
11
|
+
|
12
|
+
Enter that folder and run:
|
13
|
+
|
14
|
+
```
|
15
|
+
$ bundle install
|
16
|
+
$ bundle exec puma -p 3000
|
17
|
+
```
|
18
|
+
|
19
|
+
Then watch it say hello to the world by running:
|
20
|
+
|
21
|
+
```
|
22
|
+
$ curl http://localhost:3000/
|
23
|
+
```
|
24
|
+
|
25
|
+
🎉
|
26
|
+
|
27
|
+
# Seriously tho
|
28
|
+
|
29
|
+
I came across creating small apps in Ruby and I ended up always creating the same
|
30
|
+
code again and again. So I to created this repo and now I can more quickly create
|
31
|
+
new apps. It is both *batteries included* and *opinionated*, so beware!
|
32
|
+
|
33
|
+
It includes:
|
34
|
+
|
35
|
+
- Configuration loading
|
36
|
+
- A `config.ru` and `config/puma.rb` with forking instructions
|
37
|
+
- A Gemfile with decent default gems
|
38
|
+
- An initial folder structure
|
39
|
+
- Database setup with Sequel, PG and Redis, working out of the box with forking servers
|
40
|
+
- A `public` folder to serve assets if there should be any
|
41
|
+
- A rspec skeleton
|
42
|
+
- Database migration folder structure and rake task. Specific for Sequel
|
43
|
+
|
44
|
+
A good foundation for any app to start with in my opinion :-)
|
45
|
+
|
46
|
+
The idea is to be simple, so there's no fancy scaffolding code. Instead, a folder
|
47
|
+
(`app_template`) where one can just copy and paste it and change whatever is needed.
|
48
|
+
|
49
|
+
# License
|
50
|
+
|
51
|
+
Licensed under the [MIT license](MIT-LICENSE).
|
data/lib/grande.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
class Grande::ConfigLoader
|
5
|
+
def initialize
|
6
|
+
@conf = nil
|
7
|
+
|
8
|
+
default_path = File.join(Grande.c.app.root_path, 'config/config.yml')
|
9
|
+
|
10
|
+
if ENV.key?('GRANDE_CONFIG_PATH')
|
11
|
+
path = ENV['GRANDE_CONFIG_PATH']
|
12
|
+
if !File.exist?(path)
|
13
|
+
raise "GRANDE_CONFIG_PATH set but file does not exist. Either remove the env var or create the file"
|
14
|
+
end
|
15
|
+
Grande.logger.info("Loading configuration from #{path}")
|
16
|
+
load_conf_file!(path)
|
17
|
+
elsif File.exist?(default_path)
|
18
|
+
Grande.logger.info("Found config.yml at #{path}. Will load config from that")
|
19
|
+
|
20
|
+
load_conf_file!(default_path)
|
21
|
+
else
|
22
|
+
Grande.logger.info('Configuration file not provided. Loading all configuration from environment variables')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_int(key, default=nil)
|
27
|
+
value = get_str(key)
|
28
|
+
return default if value == nil
|
29
|
+
Integer(value)
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_int!(key)
|
33
|
+
value = get_int(key)
|
34
|
+
raise "Config key #{key} is required" unless value
|
35
|
+
Integer(value)
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_str(key, default=nil)
|
39
|
+
env_var_name = key_to_env_var_name(key)
|
40
|
+
return ENV[env_var_name] if ENV.key?(env_var_name)
|
41
|
+
|
42
|
+
parts = key.split('.')
|
43
|
+
base_obj = if parts.length == 1
|
44
|
+
@conf
|
45
|
+
else
|
46
|
+
@conf.dig(*parts[0...-1])
|
47
|
+
end
|
48
|
+
|
49
|
+
# binding.pry if key =="redis.url"
|
50
|
+
|
51
|
+
base_obj.fetch(parts.last, default)
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_str!(key)
|
55
|
+
none = Object.new
|
56
|
+
value = get_str(key, none)
|
57
|
+
raise "Config key #{key} is required" if value == none
|
58
|
+
|
59
|
+
value
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Convert key format from foo.bar.qux
|
65
|
+
# into FOO_BAR_QUX
|
66
|
+
def key_to_env_var_name(key)
|
67
|
+
key.split('.').map(&:upcase).join('_')
|
68
|
+
end
|
69
|
+
|
70
|
+
def load_conf_file!(path)
|
71
|
+
@conf = YAML.safe_load(File.read(path))
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Grande
|
4
|
+
def self.c
|
5
|
+
::Grande::Context.instance
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.logger
|
9
|
+
c.app.logger
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Grande::Context
|
14
|
+
attr_reader :app
|
15
|
+
|
16
|
+
def self.instance
|
17
|
+
@instance ||= new
|
18
|
+
end
|
19
|
+
|
20
|
+
def set_app(app)
|
21
|
+
raise "App can't be set twice" if @app
|
22
|
+
raise 'App can only be set if in booting phase' unless app.booting?
|
23
|
+
|
24
|
+
@app = app
|
25
|
+
end
|
26
|
+
|
27
|
+
# If one is using forking servers,
|
28
|
+
# this could should be run to re-setup all DB connections
|
29
|
+
def run_after_fork
|
30
|
+
@app.setup_db_connections
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal
|
2
|
+
|
3
|
+
require 'redis'
|
4
|
+
require 'connection_pool'
|
5
|
+
|
6
|
+
module Grande::UseRedis
|
7
|
+
attr_reader :redis_pool
|
8
|
+
|
9
|
+
def setup_redis_connection
|
10
|
+
pool_size = @config_loader.get_int('redis.pool.size', 4)
|
11
|
+
pool_timeout = @config_loader.get_int('redis.pool.timeout', 5)
|
12
|
+
redis_url = @config_loader.get_str!('redis.url')
|
13
|
+
|
14
|
+
Grande.logger.info("Using redis at #{redis_url}")
|
15
|
+
|
16
|
+
@redis_pool = ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
|
17
|
+
Redis.new(url: redis_url)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './use_sequel'
|
4
|
+
require 'pg'
|
5
|
+
|
6
|
+
module Grande::UseSequel
|
7
|
+
attr_reader :db
|
8
|
+
|
9
|
+
def setup_sequel_connection
|
10
|
+
db_url = @config_loader.get_str!("database.url")
|
11
|
+
@db = Sequel.connect(db_url)
|
12
|
+
@db.extension(:pg_json)
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grande
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- André D. Piske
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
Opinionated starting point for creating ruby apps.
|
15
|
+
Designed to create APIs or Web apps.
|
16
|
+
email: andrepiske@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- MIT-LICENSE
|
22
|
+
- README.md
|
23
|
+
- lib/grande.rb
|
24
|
+
- lib/grande/config_loader.rb
|
25
|
+
- lib/grande/context.rb
|
26
|
+
- lib/grande/environment.rb
|
27
|
+
- lib/grande/use_redis.rb
|
28
|
+
- lib/grande/use_sequel.rb
|
29
|
+
- lib/grande/use_sequel_with_pg.rb
|
30
|
+
- lib/grande/use_sinatra.rb
|
31
|
+
- lib/grande/version.rb
|
32
|
+
homepage: https://github.com/andrepiske/grande
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata: {}
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubygems_version: 3.0.3
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: Superpower your small but grande app
|
55
|
+
test_files: []
|