Merge pull request 'GraphQL endpoint development mode' (#52) from graphql-development-endpoint into main
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: mentorenwahl/mentorenwahl#52
This commit is contained in:
commit
134d771bd0
6 changed files with 29 additions and 20 deletions
|
@ -21,7 +21,7 @@ RUN shards install --production
|
|||
|
||||
FROM crystallang/crystal:1.3-alpine as builder
|
||||
ARG BUILD_ENV
|
||||
WORKDIR /app/backend
|
||||
WORKDIR /app
|
||||
COPY --from=deps /app/shard.yml /app/shard.lock ./
|
||||
COPY --from=deps /app/lib ./lib
|
||||
COPY ./LICENSE ./LICENSE
|
||||
|
@ -36,7 +36,7 @@ RUN if [ "${BUILD_ENV}" = "development" ]; then \
|
|||
FROM alpine as runner
|
||||
WORKDIR /app
|
||||
RUN adduser -S backend -u 1001
|
||||
COPY --from=builder /app/backend/bin ./bin
|
||||
COPY --from=builder /app/bin ./bin
|
||||
COPY ./db ./db
|
||||
USER backend
|
||||
EXPOSE 80
|
||||
|
|
|
@ -2,7 +2,7 @@ version: 2.0
|
|||
shards:
|
||||
bindata:
|
||||
git: https://github.com/spider-gazelle/bindata.git
|
||||
version: 1.9.1
|
||||
version: 1.10.0
|
||||
|
||||
commander:
|
||||
git: https://github.com/mrrooijen/commander.git
|
||||
|
@ -18,7 +18,7 @@ shards:
|
|||
|
||||
email:
|
||||
git: https://github.com/arcage/crystal-email.git
|
||||
version: 0.6.3
|
||||
version: 0.6.4
|
||||
|
||||
env_config:
|
||||
git: https://github.com/repomaa/env_config.cr.git
|
||||
|
|
|
@ -36,8 +36,6 @@ dependencies:
|
|||
github: amberframework/quartz-mailer
|
||||
kilt:
|
||||
github: jeromegn/kilt
|
||||
email:
|
||||
github: arcage/crystal-email
|
||||
router:
|
||||
github: tbrand/router.cr
|
||||
html-minifier:
|
||||
|
|
|
@ -16,12 +16,17 @@
|
|||
|
||||
require "http/request"
|
||||
require "graphql"
|
||||
require "granite"
|
||||
|
||||
module Backend
|
||||
module Api
|
||||
# GraphQL request context class
|
||||
class Context < GraphQL::Context
|
||||
# Request object
|
||||
getter request
|
||||
|
||||
# Development mode
|
||||
getter development : Bool
|
||||
|
||||
# Authenticated user
|
||||
getter user : Db::User?
|
||||
|
||||
|
@ -34,10 +39,10 @@ module Backend
|
|||
# User's external object
|
||||
getter external : (Db::Teacher | Db::Student)?
|
||||
|
||||
def initialize(request : HTTP::Request, max_complexity : Int32? = nil)
|
||||
super(max_complexity)
|
||||
def initialize(@request : HTTP::Request, @development : Bool, *rest)
|
||||
super(*rest)
|
||||
|
||||
token = request.headers["Authorization"]?
|
||||
token = @request.headers["Authorization"]?
|
||||
if token && token[..Auth::BEARER.size - 1] == Auth::BEARER
|
||||
payload = Auth.decode_jwt?(token[Auth::BEARER.size..])
|
||||
return unless payload
|
||||
|
|
|
@ -56,12 +56,21 @@ module Backend
|
|||
context.response.content_type = "application/json"
|
||||
|
||||
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(
|
||||
Schema::SCHEMA.execute(
|
||||
data.query,
|
||||
data.variables,
|
||||
data.operation_name,
|
||||
Context.new(context.request)
|
||||
Context.new(context.request, development)
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -73,14 +82,12 @@ module Backend
|
|||
def run : Nil
|
||||
draw_routes
|
||||
|
||||
server = HTTP::Server.new(
|
||||
[
|
||||
HTTP::LogHandler.new,
|
||||
HTTP::ErrorHandler.new,
|
||||
HTTP::CompressHandler.new,
|
||||
route_handler,
|
||||
]
|
||||
)
|
||||
server = HTTP::Server.new([
|
||||
HTTP::LogHandler.new,
|
||||
HTTP::ErrorHandler.new,
|
||||
HTTP::CompressHandler.new,
|
||||
route_handler,
|
||||
])
|
||||
server.bind_tcp("0.0.0.0", 80, true)
|
||||
server.listen
|
||||
end
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
require "quartz_mailer"
|
||||
require "email"
|
||||
require "kilt"
|
||||
|
||||
require "./mailers/*"
|
||||
|
|
Loading…
Reference in a new issue