redirectly 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
+ SHA256:
3
+ metadata.gz: 03e1129cdee2694e703d013e98d48d3f6056ee7a8b48dbaf357bc71970eb8fa1
4
+ data.tar.gz: 2333ceaa20dd042000a9e597a36c7ccb02c7c6d2d66f12e76c8eeb8af02fa0ee
5
+ SHA512:
6
+ metadata.gz: d3e8c208c2b26675ebe31759fb12f037623f57bbaa045aa19a34604e44a4b06eb1067e2fed9eed85553a18b072115b3fc34953e2d50aa431ac49f64fcd224d08
7
+ data.tar.gz: b3cff6f27727166d3327595a68caef334eb5ecbe83dd03af9d7ece203bef507cce7370c1922fe6487e6226fd6448725343a0b3d2249ad85795399016d0df14d4
data/bin/redirectly ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'redirectly'
3
+ require 'redirectly/cli'
4
+ require 'colsole'
5
+ include Colsole
6
+
7
+ router = Redirectly::CLI.router
8
+
9
+ begin
10
+ exit router.run ARGV
11
+
12
+ rescue Interrupt
13
+ say "\nGoodbye"
14
+ exit 1
15
+
16
+ rescue => e
17
+ if ENV['DEBUG']
18
+ puts e.backtrace.reverse
19
+ say ""
20
+ end
21
+ say "!undred!ERROR: #{e.class}"
22
+ say e.message
23
+ exit 1
24
+
25
+ end
data/lib/redirectly.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "byebug" if ENV["BYEBUG"]
2
+
3
+ require "redirectly/refinements"
4
+ require "redirectly/version"
5
+ require "redirectly/app"
@@ -0,0 +1,63 @@
1
+ require 'cgi'
2
+ require 'rack'
3
+ require 'mustermann'
4
+
5
+ module Redirectly
6
+ class App
7
+ using Refinements
8
+
9
+ attr_reader :config_path
10
+
11
+ def initialize(config_path)
12
+ @config_path = config_path
13
+ end
14
+
15
+ def call(env)
16
+ req = Rack::Request.new(env)
17
+ found = match req
18
+
19
+ if found
20
+ if found.start_with? '!'
21
+ code, target = 301, found[1..-1]
22
+ else
23
+ code, target = 302, found
24
+ end
25
+
26
+ [code, {'Location' => target}, []]
27
+
28
+ else
29
+ [404, {'Content-Type' => 'text/plain'}, ['Not Found']]
30
+
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def redirects
37
+ @redirects ||= ini_read(config_path)
38
+ end
39
+
40
+ def ini_read(path)
41
+ content = File.readlines(path, chomp:true).reject { |line| line.comment? }
42
+ content.map { |line| line.split(/\s*=\s*/, 2) }.to_h
43
+ end
44
+
45
+ def match(req)
46
+ redirects.each do |pattern, target|
47
+ pattern = "#{pattern}/" unless pattern.include? "/"
48
+ requested = "#{req.host}#{req.path}"
49
+ matcher = Mustermann.new(pattern)
50
+ params = matcher.params(requested)
51
+ if params
52
+ params.transform_keys! &:to_sym
53
+ params.delete :splat
54
+ params.transform_values! { |v| CGI.escape v }
55
+ return target % params
56
+ end
57
+ end
58
+
59
+ nil
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,14 @@
1
+ require 'mister_bin'
2
+ require 'redirectly/command'
3
+
4
+ module Redirectly
5
+ class CLI
6
+ def self.router
7
+ router = MisterBin::Runner.new version: VERSION,
8
+ header: "Redirectly Redirect Server"
9
+
10
+ router.route_all to: Command
11
+ router
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ require "colsole"
2
+ require "mister_bin"
3
+
4
+ module Redirectly
5
+ class Command < MisterBin::Command
6
+ include Colsole
7
+ help "Start the redirect server"
8
+ version Redirectly::VERSION
9
+
10
+ usage "redirectly [CONFIG --port PORT]"
11
+ usage "redirectly --init"
12
+ usage "redirectly --help | --version"
13
+
14
+ option "-p --port PORT", "Listening port [default: 3000]"
15
+ option "-i --init", "Create a sample config file and exit"
16
+
17
+ param "CONFIG", "Path to config file [default: redirects.ini]"
18
+
19
+ example "redirectly --init"
20
+ example "redirectly config.ini"
21
+
22
+ attr_reader :config_path, :port
23
+
24
+ def run
25
+ @port = args['--port'].to_i
26
+ @config_path = args['CONFIG'] || "redirects.ini"
27
+ args['--init'] ? init_file : start_server
28
+ end
29
+
30
+ private
31
+
32
+ def init_file
33
+ if File.exist? config_path
34
+ raise ArgumentError, "#{config_path} already exists"
35
+ else
36
+ File.write config_path, template
37
+ say "Initialized #{config_path}"
38
+ end
39
+ end
40
+
41
+ def template
42
+ <<~TEMPLATE
43
+ example.com = https://other-site.com/
44
+ *.mygoogle.com/:anything = https://google.com/?q=%{anything}
45
+ example.org/* = https://other-site.com/
46
+ *.old-site.com = !https://permanent.redirect.com
47
+ :sub.lvh.me/* = http://it-works.com/%{sub}
48
+ TEMPLATE
49
+ end
50
+
51
+ def start_server
52
+ raise ArgumentError, "Cannot find config file #{config_path}" unless File.exist? config_path
53
+ Rack::Server.start(app: app, Port: port, environment: 'production')
54
+ end
55
+
56
+ def app
57
+ @app ||= Redirectly::App.new config_path
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ module Redirectly
2
+ module Refinements
3
+ refine String do
4
+ def comment?
5
+ start_with? ';' or start_with? '#'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Redirectly
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redirectly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mister_bin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mustermann
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: puma
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.3'
69
+ description: Redirect server with host and dynamic URL support
70
+ email: db@dannyben.com
71
+ executables:
72
+ - redirectly
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/redirectly
77
+ - lib/redirectly.rb
78
+ - lib/redirectly/app.rb
79
+ - lib/redirectly/cli.rb
80
+ - lib/redirectly/command.rb
81
+ - lib/redirectly/refinements.rb
82
+ - lib/redirectly/version.rb
83
+ homepage: https://github.com/dannyben/redirectly
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ bug_tracker_uri: https://github.com/DannyBen/redirectly/issues
88
+ source_code_uri: https://github.com/dannyben/redirectly
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.5.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.2.16
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Redirectly redirect server
108
+ test_files: []