背景介绍
公司使用了阿里云的CDN,有时业务方需要及时刷新页面缓存。为减轻工作,决定做一个简易的网站供开发人员调用阿里云的 API,刷新缓存。
编写项目
修改 settings.py
# 添加可访问的主机
ALLOWED_HOSTS = ['*']
# 注释
#'django.middleware.csrf.CsrfViewMiddleware',
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
修改 urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('cdn/', views.aliyun_cdn),
path('result/', views.result),
]
修改 views.py
from django.shortcuts import render
import json
# Create your views here.
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkdcdn.request.v20180115.RefreshDcdnObjectCachesRequest import RefreshDcdnObjectCachesRequest
from aliyunsdkdcdn.request.v20180115.DescribeDcdnRefreshTasksRequest import DescribeDcdnRefreshTasksRequest
client = AcsClient('aaaaaaaaaaa', 'bbbbbbbbb', 'cn-hangzhou')
# 获取 url 刷新缓存
def aliyun_cdn(req):
if req.method == "POST":
request = RefreshDcdnObjectCachesRequest()
request.set_accept_format('json')
msg = req.POST.get("ibaurl", None)
request.set_ObjectPath(msg)
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))
return render(req, "index.html", {})
# 获取刷新结果,只显示 30 条
def result(req):
request = DescribeDcdnRefreshTasksRequest()
request.set_accept_format('json')
response = client.do_action_with_exception(request)
dict_str = json.loads(str(response, encoding='utf-8'))
dic_data = dict_str["Tasks"]["Task"]
return render(req, "result.html", {'dic_data': dic_data})
在 app01 下增加 static 文件夹,把 jquery.min.js 放入
在 templates 文件夹下添加 html 文件
添加 base.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>洋老板</title>
<!-- Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
<script src="/static/jquery.min.js"></script>
</head>
<body style="margin-left: 20px; margin-right: 20px">
<h1>商城 CDN 刷新平台</h1>
<ul class="nav nav-tabs">
<li id="huancun" role="presentation" class="active"><a href="/cdn/">刷新缓存</a></li>
<li id="jilu" role="presentation"><a href="/result/">操作记录</a></li>
</ul>
{% block content %}
{% endblock %}
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</body>
</html>
添加 index.html
{% extends "base.html" %}
{% block content %}
<form action="/cdn/" method="post" style="margin-top: 10px">
<div class="form-group">
<label>需要刷新的 URL </label>
<input type="text" class="form-control" placeholder="URL" name="ibaurl" style=" 40%">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>
{% endblock %}
添加 result.html
{% extends "base.html" %}
{% block content %}
<h4>结果</h4>
<table class="table table-bordered table-hover">
<thead></thead>
<tbody>
<tr class="success">
<td>操作内容</td>
<td>操作时间</td>
<td>状态</td>
<td>进度</td>
</tr>
{% for dic in dic_data %}
<tr>
<td>{{ dic.ObjectPath }}</td>
<td>{{ dic.CreationTime }}</td>
<td>{{ dic.Status }}</td>
<td>{{ dic.Process }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
$(function () {
$('#jilu').addClass('active');
$('#huancun').removeClass('active')
})
</script>
{% endblock %}
环境搭建
mkdir /data/ali_cdn && cd /data/ali_cdn
# 编辑模版,安装需要用的软件
vi ali_cdn.df
FROM python:3.5
MAINTAINER from klvchen
RUN pip install django && pip install aliyun-python-sdk-dcdn && pip install alibaba-cloud-python-sdk-v2 && pip install aliyun-python-sdk-core-v3 && apt-get clean
# 创建镜像
docker build -f ali_cdn.df -t python_django:19.12.0 .
# 编写 docker-compose.yml
cd /data/ali_cdn
# 项目代码放在 /data/ali_cdn/aliyun_cdn 目录下
vi docker-compose.yml
version: '3.4'
services:
klvchen:
image: python_django:19.12.0
ports:
- 8001:8001
command:
- /bin/bash
- -c
- |
cd /aliyun_cdn
python manage.py runserver 0.0.0.0:8001
volumes:
- /data/ali_cdn/aliyun_cdn:/aliyun_cdn
# 启动项目
docker-compose up -d
访问 http://IP:8001/cdn/