flight-router 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a06ba528e0a134181e9ad283db16ba630b5d0817
4
+ data.tar.gz: e913da299e1766275123dc9d5527d683f7fb0a0c
5
+ SHA512:
6
+ metadata.gz: a8d86dd98f3a5871d817e648cb0dff683e53d8648e10b20aeadcabaa4dd17e65f85fcb472d6b89dc70fed214d27fbfdd21274d01d5b0c5dddc126f71105135f8
7
+ data.tar.gz: 5469e04c6756409dbeac991876a3e9442a8d1366f221e912300c307d9c5aa2a1278a4907ec9909ff8cf22f7edba83ae8b66f3d71cca12f98d1f61a15627da7fd
data/.envrc ADDED
@@ -0,0 +1,6 @@
1
+ export APP_ROOT=$(pwd)
2
+ export PATH=$APP_ROOT/bin:$PATH
3
+
4
+ export DOCKER_WRAPPER_IMAGE_ruby=2.4.1
5
+
6
+ export GIT_RELEASE_VERSION_FILE=lib/flight/router/version.rb
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in flight-router.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 GETTO Systems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # flight-router
2
+
3
+ router script for flight
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'flight-router'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install flight-router
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ credential = {
25
+ "gcp" => {
26
+ "GCP_CREDENTIALS_JSON" => '"JSON"',
27
+ },
28
+ "smtp" => {
29
+ "SMTP_SERVER" => "SMTP-SERVER",
30
+ "SMTP_PORT" => "SMTP-PORT",
31
+ "SMTP_USER" => "SMTP-USER",
32
+ "SMTP_PASSWORD" => "SMTP-PASSWORD",
33
+ },
34
+ }
35
+ contents = {
36
+ "reset-email" => {
37
+ "EMAIL_FROM" => "EMAIL-FROM",
38
+ "EMAIL_SUBJECT" => "EMAIL-SUBJECT",
39
+ "EMAIL_BODY" => <<EMAIL_BODY
40
+ EMAIL
41
+ BODY
42
+ EMAIL_BODY
43
+ },
44
+ }
45
+
46
+ router = Flight::Router::Drawer.new(
47
+ env: "development",
48
+ output_dir: File.expand_path("../routes",__FILE__),
49
+ )
50
+ router.map do
51
+ set :domain, "habit.getto.systems"
52
+ set :origin, env(
53
+ production: "https://#{map[:domain]}",
54
+ development: "http://localhost:12080",
55
+ )
56
+ group :image do
57
+ set :auth, "phoenix", "0.0.0-pre23"
58
+ set :datastore, "diplomat", "0.0.0-pre14", env: credential["gcp"]
59
+ set :reset_password, "phoenix", "0.0.0-pre6", env: credential["smtp"].merge(
60
+ LOGIN_URL: "#{map[:origin]}/login/direct.html",
61
+ )
62
+ end
63
+ group :auth do
64
+ set :direct, method: "header", expire: 600, verify: 600
65
+ set :api, method: "header", expire: 604800, verify: 1209600
66
+ set :download, method: "get", expire: 600
67
+ end
68
+ end
69
+
70
+ router.app do
71
+ set :origin, map[:origin]
72
+ end
73
+
74
+ router.draw("/getto/habit") do
75
+ namespace :token do
76
+ api :auth do
77
+ [
78
+ [:auth, "format-for-auth", kind: "User"],
79
+ [:datastore, "find", kind: "User"],
80
+ [:auth, "sign", auth: :api],
81
+ ]
82
+ end
83
+ api :direct, auth: :direct do
84
+ [ [:auth, "renew", auth: :api, verify: :direct] ]
85
+ end
86
+ api :renew, auth: :api do
87
+ [ [:auth, "renew", auth: :api, verify: :api] ]
88
+ end
89
+ api :reset do
90
+ [
91
+ [:datastore, "find", kind: "User"],
92
+ [:auth, "sign", auth: :direct],
93
+ [:reset_password, "send-email", env: contents["reset-email"]],
94
+ ]
95
+ end
96
+ end
97
+
98
+ namespace :profile, auth: :api do
99
+ api :update do
100
+ [
101
+ [:auth, "password-hash", kind: "User"],
102
+ [:datastore, "modify", scope: [
103
+ [
104
+ "User",
105
+ action: :replace,
106
+ samekey: "loginID",
107
+ cols: ["email","loginID","password"],
108
+ ],
109
+ ]],
110
+ ]
111
+ end
112
+ end
113
+ end
114
+ ```
115
+
116
+ ## Development
117
+
118
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
119
+
120
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
121
+
122
+ ## Contributing
123
+
124
+ Bug reports and pull requests are welcome on GitHub at https://github.com/getto-systems/flight-router.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flight/router"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "flight/router/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flight-router"
8
+ spec.version = Flight::Router::VERSION
9
+ spec.authors = ["shun@getto.systems"]
10
+ spec.email = ["shun@getto.systems"]
11
+
12
+ spec.summary = %q{router script for flight}
13
+ spec.description = %q{generate routes.json by routes DSL}
14
+ spec.homepage = "https://github.com/getto-systems/flight-router"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.15"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ end
@@ -0,0 +1,11 @@
1
+ module Flight::Router
2
+ class App < MapBase
3
+ def initialize(map)
4
+ @map = map
5
+ end
6
+
7
+ def map
8
+ @map.map
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,135 @@
1
+ require "shellwords"
2
+
3
+ module Flight::Router
4
+ class Container
5
+ module Builder
6
+ def self.image(image,&block)
7
+ @image = image
8
+ block.call
9
+ @image = nil
10
+ end
11
+ def self.command(command,&block)
12
+ builder[@image] ||= {}
13
+ builder[@image][command] = block
14
+ end
15
+ def self.builder
16
+ @builder ||= {}
17
+ end
18
+
19
+ image :auth do
20
+ command "format-for-auth" do |kind:|
21
+ [kind]
22
+ end
23
+ command "password-hash" do |kind:|
24
+ [kind, kind]
25
+ end
26
+ command "sign" do |auth:|
27
+ [map[:auth][auth][:key]]
28
+ end
29
+ command "renew" do |auth:,verify:|
30
+ [map[:auth][auth][:key], "--verify", map[:auth][verify][:verify]]
31
+ end
32
+ end
33
+
34
+ image :datastore do
35
+ command "find" do |kind:|
36
+ [kind]
37
+ end
38
+ command "modify" do |scope:|
39
+ scope.map{|kind,action:,**opts|
40
+ [kind,action,opts.map{|k,v|
41
+ if v.respond_to?(:join)
42
+ v = v.join(",")
43
+ end
44
+ "#{k}=#{v}"
45
+ }].join(":")
46
+ }
47
+ end
48
+ end
49
+
50
+ image :reset_password do
51
+ command "send-email" do
52
+ []
53
+ end
54
+ end
55
+ end
56
+
57
+ using HashEx
58
+
59
+ def initialize(path,app:,map:,output_dir:)
60
+ @path = path
61
+ @app = app
62
+ @map = map
63
+ @output_dir = output_dir
64
+ @builder = Builder.builder
65
+ end
66
+
67
+ def map
68
+ @map
69
+ end
70
+
71
+ def config
72
+ @config ||= {}
73
+ end
74
+
75
+ def namespace(path,auth: nil,&block)
76
+ @path << path
77
+
78
+ _app = @app
79
+ app = {}
80
+ merge_auth(app,auth)
81
+ @app = @app.dup.deep_merge(app)
82
+
83
+ _config = config
84
+ @config = {}
85
+
86
+ instance_exec(&block)
87
+
88
+ @app = _app
89
+ @config = _config.merge(@config)
90
+ @path.pop
91
+ end
92
+ def api(path,auth: nil,&block)
93
+ path = @path + [path]
94
+ config = {}
95
+ merge_auth(config,auth)
96
+ commands = parse_command path, instance_exec(&block)
97
+ @config[path.join("/")] = @app
98
+ .deep_merge(config)
99
+ .deep_merge(commands: commands)
100
+ end
101
+
102
+ private
103
+
104
+ def merge_auth(hash,auth)
105
+ if auth
106
+ hash[:auth] = map[:auth][auth]
107
+ end
108
+ end
109
+
110
+ def parse_command(path,commands)
111
+ commands.each.with_index(1).map{|(image,key,opts),i|
112
+ unless info = map[:image][image]
113
+ raise "image not defined: [#{image}]"
114
+ end
115
+ if opts[:env] || info[:env]
116
+ puts_env(path, "#{i}.env", (info[:env] || {}).merge(opts[:env] || {}))
117
+ end
118
+ if block = @builder[image][key]
119
+ command_args = instance_exec(**opts,&block)
120
+ else
121
+ raise "unknown image/key pair: #{image}/#{key}"
122
+ end
123
+ "#{info[:name]} flight_#{image} #{key} #{Shellwords.join(command_args)}"
124
+ }
125
+ end
126
+
127
+ def puts_env(path,file,env)
128
+ dir = File.join(@output_dir.to_s,path.map(&:to_s))
129
+ FileUtils.mkdir_p(dir)
130
+ File.write(File.join(dir,file),env.map{|k,v|
131
+ "#{k}=#{v.gsub("\n",'\n')}"
132
+ }.join("\n"))
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,33 @@
1
+ require "json"
2
+
3
+ module Flight::Router
4
+ class Drawer
5
+ def initialize(opts)
6
+ @output_dir = opts[:output_dir]
7
+ @output_file = opts[:output_file]
8
+ @map = Map.new(opts[:env])
9
+ @app = App.new(@map)
10
+ end
11
+
12
+ def map(&block)
13
+ @map.instance_exec(&block)
14
+ end
15
+ def app(&block)
16
+ @app.instance_exec(&block)
17
+ end
18
+
19
+ def draw(path,&block)
20
+ dir = File.join(@output_dir,path)
21
+ FileUtil.mkdir_p(dir)
22
+ File.write File.join(dir,"routes.json"), JSON.generate(build(path,&block))
23
+ end
24
+ def build(path,&block)
25
+ if path == "/"
26
+ path == ""
27
+ end
28
+ container = Container.new([path], app: @app.config, map: @map.map, output_dir: @output_dir)
29
+ container.instance_exec(&block)
30
+ container.config
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module Flight::Router
2
+ module HashEx
3
+ refine Hash do
4
+ def deep_merge(hash)
5
+ unless hash.respond_to?(:map)
6
+ hash
7
+ else
8
+ merge Hash[hash.map{|k,v|
9
+ case self[k]
10
+ when Hash
11
+ v = self[k].deep_merge(v)
12
+ end
13
+ [k, v]
14
+ }]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ module Flight::Router
2
+ class Map < MapBase
3
+ module Builder
4
+ def self.build(*keys,&block)
5
+ builder[keys] = block
6
+ end
7
+ def self.builder
8
+ @builder ||= {}
9
+ end
10
+
11
+ build :image do |key,type,tag,**opts|
12
+ opts.merge(
13
+ name: "getto/flight-#{key}-#{type}:#{tag}",
14
+ )
15
+ end
16
+
17
+ build :auth do |key,config|
18
+ config.merge(
19
+ image: map[:image][:auth][:name],
20
+ key: "#{key}.#{map[:domain]}",
21
+ )
22
+ end
23
+ end
24
+
25
+ def initialize(env)
26
+ @env = env.to_sym
27
+ @builder = Builder.builder
28
+ end
29
+
30
+ def env(**opts)
31
+ unless opts.has_key?(@env)
32
+ raise "env not defined: [#{@env}]"
33
+ end
34
+ opts[@env]
35
+ end
36
+
37
+ private
38
+
39
+ def value(key,args,opts)
40
+ if block = @builder[@path]
41
+ instance_exec(key,*args,**opts,&block)
42
+ else
43
+ super
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,38 @@
1
+ module Flight::Router
2
+ class MapBase
3
+ using HashEx
4
+
5
+ def map
6
+ @_config || config
7
+ end
8
+ def config
9
+ @config ||= {}
10
+ end
11
+
12
+ def group(path,&block)
13
+ @path ||= []
14
+ @path << path
15
+
16
+ @_config = config
17
+ @config = {}
18
+
19
+ instance_exec(&block)
20
+
21
+ @config = @_config.deep_merge(
22
+ path => @config,
23
+ )
24
+ @_config = nil
25
+
26
+ @path.pop
27
+ end
28
+ def set(key,*args,**opts)
29
+ config[key] = value(key,args,opts)
30
+ end
31
+
32
+ private
33
+
34
+ def value(key,args,opts)
35
+ args.first
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Flight
2
+ module Router
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require "flight/router/version"
2
+
3
+ module Flight
4
+ module Router
5
+ autoload :HashEx, "flight/router/hash_ex"
6
+ autoload :Drawer, "flight/router/drawer"
7
+ autoload :Container, "flight/router/container"
8
+ autoload :MapBase, "flight/router/map_base"
9
+ autoload :Map, "flight/router/map"
10
+ autoload :App, "flight/router/app"
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flight-router
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shun@getto.systems
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: generate routes.json by routes DSL
56
+ email:
57
+ - shun@getto.systems
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".envrc"
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - flight-router.gemspec
72
+ - lib/flight/router.rb
73
+ - lib/flight/router/app.rb
74
+ - lib/flight/router/container.rb
75
+ - lib/flight/router/drawer.rb
76
+ - lib/flight/router/hash_ex.rb
77
+ - lib/flight/router/map.rb
78
+ - lib/flight/router/map_base.rb
79
+ - lib/flight/router/version.rb
80
+ homepage: https://github.com/getto-systems/flight-router
81
+ licenses: []
82
+ metadata:
83
+ allowed_push_host: https://rubygems.org
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.6.13
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: router script for flight
104
+ test_files: []