~arestifo/www

a7b4ec39aee029056875740548b0e5dc3b1c210a — Alberto Restifo 9 days ago 485bef6
Upgrade lucky
M Procfile.dev => Procfile.dev +1 -0
@@ 1,2 1,3 @@
web: lucky watch --reload-browser
assets: yarn watch
system_check: script/system_check && sleep 100000
\ No newline at end of file

M config/database.cr => config/database.cr +0 -3
@@ 21,7 21,4 @@ Avram.configure do |settings|
  # In production, allow lazy loading (N+1).
  # In development and test, raise an error if you forget to preload associations
  settings.lazy_load_enabled = Lucky::Env.production?

  # Uncomment the next line to log all SQL queries
  # settings.query_log_level = ::Logger::Severity::DEBUG
end

A config/log.cr => config/log.cr +44 -0
@@ 0,0 1,44 @@
require "file_utils"

if Lucky::Env.test?
  # Logs to `tmp/test.log` so you can see what's happening without having
  # a bunch of log output in your spec results.
  FileUtils.mkdir_p("tmp")

  backend = Log::IOBackend.new(File.new("tmp/test.log", mode: "w"))
  backend.formatter = Lucky::PrettyLogFormatter.proc
  Log.dexter.configure(:debug, backend)
elsif Lucky::Env.production?
  # Lucky uses JSON in production so logs can be searched more easily
  #
  # If you want logs like in develpoment use 'Lucky::PrettyLogFormatter.proc'.
  backend = Log::IOBackend.new
  backend.formatter = Dexter::JSONLogFormatter.proc
  Log.dexter.configure(:info, backend)
else
  # Use a pretty formatter printing to STDOUT in development
  backend = Log::IOBackend.new
  backend.formatter = Lucky::PrettyLogFormatter.proc
  Log.dexter.configure(:debug, backend)
end

# Lucky only logs when before/after pipes halt by redirecting, or rendering a
# response. Pipes that run without halting are not logged.
#
# If you want to log every pipe that runs, set the log level to ':info'
Lucky::ContinuedPipeLog.dexter.configure(:none)

# Lucky only logs failed queries by default.
#
# Set the log to ':info' to log all queries
Avram::QueryLog.dexter.configure(:none)

# Skip logging static assets requests in development
Lucky::LogHandler.configure do |settings|
  if Lucky::Env.development?
    settings.skip_if = ->(context : HTTP::Server::Context) {
      context.request.method.downcase == "get" &&
      context.request.resource.starts_with?(/\/css\/|\/js\/|\/assets\/|\/favicon\.ico/)
    }
  end
end

D config/logger.cr => config/logger.cr +0 -48
@@ 1,48 0,0 @@
require "file_utils"

logger =
  if Lucky::Env.test?
    # Logs to `tmp/test.log` so you can see what's happening without having
    # a bunch of log output in your spec results.
    FileUtils.mkdir_p("tmp")
    Dexter::Logger.new(
      io: File.new("tmp/test.log", mode: "w"),
      level: Logger::Severity::DEBUG,
      log_formatter: Lucky::PrettyLogFormatter
    )
  elsif Lucky::Env.production?
    # This sets the log formatter to JSON so you can parse the logs with
    # services like Logentries or Logstash.
    #
    # If you want logs like in develpoment use `Lucky::PrettyLogFormatter`.
    Dexter::Logger.new(
      io: STDOUT,
      level: Logger::Severity::INFO,
      log_formatter: Dexter::Formatters::JsonLogFormatter
    )
  else
    # For development, log everything to STDOUT with the pretty formatter.
    Dexter::Logger.new(
      io: STDOUT,
      level: Logger::Severity::DEBUG,
      log_formatter: Lucky::PrettyLogFormatter
    )
  end

Lucky.configure do |settings|
  settings.logger = logger
end

Lucky::LogHandler.configure do |settings|
  # Skip logging static assets in development
  if Lucky::Env.development?
    settings.skip_if = ->(context : HTTP::Server::Context) {
      context.request.method.downcase == "get" &&
      context.request.resource.starts_with?(/\/css\/|\/js\/|\/assets\/|\/favicon\.ico/)
    }
  end
end

Avram.configure do |settings|
  settings.logger = logger
end

A script/helpers/function_helpers => script/helpers/function_helpers +67 -0
@@ 0,0 1,67 @@
#!/usr/bin/env bash

# This file contains a set of functions used as helpers
# for various tasks. Read the examples for each one for
# more information. Feel free to put any additional helper
# functions you may need for your app


# Returns true if the command $1 is not found
# example:
#   if command_not_found "yarn"; then
#     echo "no yarn"
#   fi
command_not_found() {
  ! command -v $1 > /dev/null
  return $?
}

# Returns true if the command $1 is not running
# You must supply the full command to check as an argument
# example:
#   if command_not_running "redis-cli ping"; then
#     print_error "Redis is not running"
#   fi
command_not_running() {
  $1
  if [ $? -ne 0 ]; then
    true
  else
    false
  fi
}

# Returns true if the OS is macOS
# example:
#   if is_mac; then
#     echo "do mac stuff"
#   fi
is_mac() {
  if [[ "$OSTYPE" == "darwin"* ]]; then
    true
  else
    false
  fi
}

# Returns true if the OS is linux based
# example:
#   if is_linux; then
#     echo "do linux stuff"
#   fi
is_linux() {
  if [[ "$OSTYPE" == "linux"* ]]; then
    true
  else
    false
  fi
}

# Prints error and exit.
# example:
#   print_error "Redis is not running. Run it with some_command"
print_error() {
  printf "${BOLD_RED_COLOR}There is a problem with your system setup:\n\n"
  printf "${BOLD_RED_COLOR}$1 \n\n" | indent
  exit 1
}

A script/helpers/text_helpers => script/helpers/text_helpers +32 -0
@@ 0,0 1,32 @@
#!/usr/bin/env bash

# This file contains a set of functions used to format text,
# and make printing text a little easier. Feel free to put
# any additional functions you need for formatting your shell
# output text.

# Colors
BOLD_RED_COLOR="\e[1m\e[31m"

# Indents the text 2 spaces
# example:
#   printf "Hello" | indent
indent() {
  while read LINE; do
    echo "  $LINE" || true
  done
}

# Prints out an arrow to your custom notice
# example:
#   notice "Installing new magic"
notice() {
  printf "\n▸ $1\n"
}

# Prints out a check mark and Done.
# example:
#   print_done
print_done() {
  printf "✔ Done\n" | indent
}

M script/setup => script/setup +17 -42
@@ 4,67 4,42 @@
set -e
set -o pipefail

indent() {
  while read LINE; do
    echo "  $LINE" || true
  done
}
source script/helpers/text_helpers

# Ensure postgres client tools are installed
check_postgres() {
  if ! command -v createdb > /dev/null; then
    printf 'Please install the postgres CLI tools, then try again.\n'
    if [[ "$OSTYPE" == "darwin"* ]]; then
      printf "If you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html.\n"
    fi
    printf 'See https://www.postgresql.org/docs/current/tutorial-install.html for install instructions.\n'
    exit 1
  fi
}

if ! command -v yarn > /dev/null; then
  printf 'Yarn is not installed.\n'
  printf 'See https://yarnpkg.com/lang/en/docs/install/ for install instructions.\n'
  exit 1
fi
notice "Running System Check"
./script/system_check
print_done

printf "\n▸ Installing node dependencies\n"
notice "Installing node dependencies"
yarn install --no-progress | indent

printf "\n▸ Compiling assets\n"
notice "Compiling assets"
yarn dev | indent

printf "\n▸ Installing shards\n"
shards install | indent
print_done

printf "\n▸ Checking that a process runner is installed\n"
# Only if this isn't CI
if [ -z "$CI" ]; then
  lucky ensure_process_runner_installed
fi
printf "✔ Done\n" | indent
notice "Installing shards"
shards install | indent

if [ ! -f ".env" ]; then
  printf "\n▸ No .env found. Creating one.\n"
  notice "No .env found. Creating one."
  touch .env
  printf "✔ Done\n" | indent
  print_done
fi

printf "\n▸ Checking that postgres is installed\n"
check_postgres | indent
printf "✔ Done\n" | indent

printf "\n▸ Creating the database\n"
notice "Creating the database"
lucky db.create | indent

printf "\n▸ Verifying postgres connection\n"
notice "Verifying postgres connection"
lucky db.verify_connection | indent

printf "\n▸ Migrating the database\n"
notice "Migrating the database"
lucky db.migrate | indent

printf "\n▸ Seeding the database with required and sample records\n"
notice "Seeding the database with required and sample records"
lucky db.create_required_seeds | indent
lucky db.create_sample_seeds | indent

printf "\n✔ All done. Run 'lucky dev' to start the app\n"
print_done
notice "Run 'lucky dev' to start the app"
\ No newline at end of file

A script/system_check => script/system_check +42 -0
@@ 0,0 1,42 @@
#!/usr/bin/env bash

source script/helpers/text_helpers
source script/helpers/function_helpers

# Use this script to check the system for required tools and process that your app needs.
# A few helper functions are provided to make writing bash a little easier. See the 
# script/helpers/function_helpers file for more examples.
#
# A few examples you might use here:
#   * 'lucky db.verify_connection' to test postgres can be connected
#   * Checking that elasticsearch, redis, or postgres is installed and/or booted
#   * Note: Booting additional processes for things like mail, background jobs, etc...
#     should go in your Procfile.dev.

if command_not_found "yarn"; then
  print_error "Yarn is not installed\n  See https://yarnpkg.com/lang/en/docs/install/ for install instructions."
fi

# Only if this isn't CI
if [ -z "$CI" ]; then
  lucky ensure_process_runner_installed
fi

if command_not_found "createdb"; then
  MSG="Please install the postgres CLI tools, then try again."
  if is_mac; then
    MSG="$MSG\nIf you're using Postgres.app, see https://postgresapp.com/documentation/cli-tools.html."
  fi
  MSG="$MSG\nSee https://www.postgresql.org/docs/current/tutorial-install.html for install instructions."

  print_error "$MSG"
fi


## CUSTOM PRE-BOOT CHECKS ##
# example:
# if command_not_running "redis-cli ping"; then
#   print_error "Redis is not running."
# fi



M shard.lock => shard.lock +5 -5
@@ 6,11 6,11 @@ shards:

  avram:
    github: luckyframework/avram
    version: 0.13.1
    version: 0.14.0

  bindata:
    github: spider-gazelle/bindata
    version: 1.3.0
    version: 1.5.1

  blank:
    github: kostya/blank


@@ 30,7 30,7 @@ shards:

  dexter:
    github: luckyframework/dexter
    version: 0.1.3
    version: 0.2.0

  dotenv:
    github: gdotdesign/cr-dotenv


@@ 54,11 54,11 @@ shards:

  lucky:
    github: luckyframework/lucky
    version: 0.20.0
    version: 0.21.1

  lucky_cli:
    github: luckyframework/lucky_cli
    version: 0.20.0
    version: 0.21.0

  lucky_flow:
    github: luckyframework/lucky_flow

M shard.yml => shard.yml +3 -3
@@ 13,10 13,10 @@ crystal: 0.34.0
dependencies:
  lucky:
    github: luckyframework/lucky
    version: ~> 0.20.0
    version: ~> 0.21.0
  authentic:
    github: luckyframework/authentic
    version: ~> 0.5.2
    version: ~> 0.5.4
  carbon:
    github: luckyframework/carbon
    version: ~> 0.1.1


@@ 25,7 25,7 @@ dependencies:
    version: 0.7.0
  lucky_flow:
    github: luckyframework/lucky_flow
    version: ~> 0.6.2
    version: ~> 0.6.3
  jwt:
    github: crystal-community/jwt
    version: ~> 1.4.0

M src/models/user_token.cr => src/models/user_token.cr +1 -1
@@ 17,7 17,7 @@ class UserToken
    payload, _header = JWT.decode(token, Lucky::Server.settings.secret_key_base, ALGORITHM)
    payload["user_id"].to_s.to_i64
  rescue e : JWT::Error
    Lucky.logger.error(jwt_decode_error: e.message)
    Log.dexter.error { {jwt_decode_error: e.message} }
    nil
  end