mentorenwahl/docker/backend/src/backend/api/webserver.cr
Dominic Grimm 0e742965cc
All checks were successful
continuous-integration/drone/push Build is passing
Added frontend
2022-01-29 16:40:39 +01:00

53 lines
1.1 KiB
Crystal

require "router"
require "http/server"
require "json"
module Backend
module API
class WebServer
include Router
def draw_routes : Nil
post "/" do |context|
context.response.content_type = "application/json"
data = GraphQLQueryData.from_json(context.request.body.not_nil!.gets.not_nil!)
context.response.puts(
Schema::SCHEMA.execute(
data.query,
data.variables,
data.operation_name,
Context.new(context.request)
)
)
context
end
end
def run : Nil
draw_routes
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
private struct GraphQLQueryData
include JSON::Serializable
property query : String
property variables : Hash(String, JSON::Any)?
property operation_name : String?
end
end
end
end