~statianzo/sevenlangs

sevenlangs/prolog/gopher.pl -rw-r--r-- 760 bytes
2fcd4511Jason Staten day one logic style 4 months ago
                                                                                
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
route('/about', 'Read all about it!').
route('/cool', 'The coolest page on this server.').
route('/', '\
iHello From Prolog! \n\
0About\t/about\tlocalhost\t7777\n\
0Cool\t/cool\tlocalhost\t7777\n').

read_line(Stream, Line) :-
  read_line(Stream, '', Line).

read_line(_, Content, Line) :-
  atom_concat(Line, '\r\n', Content).

read_line(Stream, Content, Line) :-
  get_char(Stream, Char),
  atom_concat(Content, Char, Next),
  read_line(Stream, Next, Line).

main :-
  socket('AF_INET', Socket),
  socket_bind(Socket, 'AF_INET'(_,7777)),
  socket_listen(Socket, 1),
  socket_accept(Socket, StreamIn, StreamOut),
  read_line(StreamIn, Line),
  route(Line, Output),
  write(StreamOut, Output),
  flush_output(StreamOut),
  close(StreamIn),
  close(StreamOut).