forked from triska/the-power-of-prolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pl
58 lines (44 loc) · 1.83 KB
/
main.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The Power of Prolog
===================
Copyright (C) 2005-2017 Markus Triska [email protected]
Online version:
https://www.metalevel.at/prolog
===============================
Example usage to spawn a server on port 5050:
$ swipl main.pl --port=5050 --interactive
then direct your browser to:
http://localhost:5050/prolog
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
:- module(main,
[]).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_server_files)).
:- use_module(library(http/http_files)).
:- use_module(library(http/http_header)).
:- use_module(library(http/http_log)).
:- use_module(library(http/mimetype)).
:- use_module(library(settings)).
:- use_module(library(http/http_unix_daemon)).
:- set_prolog_flag(default_mimetype, text/plain).
:- http_handler(/, handle_request, [prefix]).
:- http_handler('/prolog', http_reply_file('prolog/prolog.html', []), []).
handle_request(Request) :-
memberchk(path(Path0), Request),
atom_concat(., Path0, Path1),
http_safe_file(Path1, []),
absolute_file_name(Path1, Path),
( exists_file(Path),
http_reply_file(Path, [unsafe(true)], Request)
; atom_concat(Path, '.html', HTML),
exists_file(HTML),
http_reply_file(HTML, [unsafe(true)], Request)
; path_segments_atom(Segments, Path0),
Segments = _/Last/'',
atomic_list_concat([Path,Last,'.html'], File),
exists_file(File),
http_reply_file(File, [unsafe(true)], Request)
; atom_concat('https://www.metalevel.at', Path0, Metalevel),
http_redirect(see_other, Metalevel, Request)
).