zmk/lambda/web_app.rb
Chris Andreae c7fc3432cd
Docker container and lambda function for performing firmware builds
Provides an entry point that builds and returns a combined LH + RH keyboard
firmware when provided a keymap via a POST body.

Wraps compilation with ccache, and includes a pre-warmed cache of the build in /tmp/ccache.
To maximize chance of a direct cache hit, changes the lambda driver to always build in /tmp/build.

some back of the envelope measurements (2012 xeon e3-1230v2, nixos)
clean build, no cache -> 21.308
clean build, cache -> 7.145
modified keymap, clean build, cache -> 12.127
2022-12-24 13:00:14 +09:00

43 lines
913 B
Ruby

# frozen_string_literal: true
require 'sinatra/base'
require './compiler'
class WebApp < Sinatra::Base
set :environment, :production
set :show_exceptions, false
set :logging, nil
set :default_content_type, 'application/json'
def json_body(hash)
body(hash.to_json)
end
post '/api/compile' do
request.body.rewind
keymap_data = request.body.read
result, log = Compiler.new.compile(keymap_data)
status 200
content_type 'application/octet-stream'
headers 'X-Debug-Output': log.to_json
body result
end
error Compiler::CompileError do
e = env['sinatra.error']
status e.status
json_body(error: e.message, detail: e.log)
end
error do
e = env['sinatra.error']
status 500
json_body(error: "Unexpected error: #{e.class}", detail: [e.message])
end
not_found do
status 404
json_body(error: 'No such path', detail: nil)
end
end