Browse Source

Add DB path configuration

master
Cassidy Dingenskirchen 10 months ago
parent
commit
cd974d873c
Signed by: StDingenskirchen GPG Key ID: 14FE9712CC42FE8B
3 changed files with 12 additions and 7 deletions
  1. +1
    -0
      example.env
  2. +9
    -6
      src/logic.cr
  3. +2
    -1
      src/shashort.cr

+ 1
- 0
example.env View File

@ -1 +1,2 @@
port = 8001
dbfile = ./shashort.db

+ 9
- 6
src/logic.cr View File

@ -1,18 +1,21 @@
DATABASE = DB.open "sqlite3://./shashort.sqlite3"
URLREGEX = Regex.new("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
DATABASE = DB.open "sqlite3:#{ENV["dbfile"]}"
URLREGEX = Regex.new("^https?://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]")
module SHAShortLogic
extend self
class TooLongError < Exception
def to_s
"Too Long"
end
end
class NoURLError < Exception
def to_s
"Not An URL"
end
end
class RateLimitError < Exception
def to_s
"Rate Limited"
@ -20,9 +23,9 @@ module SHAShortLogic
end
def create_link(url)
raise TooLongError.new() if url.size >= 2000
raise NoURLError.new() if URLREGEX.match(url).nil?
raise RateLimitError.new() if SHAShort.ratelimited?
raise TooLongError.new if url.size >= 2000
raise NoURLError.new if URLREGEX.match(url).nil?
raise RateLimitError.new if SHAShort.ratelimited?
SHAShort.ratelimit_set_now
hash = Digest::SHA3.hexdigest(url)
DATABASE.exec "insert into urlmap (url, hash) values (?,?)", url, hash
@ -36,7 +39,7 @@ module SHAShortLogic
end
def fetch_link(hash)
raise InvalidHashLengthError.new() if hash.size != 128
raise InvalidHashLengthError.new if hash.size != 128
DATABASE.query_one "select url from urlmap where hash = ?", hash, as: String
end
end

+ 2
- 1
src/shashort.cr View File

@ -18,6 +18,8 @@ module SHAShort
end
end
Dotenv.load!
require "./responses/api/web/create"
require "./responses/api/web/resolve"
require "./responses/api/json/create"
@ -40,5 +42,4 @@ after_all do |env|
end
end
Dotenv.load!
Kemal.run ENV["port"].to_i32

Loading…
Cancel
Save