Dominic Grimm
0e742965cc
All checks were successful
continuous-integration/drone/push Build is passing
53 lines
1.1 KiB
Crystal
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
|