863 |
863 |
864 /* responses and encoding */ |
864 /* responses and encoding */ |
865 |
865 |
866 case class Result(blocks: Blocks, facets: Facets) |
866 case class Result(blocks: Blocks, facets: Facets) |
867 |
867 |
868 class Encode(options: Options) { |
868 object Encode { |
869 val library_base_url = Url(options.string("find_facts_url_library")) |
869 def print_url(block: Block): String = |
870 val afp_base_url = Url(options.string("find_facts_url_afp")) |
870 HTTP.Browser_Info_Service.name + "/" + block.url_path.implode |
871 |
|
872 def url(block: Block): Url = { |
|
873 val base_url = if (block.chapter == AFP.chapter) afp_base_url else library_base_url |
|
874 base_url.resolve(block.url_path.implode) |
|
875 } |
|
876 |
871 |
877 def block(block: Block): JSON.T = |
872 def block(block: Block): JSON.T = |
878 JSON.Object( |
873 JSON.Object( |
879 "id" -> block.id, |
874 "id" -> block.id, |
880 "chapter" -> block.chapter, |
875 "chapter" -> block.chapter, |
881 "session" -> block.session, |
876 "session" -> block.session, |
882 "theory" -> block.theory, |
877 "theory" -> block.theory, |
883 "file" -> block.file, |
878 "file" -> block.file, |
884 "file_name" -> block.file_name, |
879 "file_name" -> block.file_name, |
885 "url" -> url(block).toString, |
880 "url" -> print_url(block), |
886 "command" -> block.command, |
881 "command" -> block.command, |
887 "start_line" -> block.start_line, |
882 "start_line" -> block.start_line, |
888 "src_before" -> block.src_before, |
883 "src_before" -> block.src_before, |
889 "src_after" -> block.src_after, |
884 "src_after" -> block.src_after, |
890 "html" -> block.html, |
885 "html" -> block.html, |
981 port: Int = 0, |
976 port: Int = 0, |
982 devel: Boolean = false, |
977 devel: Boolean = false, |
983 progress: Progress = new Progress |
978 progress: Progress = new Progress |
984 ): Unit = { |
979 ): Unit = { |
985 val database = options.string("find_facts_database_name") |
980 val database = options.string("find_facts_database_name") |
986 val encode = new Encode(options) |
|
987 |
981 |
988 def rebuild(): String = { |
982 def rebuild(): String = { |
989 build_html(default_web_dir + web_html, progress = progress) |
983 build_html(default_web_dir + web_html, progress = progress) |
990 File.read(default_web_dir + web_html) |
984 File.read(default_web_dir + web_html) |
991 } |
985 } |
1006 |
1000 |
1007 val server = |
1001 val server = |
1008 HTTP.server(port, name = "", services = List( |
1002 HTTP.server(port, name = "", services = List( |
1009 HTTP.Fonts_Service, |
1003 HTTP.Fonts_Service, |
1010 HTTP.CSS_Service, |
1004 HTTP.CSS_Service, |
|
1005 HTTP.Browser_Info(database = browser_info_database(db)), |
1011 new HTTP.Service("find_facts") { |
1006 new HTTP.Service("find_facts") { |
1012 def apply(request: HTTP.Request): Option[HTTP.Response] = |
1007 def apply(request: HTTP.Request): Option[HTTP.Response] = |
1013 if (request.toplevel) Some(HTTP.Response.html(if (devel) rebuild() else html)) |
1008 if (request.toplevel) Some(HTTP.Response.html(if (devel) rebuild() else html)) |
1014 else { |
1009 else { |
1015 request.uri_path.flatMap(path => web_assets.collectFirst({ |
1010 request.uri_path.flatMap(path => web_assets.collectFirst({ |
1021 new HTTP.REST_Service("api/block", progress = progress) { |
1016 new HTTP.REST_Service("api/block", progress = progress) { |
1022 def handle(body: JSON.T): Option[JSON.T] = |
1017 def handle(body: JSON.T): Option[JSON.T] = |
1023 for { |
1018 for { |
1024 request <- Parse.query_block(body) |
1019 request <- Parse.query_block(body) |
1025 block <- query_block(db, request) |
1020 block <- query_block(db, request) |
1026 } yield encode.block(block) |
1021 } yield Encode.block(block) |
1027 }, |
1022 }, |
1028 new HTTP.REST_Service("api/blocks", progress = progress) { |
1023 new HTTP.REST_Service("api/blocks", progress = progress) { |
1029 def handle(body: JSON.T): Option[JSON.T] = |
1024 def handle(body: JSON.T): Option[JSON.T] = |
1030 for (request <- Parse.query_blocks(body)) |
1025 for (request <- Parse.query_blocks(body)) |
1031 yield encode.blocks(query_blocks(db, request.query, Some(request.cursor))) |
1026 yield Encode.blocks(query_blocks(db, request.query, Some(request.cursor))) |
1032 }, |
1027 }, |
1033 new HTTP.REST_Service("api/query", progress = progress) { |
1028 new HTTP.REST_Service("api/query", progress = progress) { |
1034 def handle(body: JSON.T): Option[JSON.T] = |
1029 def handle(body: JSON.T): Option[JSON.T] = |
1035 for (query <- Parse.query(body)) yield { |
1030 for (query <- Parse.query(body)) yield { |
1036 val facet = query_facet(db, query) |
1031 val facet = query_facet(db, query) |
1037 val blocks = query_blocks(db, query) |
1032 val blocks = query_blocks(db, query) |
1038 encode.result(Result(blocks, facet)) |
1033 Encode.result(Result(blocks, facet)) |
1039 } |
1034 } |
1040 })) |
1035 })) |
1041 |
1036 |
1042 server.start() |
1037 server.start() |
1043 progress.echo("Server started " + server.toString + "/find_facts") |
1038 progress.echo("Server started " + server.toString + "/find_facts") |