Skip to content

Commit

Permalink
Add web api
Browse files Browse the repository at this point in the history
  • Loading branch information
pjialin committed Jul 8, 2019
1 parent f072913 commit caace62
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
4 changes: 4 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[app]
env = "production"

[web]
host = "127.0.0.1"
port = 8080

[redis]
address = "redis://127.0.0.1:6379"
db = 1
Expand Down
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio

from src.app.web import Web
from src.app.ip_checker import IPChecker
from src.sites import *
from src.app.ip_get import IPGet
Expand All @@ -9,6 +11,7 @@ def main():
tasks = []
tasks.append(IPGet.share().run())
tasks.append(IPChecker().run())
Web().start()
loop.run_until_complete(asyncio.wait(tasks))


Expand Down
8 changes: 8 additions & 0 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class AppEnvType:
# Coroutine count
COROUTINE_COUNT_IP_CHECK = 20

WEB = {
'host': '0.0.0.0',
'port': 8008
}
# Config
REDIS = {
'address': '127.0.0.1:6379',
Expand Down Expand Up @@ -61,6 +65,10 @@ def load(cls):
if redis:
cls.REDIS.update(redis)

web = configs.get('web')
if redis:
cls.WEB.update(web)

app = configs.get('app')
if app:
cls.APP_ENV = app.get('env', cls.APP_ENV)
Expand Down
25 changes: 25 additions & 0 deletions src/app/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from sanic import Sanic
from sanic.response import json
from multiprocessing import Process

from src.app.ip_factory import IPFactory
from src.app.main import Config

app = Sanic()


class Web(Process):

def run(self):
app.run(**Config.WEB)


@app.route('/get_ip')
async def get_ip(request):
ip = await IPFactory.get_random_ip(bool(request.raw_args.get('https', False)))
return json({
'ip': ip.ip,
'port': ip.port,
# 'https': ip.https,
'http': ip.to_http(),
})
10 changes: 5 additions & 5 deletions src/lib/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@


class IPData(DataHelper):
ip: str
port: int
delay: float
http: bool
https: bool
ip: str = ''
port: int = 0
delay: float = 0.0
http: bool = False
https: bool = False
score: float = Config.DEFAULT_SCORE

@classmethod
Expand Down

0 comments on commit caace62

Please sign in to comment.