# -*- coding: UTF-8 -*-
#!/usr/bin/env pyton3 
from http.server import  HTTPServer
from src.handler import Handler as Handler
import os, sys, json, ssl		
#PORT_NUMBER = 8000
from utils.findfsdb import create_db
from utils.utils import load_config

def https_server():
	server_address = ('localhost', 4443)
	httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
	httpd.socket = ssl.wrap_socket(httpd.socket,
									server_side=True,
									certfile='localhost.pem',                           
									ssl_version=ssl.PROTOCOL_TLS)
	httpd.serve_forever()

def run():
	create_db()
	config = load_config()
	if len(config) ==0:
		print("конфиг пустой")
	try:
		print(config['PORT'])
	except KeyError:
		print("KeyError")
	except KeyboardInterrupt:
		pass

	server = HTTPServer(('', config['PORT']), Handler)
	#p"rint ('Started HTTP Server on port ' , config['PORT'])
	
	server.socket = ssl.wrap_socket(server.socket,
									server_side=True,
									certfile='localhost.pem',
									ssl_version=ssl.PROTOCOL_TLS)
	print ('Started HTTP Server on port ' , config['PORT'])
	
	server.serve_forever()
	
if  __name__ == '__main__':
	run()
	https_server()