2023-12-08 17:28:45 +01:00
|
|
|
# frozen_string_literal: true
|
2023-12-24 20:15:46 +01:00
|
|
|
|
2023-12-08 17:28:45 +01:00
|
|
|
module Twenty
|
2024-01-09 00:48:37 +01:00
|
|
|
require "fileutils"
|
2024-03-28 06:22:08 +01:00
|
|
|
require "sequel"
|
2024-01-07 09:51:08 +01:00
|
|
|
|
2024-04-27 04:55:33 +02:00
|
|
|
extend Module.new {
|
2024-04-27 05:46:24 +02:00
|
|
|
extend self
|
2024-04-27 04:55:33 +02:00
|
|
|
extend FileUtils
|
|
|
|
require "tmpdir"
|
|
|
|
|
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns the path for the default SQLite database
|
|
|
|
def default_database
|
|
|
|
@default_database ||= File.join(datadir, "database.sqlite")
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns the directory where twenty stores data
|
|
|
|
def datadir
|
|
|
|
File.join(Dir.home, ".local", "share", "twenty")
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns the directory where twenty stores temporary data
|
|
|
|
def tmpdir
|
|
|
|
File.join(Dir.tmpdir, "twenty")
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns the path to a PID file
|
|
|
|
def pid
|
|
|
|
File.join(tmpdir, "server.pid")
|
|
|
|
end
|
|
|
|
|
|
|
|
mkdir_p(datadir)
|
|
|
|
mkdir_p(tmpdir)
|
|
|
|
}
|
2024-01-09 00:48:37 +01:00
|
|
|
|
|
|
|
##
|
2024-04-27 04:44:20 +02:00
|
|
|
# Establishes a database connection
|
2024-01-09 00:48:37 +01:00
|
|
|
#
|
|
|
|
# @param [String] path
|
2024-04-27 04:44:20 +02:00
|
|
|
# The path to a SQLite database
|
2024-01-09 00:48:37 +01:00
|
|
|
#
|
|
|
|
# @return [void]
|
|
|
|
def self.establish_connection(path:)
|
2024-03-28 06:22:08 +01:00
|
|
|
@connection = Sequel.connect(
|
|
|
|
adapter: "sqlite",
|
|
|
|
database: path
|
2024-01-09 00:48:37 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-04-27 04:44:20 +02:00
|
|
|
##
|
|
|
|
# @return [Sequel::Database::SQLite]
|
|
|
|
# Returns the connection to a database
|
2024-03-28 06:22:08 +01:00
|
|
|
def self.connection
|
|
|
|
establish_connection unless @connection
|
|
|
|
@connection
|
|
|
|
end
|
|
|
|
|
2024-04-22 01:08:29 +02:00
|
|
|
require_relative "server/graphql"
|
|
|
|
require_relative "server/rack"
|
2023-12-08 17:28:45 +01:00
|
|
|
end
|