2 (* Copyright (C) 2002, HELM Team.
4 * This file is part of HELM, an Hypertextual, Electronic
5 * Library of Mathematics, developed at the Computer Science
6 * Department, University of Bologna, Italy.
8 * HELM is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * HELM is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with HELM; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
28 let debug_print s = if debug then prerr_endline s;;
29 Http_common.debug := debug;;
33 let daemon_name = "Search Engine";;
34 let default_port = 58085;;
35 let port_env_var = "SEARCH_ENGINE_PORT";;
39 int_of_string (Sys.getenv port_env_var)
41 | Not_found -> default_port
42 | Failure "int_of_string" ->
43 prerr_endline "Warning: invalid port, reverting to default";
46 let pp_result result =
47 let result_string = MQueryUtil.text_of_result result "\n" in
48 (sprintf "<html><body><pre>%s</pre></body></html>" result_string)
50 let pp_error = sprintf "<html><body><h1>Error: %s</h1></body></html>" in
51 let bad_request body outchan =
52 Http_daemon.respond_error ~status:(`Client_error `Bad_request) ~body outchan
54 let callback req outchan =
58 let query_string = req#param "query" in
59 let lexbuf = Lexing.from_string query_string in
60 let query = MQueryUtil.query_of_text lexbuf in
61 let result = MQueryGenerator.execute_query query in
62 let result_string = MQueryUtil.text_of_result result "\n" in
65 (sprintf "<html><body><pre>%s</pre></body></html>" result_string)
68 let id = req#param "id" in
69 let result = MQueryGenerator.locate id in
70 Http_daemon.respond ~body:(pp_result result) outchan
72 let term_string = req#param "term" in
73 let precision = int_of_string (req#param "precision") in
74 let lexbuf = Lexing.from_string term_string in
76 CicTextualParser.main CicTextualLexer.token lexbuf
79 | [] -> (* no free variables *)
80 let term = mkterm (fun _ -> None) in
81 prerr_endline (CicPp.ppterm term);
82 let result = MQueryGenerator.searchPattern [] [] term precision in
83 Http_daemon.respond ~body:(pp_result result) outchan
87 "identifiers resolution in the environment not yet implemented")
90 Http_daemon.respond_error ~status:(`Client_error `Bad_request) outchan)
92 | Http_request.Param_not_found attr_name ->
93 bad_request (sprintf "Parameter '%s' is missing" attr_name) outchan
94 | Failure "int_of_string" ->
95 bad_request "Invalid 'precision' value, must be an integer" outchan
98 ~body:(pp_error ("Uncaught exception: " ^ (Printexc.to_string exc)))
101 printf "%s started and listening on port %d\n" daemon_name port;
102 printf "current directory is %s\n" (Sys.getcwd ());
104 Mqint.set_database Mqint.postgres_db;
105 Mqint.init "host=mowgli.cs.unibo.it dbname=helm_mowgli_new_schema user=helm";
106 Http_daemon.start' ~port callback;
108 printf "%s is terminating, bye!\n" daemon_name