Added development mode to GraphQL endpoint
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
Dominic Grimm 2022-02-13 11:53:19 +01:00
parent 65348f124c
commit 6ce970a3d4
6 changed files with 29 additions and 20 deletions

View file

@ -21,7 +21,7 @@ RUN shards install --production
FROM crystallang/crystal:1.3-alpine as builder FROM crystallang/crystal:1.3-alpine as builder
ARG BUILD_ENV ARG BUILD_ENV
WORKDIR /app/backend WORKDIR /app
COPY --from=deps /app/shard.yml /app/shard.lock ./ COPY --from=deps /app/shard.yml /app/shard.lock ./
COPY --from=deps /app/lib ./lib COPY --from=deps /app/lib ./lib
COPY ./LICENSE ./LICENSE COPY ./LICENSE ./LICENSE
@ -36,7 +36,7 @@ RUN if [ "${BUILD_ENV}" = "development" ]; then \
FROM alpine as runner FROM alpine as runner
WORKDIR /app WORKDIR /app
RUN adduser -S backend -u 1001 RUN adduser -S backend -u 1001
COPY --from=builder /app/backend/bin ./bin COPY --from=builder /app/bin ./bin
COPY ./db ./db COPY ./db ./db
USER backend USER backend
EXPOSE 80 EXPOSE 80

View file

@ -2,7 +2,7 @@ version: 2.0
shards: shards:
bindata: bindata:
git: https://github.com/spider-gazelle/bindata.git git: https://github.com/spider-gazelle/bindata.git
version: 1.9.1 version: 1.10.0
commander: commander:
git: https://github.com/mrrooijen/commander.git git: https://github.com/mrrooijen/commander.git
@ -18,7 +18,7 @@ shards:
email: email:
git: https://github.com/arcage/crystal-email.git git: https://github.com/arcage/crystal-email.git
version: 0.6.3 version: 0.6.4
env_config: env_config:
git: https://github.com/repomaa/env_config.cr.git git: https://github.com/repomaa/env_config.cr.git

View file

@ -36,8 +36,6 @@ dependencies:
github: amberframework/quartz-mailer github: amberframework/quartz-mailer
kilt: kilt:
github: jeromegn/kilt github: jeromegn/kilt
email:
github: arcage/crystal-email
router: router:
github: tbrand/router.cr github: tbrand/router.cr
html-minifier: html-minifier:

View file

@ -16,12 +16,17 @@
require "http/request" require "http/request"
require "graphql" require "graphql"
require "granite"
module Backend module Backend
module Api module Api
# GraphQL request context class # GraphQL request context class
class Context < GraphQL::Context class Context < GraphQL::Context
# Request object
getter request
# Development mode
getter development : Bool
# Authenticated user # Authenticated user
getter user : Db::User? getter user : Db::User?
@ -34,10 +39,10 @@ module Backend
# User's external object # User's external object
getter external : (Db::Teacher | Db::Student)? getter external : (Db::Teacher | Db::Student)?
def initialize(request : HTTP::Request, max_complexity : Int32? = nil) def initialize(@request : HTTP::Request, @development : Bool, *rest)
super(max_complexity) super(*rest)
token = request.headers["Authorization"]? token = @request.headers["Authorization"]?
if token && token[..Auth::BEARER.size - 1] == Auth::BEARER if token && token[..Auth::BEARER.size - 1] == Auth::BEARER
payload = Auth.decode_jwt?(token[Auth::BEARER.size..]) payload = Auth.decode_jwt?(token[Auth::BEARER.size..])
return unless payload return unless payload

View file

@ -56,12 +56,21 @@ module Backend
context.response.content_type = "application/json" context.response.content_type = "application/json"
data = GraphQLQueryData.from_json(context.request.body.not_nil!.gets.not_nil!) data = GraphQLQueryData.from_json(context.request.body.not_nil!.gets.not_nil!)
development = {% if flag?(:development) %}
context.request.query_params["development"]? == "true"
{% else %}
false
{% end %}
{% if flag?(:development) %}
Log.notice { "Development request incoming" } if development
{% end %}
context.response.print( context.response.print(
Schema::SCHEMA.execute( Schema::SCHEMA.execute(
data.query, data.query,
data.variables, data.variables,
data.operation_name, data.operation_name,
Context.new(context.request) Context.new(context.request, development)
) )
) )
@ -73,14 +82,12 @@ module Backend
def run : Nil def run : Nil
draw_routes draw_routes
server = HTTP::Server.new( server = HTTP::Server.new([
[
HTTP::LogHandler.new, HTTP::LogHandler.new,
HTTP::ErrorHandler.new, HTTP::ErrorHandler.new,
HTTP::CompressHandler.new, HTTP::CompressHandler.new,
route_handler, route_handler,
] ])
)
server.bind_tcp("0.0.0.0", 80, true) server.bind_tcp("0.0.0.0", 80, true)
server.listen server.listen
end end

View file

@ -15,7 +15,6 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
require "quartz_mailer" require "quartz_mailer"
require "email"
require "kilt" require "kilt"
require "./mailers/*" require "./mailers/*"