api_cache.js 1.42 KB


'use strict';

import Router from 'koa-router';
import _ from 'lodash';

import {
    MemcachedHost
} from '../../models';

import ApiCache from '../../ci/api_cache';

let r = new Router();

const api_cache = {
    async index(ctx) {
        let hosts = await MemcachedHost.findAll();

        await ctx.render('action/api_cache', {hosts: hosts});
    },

    async addHost(ctx) {
        let q = ctx.request.body;

        await MemcachedHost.insert({host: q.host});
        return ctx.body = {
            code: 200
        };
    },

    async delHost(ctx) {
        let q = ctx.request.body;

        await MemcachedHost.removeById(q.id);
        return ctx.body = {
            code: 200
        };
    },

    async cleanKey(ctx) {
        let q = ctx.request.body;
        let key = q.key;

        let hosts = await MemcachedHost.findAll();

        _.each(hosts, (h) => {
            (new ApiCache(h.host)).clean(key);
        });

        return ctx.body = {
            code: 200
        };
    },

    async flushAll(ctx) {
        let hosts = await MemcachedHost.findAll();

        _.each(hosts, (h) => {
            (new ApiCache(h.host)).flushAll();
        });

        return ctx.body = {
            code: 200
        };
    }
};

r.get('/', api_cache.index);
r.post('/host/add', api_cache.addHost);
r.post('/host/del', api_cache.delHost);
r.post('/clean', api_cache.cleanKey);
r.post('/flush', api_cache.flushAll);

export default r;