Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from argparse import Parser 

2from argparse import ValueType 

3from fiber import Event 

4from os.path import Path 

5from os.subprocess import run 

6from os.signal import enable as enable_signal 

7from os.signal import Signal 

8from net.tcp.server import Server 

9from .database import Database 

10from .graphql import GraphQL 

11from .statistics import Statistics 

12from .activities import Activities 

13from .client_handler_fiber import ClientHandlerFiber 

14 

15func main(argv: [string]): 

16 enable_signal(Signal.Interrupt) 

17 

18 parser = Parser(version=__version__) 

19 parser.add_option("--port", 

20 short="-p", 

21 default="8000", 

22 help="Port to listen for clients on (default: 8000).") 

23 parser.add_option("--database-directory", 

24 short="-d", 

25 default=".", 

26 value_type=ValueType.DirPath, 

27 help="Database directory (default: \".\").") 

28 parser.add_option("--ipinfo-token", 

29 short="-i", 

30 takes_value=True, 

31 help="ipinfo.io token.") 

32 args = parser.parse(argv) 

33 

34 database = Database(Path(args.value_of("--database-directory"))) 

35 activities = Activities(database) 

36 statistics = Statistics(args.value_of("--ipinfo-token"), activities) 

37 graphql = GraphQL(database, statistics, activities) 

38 

39 idle_client_handlers: [ClientHandlerFiber] = [] 

40 idle_client_handlers_ready = Event() 

41 

42 for i in range(20): 

43 client_handler = ClientHandlerFiber(database, 

44 statistics, 

45 graphql, 

46 activities, 

47 idle_client_handlers, 

48 idle_client_handlers_ready, 

49 i) 

50 client_handler.start() 

51 idle_client_handlers.append(client_handler) 

52 

53 server = Server() 

54 port = i64(args.value_of("--port")) 

55 server.listen(port) 

56 

57 print(f"Listening for clients on port {port}.") 

58 

59 try: 

60 activities.add("▶️", "Website started.") 

61 

62 while True: 

63 if idle_client_handlers.length() == 0: 

64 statistics.no_idle_client_handlers += 1 

65 idle_client_handlers_ready.clear() 

66 idle_client_handlers_ready.wait() 

67 

68 client = server.accept() 

69 client_handler = idle_client_handlers.pop() 

70 client_handler.serve_client(client) 

71 except InterruptError: 

72 print("Interrupted. Exiting.") 

73 

74 activities.add("⏹️", "Website stopped.") 

75 activities.save() 

76 

77test application(): 

78 run("mys build -c") 

79 run("cd tests && python3 test.py", capture_output=False)