Sha256: 2b47381461cd3decd6e50924d4e7b3ea5b00106f5c3a37dc04934901052a4513

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

# typed: true

require_relative "schemaless_rest_api/version"
require_relative "schemaless_rest_api/entities"
require_relative "schemaless_rest_api/swagger_builder"
require "tapioca"
require "logger"
require "ougai"
require "json"
require "securerandom"

# Global params for Schemalass REST API
module SchemalessRestApi
  if ENV["Log"] == 'structured'
    @logger = Ougai::Logger.new($stdout)
    @log_type = :ougai
  else
    @logger = Logger.new($stdout)
    @log_type = :basic
  end
  
  class << self
    # @return Logger
    attr_accessor :logger

    # @return Logger type
    attr_accessor :log_type
  end

  class Error < StandardError; end
end

def models_env_not_set
  !ENV["models"] || T.must(ENV["models"]).empty?
end

def extract_models
  return [] if !Entities.models.empty? && models_env_not_set

  error_msg = "Make 'models' environment variable an array (e.g ['model1', 'model2'])"
  raise "Please set 'models' ENV variable or create 'db.json'. #{error_msg}" if ENV["models"].nil?

  models = eval(T.must(ENV["models"])) # rubocop:disable Security/Eval
  raise error_msg unless models.is_a? Array

  models
end

SEED_FILE = "db.json"

if File.exist? SEED_FILE
  puts "Seeding db based on '#{SEED_FILE}'"
  seed_data = JSON.parse(File.read(SEED_FILE))
  seed_data.each do |entity, values|
    Entities.models[entity.to_sym] = {} unless Entities.models[entity.to_sym]
    if values.is_a? Array
      values.each do |value|
        next unless value["id"]

        Entities.models[entity.to_sym][value["id"].to_s] = value
      end
    else
      puts "Entity 'entity' doesn't have array, skipping"
    end
  end
end

extract_models.each do |model|
  Entities.models[model.to_sym] = {}
end

puts "Modelling #{Entities.models.keys}"

if ENV["mongodb"]
  require_relative "schemaless_rest_api/mongo_client"
else
  puts "[INFO] Using in memory storage. Pass in 'mongodb' ENV variable to store
in db"
end
require_relative "schemaless_rest_api/rest_server"

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
schemaless_rest_api-0.4.2 lib/schemaless_rest_api.rb
schemaless_rest_api-0.4.1 lib/schemaless_rest_api.rb