Chris@0: :- module(urispace,[init/0]). Chris@0: Chris@0: :- use_module(library('http/thread_httpd')). Chris@27: :- use_module(library('http/http_cors')). Chris@0: :- use_module(library('semweb/rdf_db')). Chris@0: :- use_module(log). Chris@0: :- use_module(config). Chris@0: :- use_module(mapping). Chris@0: Chris@27: :- set_setting_default(http:cors, [*]). Chris@27: Chris@27: Chris@0: server(Port, Options) :- Chris@0: http_server(reply,[ port(Port),timeout(20)| Options]). Chris@0: Chris@0: /** Chris@0: * Closes the servlet Chris@0: */ Chris@0: reply(Request) :- Chris@0: log:log(Request), Chris@0: member(path('/quit'), Request), !, Chris@0: format('Connection: close~n', []), Chris@0: format('Content-type: text/html~n~n', []), Chris@0: format('Bye Bye~n'). Chris@0: Chris@0: Chris@0: /** Chris@0: * Sends back 303 to RDF document describing the resource Chris@0: */ Chris@0: reply(Request) :- Chris@0: member(path(Path),Request), Chris@0: member(accept(AcceptHeader),Request), Chris@0: format(user_error,'Accept header: ~w ',[AcceptHeader]), Chris@0: %accept_rdf(AcceptHeader), Chris@0: concat_atom(PatternX,'/',Path), Chris@0: delete(PatternX,'',Pattern), Chris@0: mapping:see_other_rdf(requested_pattern(Pattern),redirect_pattern(RedirectP)), Chris@0: !, Chris@0: concat_atom(RedirectP,Redirect), Chris@27: log:log('Sending a 303 towards ~w',Redirect), Chris@27: cors_enable, Chris@0: throw(http_reply(see_other(Redirect),[])). Chris@0: Chris@0: accept_rdf('application/rdf+xml'). Chris@0: %accept_rdf('text/xml'). Chris@0: accept_rdf(AcceptHeader) :- Chris@0: sub_atom(AcceptHeader,_,_,_,'application/rdf+xml'). Chris@0: %accept_rdf(AcceptHeader) :- Chris@0: % sub_atom(AcceptHeader,_,_,_,'text/xml'). Chris@0: Chris@0: /** Chris@0: * Sends back towards the default representation of the resource Chris@0: * (usually html) Chris@0: */ Chris@0: reply(Request) :- Chris@0: member(path(Path),Request), Chris@0: concat_atom(PatternX,'/',Path), Chris@0: delete(PatternX,'',Pattern), Chris@0: mapping:see_other(requested_pattern(Pattern),redirect_pattern(RedirectP)), Chris@0: !, Chris@0: concat_atom(RedirectP,Redirect), Chris@0: log:log('Sending a 303 towards ~w',Redirect), Chris@27: cors_enable, Chris@0: throw(http_reply(see_other(Redirect),[])). Chris@0: Chris@0: init :- Chris@0: config:port(P), Chris@0: server(P,[]), Chris@0: nl, Chris@0: writeln(' - Server launched'),nl. Chris@0: Chris@0: :- Chris@0: nl,writeln('----------------'),nl, Chris@0: writeln(' - Launch the server by running ?-init.'), Chris@0: writeln(' - To change the port, change config.pl'),nl,writeln('----------------'),nl. Chris@0: Chris@0: