Skip to content

Commit caace62

Browse files
committed
Add web api
1 parent f072913 commit caace62

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

config.toml.example

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
[app]
22
env = "production"
33

4+
[web]
5+
host = "127.0.0.1"
6+
port = 8080
7+
48
[redis]
59
address = "redis://127.0.0.1:6379"
610
db = 1

main.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import asyncio
2+
3+
from src.app.web import Web
24
from src.app.ip_checker import IPChecker
35
from src.sites import *
46
from src.app.ip_get import IPGet
@@ -9,6 +11,7 @@ def main():
911
tasks = []
1012
tasks.append(IPGet.share().run())
1113
tasks.append(IPChecker().run())
14+
Web().start()
1215
loop.run_until_complete(asyncio.wait(tasks))
1316

1417

src/app/main.py

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class AppEnvType:
1818
# Coroutine count
1919
COROUTINE_COUNT_IP_CHECK = 20
2020

21+
WEB = {
22+
'host': '0.0.0.0',
23+
'port': 8008
24+
}
2125
# Config
2226
REDIS = {
2327
'address': '127.0.0.1:6379',
@@ -61,6 +65,10 @@ def load(cls):
6165
if redis:
6266
cls.REDIS.update(redis)
6367

68+
web = configs.get('web')
69+
if redis:
70+
cls.WEB.update(web)
71+
6472
app = configs.get('app')
6573
if app:
6674
cls.APP_ENV = app.get('env', cls.APP_ENV)

src/app/web.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from sanic import Sanic
2+
from sanic.response import json
3+
from multiprocessing import Process
4+
5+
from src.app.ip_factory import IPFactory
6+
from src.app.main import Config
7+
8+
app = Sanic()
9+
10+
11+
class Web(Process):
12+
13+
def run(self):
14+
app.run(**Config.WEB)
15+
16+
17+
@app.route('/get_ip')
18+
async def get_ip(request):
19+
ip = await IPFactory.get_random_ip(bool(request.raw_args.get('https', False)))
20+
return json({
21+
'ip': ip.ip,
22+
'port': ip.port,
23+
# 'https': ip.https,
24+
'http': ip.to_http(),
25+
})

src/lib/structs.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44

55
class IPData(DataHelper):
6-
ip: str
7-
port: int
8-
delay: float
9-
http: bool
10-
https: bool
6+
ip: str = ''
7+
port: int = 0
8+
delay: float = 0.0
9+
http: bool = False
10+
https: bool = False
1111
score: float = Config.DEFAULT_SCORE
1212

1313
@classmethod

0 commit comments

Comments
 (0)