Pfapi plugin uses local and redis caches to achieve single digit milliseconds on average api response time.

Overview

Strapi plugin pfapi

Pfapi plugin provides configurable, secure and fast API services. APIs are configurable through the admin panel with components and dynamic zone. Pfapi uses local and Redis caches to achieve single-digit milliseconds on average API response time. IP allow list, block list and Rate limits mechanisms are included and conveniently accessible.

Here are some test results for cached vs no cache comparison:

test 1 test 2 test 3
cached api response time 3.13 0.97 1.07
no cache api response time 16.93 16.74 16.72

test results

We can see pfapi plugin improves the api response time to single digit milliseconds range. For detail, please read pfapi-tester and the section at bottom.

how to install

yarn add strapi-plugin-pfapi

config Redis URI

By default, if it is not set, Pfapi uses redis://localhost/0.

You can set it to a different host and database number by providing REDIS_URI in the plugins config file.

For Redis cluster config, here is an example:

REDIS_URI=redis://172.31.23.70:6379,172.31.30.210:6379,172.31.22.214:6379/0

the plugins config file is located at:

config/plugins.js

module.exports = ({ env }) => ({
  //...
  pfapi: {
    enabled: true,
    config: {
      redis_uri: env('REDIS_URI'),
    }
  }
  //...
})

API parameters

The same Strapi API parameters: sort, filters, populate, fields, pagination and publicationState works for Pfapi.

Security Defense

1) IP unlimited list and blocked list

PfapiIp conveniently provides access to IP unlimited list and blocked list mechanism. IPs in unlimited list will not check rate limits. IPs in blocked list will not have access to the prefix.

2) Rate limits for API calls

PfapiRateLimit provides access to the rate limits mechanism. rate limits can set with IP Mask and prefix.

(it is not the same as the rate limits that come with strapi)

Changes made to the two collections are effective immediately without restarting strapi servers.

Without enabling the defense middleware of pfapi plugin, the above mechanisms work only for Pfapi APIs.

To enable the defense middleware and cover all routes:

config/middlewares.js

module.exports = [
  //...
  'plugin::pfapi.defense',
];

EJS template for text and richtext component fields

We can use the EJS template to customize String fields in the api response JSON object.

For example:

In the northern-city handle, Northern City - <%= item.name %> is the title

How to use

components and dynamic zone

The plugin uses the strapi-pfapi library. With the world cities test data set provided by plugin strapi-plugin-pfapi-data, we can run a few API calls to demonstrate the idea.

step 1 install Redis server

Refer to: install redis server on your local computer.

step 2 create strapi app

yarn create strapi-app strapi-pfapi-app --quickstart 

After creating and logging into your Strapi account from the browser, stop the strapi server.

step 3 install strapi-plugin-pfapi and strapi-plugin-pfapi-data

You don't have to install strapi-plugin-pfapi-data for your production.

strapi-plugin-pfapi-data provides a test data test for demo and test

cd strapi-pfapi-app

yarn add strapi-plugin-pfapi strapi-plugin-pfapi-data

yarn develop

Admin Panel

step 4 setup api_key and permissions

get your api_key from:

http://localhost:1337/admin/content-manager/collectionType/plugin::pfapi.pfapi-key?page=1&pageSize=10&sort=key:ASC

A role with name PfapiDemo is installed in the above steps.

Go to Settings > USERS & PERMISSIONS PLUGIN > Roles:

PfapiDemo

http://localhost:1337/admin/settings/users-permissions/roles

click on PfapiDemo,

Under Permissions > World-city

assign find and findOne permissions to PfapiDemo and click save.

OK, we are ready to run tests, please replace Pfapi-Demo-XXXXXXXX with your specific key or set your api_key to Pfapi-Demo-XXXXXXXX.

step 5 demos

a) tests content-type name world-cities as path variable

http://localhost:1337/pfapi/world-cities?api_key=Pfapi-Demo-XXXXXXXX

http://localhost:1337/pfapi/world-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

b) tests config handle northern-cities as path variable

handle configs are defined in PfapiHandle.

/pfapi

http://localhost:1337/pfapi/northern-cities?api_key=Pfapi-Demo-XXXXXXXX

http://localhost:1337/pfapi/northern-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

/pfapi/pf

http://localhost:1337/pfapi/pf/northern-cities?api_key=Pfapi-Demo-XXXXXXXX

http://localhost:1337/pfapi/pf/northern-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

strapi api parameters

http://localhost:1337/pfapi/northern-cities?filters[iso3]=USA&api_key=Pfapi-Demo-XXXXXXXX

c) tests with config handle northern-city with id_field is name

config data defined in PfapiHandles for handle northern-city:

http://localhost:1337/pfapi/pf/northern-city/Anchorage?api_key=Pfapi-Demo-XXXXXXXX

d) test data update

goto http://localhost:1337/admin/content-manager/collectionType/api::world-city.world-city/2148

make some change, for example: change the population from 288000 to 288001

check APIs:

http://localhost:1337/pfapi/northern-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

http://localhost:1337/pfapi/pf/northern-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

http://localhost:1337/pfapi/pf/northern-city/Anchorage?api_key=Pfapi-Demo-XXXXXXXX

to see if the cached data was evicted and updated

e) test config update

goto http://localhost:1337/admin/content-manager/collectionType/plugin::pfapi.pfapi-handle/1

make some changes, for example: add or remove country to the fields array

check APIs:

http://localhost:1337/pfapi/northern-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

http://localhost:1337/pfapi/pf/northern-cities/2148?api_key=Pfapi-Demo-XXXXXXXX

pfapi-tester

pfapi-tester is the stress tester for pfapi plugin. please refer to pfapi-tester readme for detail.

We can use it to get api capacity metrics of APIs that use pfapi plugin.

Here are the raw data collected for the cached vs no cache comparison:

# 3 tests for cached data

> pfapi-tester -t 10 
++++++++++
{
  base_url: '...',
  path: '/pfapi/pf/northern-cities',
  times: 10
}
total: 10 ok: 10 not_ok: 0
------------------------------
   	pfapi	http	total
------------------------------
ave	3.13	4.24	134.44
min	0.88	1.78	94.34
max	21.14	22.36	229.76
------------------------------

> pfapi-tester -t 10 
++++++++++
{
  base_url: '...',
  path: '/pfapi/pf/northern-cities',
  times: 10
}
total: 10 ok: 10 not_ok: 0
------------------------------
   	pfapi	http	total
------------------------------
ave	0.97	2.05	125.62
min	0.89	1.82	101.23
max	1.05	3.24	178.90
------------------------------

> pfapi-tester -t 10 
++++++++++
{
  base_url: '...',
  path: '/pfapi/pf/northern-cities',
  times: 10
}
total: 10 ok: 10 not_ok: 0
------------------------------
   	pfapi	http	total
------------------------------
ave	1.07	2.09	132.43
min	0.91	1.79	99.12
max	1.96	2.97	182.29
------------------------------

# 3 tests for no cache data

> pfapi-tester -t 10 -r
++++++++++
{
  base_url: '...',
  path: '/pfapi/pf/northern-cities',
  times: 10,
  ss_rand: true
}
total: 10 ok: 10 not_ok: 0
------------------------------
   	pfapi	http	total
------------------------------
ave	16.93	17.90	142.81
min	16.33	17.27	109.83
max	18.07	18.94	192.21
------------------------------

> pfapi-tester -t 10 -r
++++++++++
{
  base_url: '...',
  path: '/pfapi/pf/northern-cities',
  times: 10,
  ss_rand: true
}
total: 10 ok: 10 not_ok: 0
------------------------------
   	pfapi	http	total
------------------------------
ave	16.74	17.79	147.02
min	16.19	17.13	112.83
max	17.99	19.07	187.56
------------------------------

> pfapi-tester -t 10 -r
++++++++++
{
  base_url: '...',
  path: '/pfapi/pf/northern-cities',
  times: 10,
  ss_rand: true
}
total: 10 ok: 10 not_ok: 0
------------------------------
   	pfapi	http	total
------------------------------
ave	16.72	17.70	135.34
min	16.22	17.15	109.03
max	17.44	18.31	189.94
------------------------------

You might also like...

A jQuery plugin to submit forms with files via AJAX and to get a response with errors.

jquery-ajaxform A jQuery plugin to submit form with files via AJAX and to get a response with errors. Browsers without FormData uses iframe transport

Mar 30, 2021

Use real-time computing technology and web technology to build a big data Kanban l to solve the problem. Among them, practical technologies include MySQL, Kafka, Flink, Redis, Flask and Echarts

Use real-time computing technology and web technology to build a big data Kanban l to solve the problem. Among them, practical technologies include MySQL, Kafka, Flink, Redis, Flask and Echarts

实时计算(English Version) 运用实时计算技术、Web 技术构建一个大数据看板来解决问题。其中实用技术包括Mysql、Kafka、Flink、Redis、Flask和Echarts 目录 1.问题需求 2.方案分析 3.安装环境 4.环境启动命令和运行代码的方法 5.代码目录结构说明

Jan 8, 2022

An app to manage tasks. A user can add, delete and edit a task and mark it as completed, It uses simple GUI and relies on DOM manipulation in pure JS and using local storage.

An app to manage tasks. A user can add, delete and edit a task and mark it as completed, It uses simple GUI and relies on DOM manipulation in pure JS and using local storage.

An app to manage tasks. A user can add, delete and edit a task and mark it as completed, It uses simple GUI and relies on DOM manipulation in pure JS and using local storage.

Aug 20, 2022

On this page, you can save and load all the awesome books you have and save the name and the author into the local storage. this project uses Javascript to interact with the pages

Awesome Books: refactor to use JavaScript classes In this project, We add the links to the applications into the final project Getting Started if you

Nov 29, 2022

A simple single page web-app that stores, removes and edits books' names in the browser's local storage.

A simple single page web-app that stores, removes and edits books' names in the browser's local storage.

A simple single page web-app that stores, removes and edits books' names in the browser's local storage. Made with Webpack bundler

Apr 1, 2022

Ping.js is a small and simple Javascript library for the browser to "ping" response times to web servers in Javascript

Ping.js Ping.js is a small and simple Javascript library for the browser to "ping" response times to web servers in Javascript! This is useful for whe

Dec 27, 2022

A tool to check for response status codes with ease

A tool to check for response status codes with ease

About Archer Archer is an cross-platform tool developed using Nodejs which focuses on the reconnaissance phase of a penetration test. Got a bunch of l

Oct 19, 2022

Room impulse response measurement in the browser.

Room impulse response measurement - in the browser! Check it out on danielrudrich.github.io/Broom. Behind the scenes Broom uses the WebAudioAPI to pla

Sep 13, 2022

Create a html response with JSX.

Htm Create a text/html response with JSX, powered by Preact and UnoCSS. Usage To use Htm, create a server.tsx file like this: /** @jsx h */ import {

Dec 9, 2022
Releases(v2.3.2)
  • v2.0.0(Jul 9, 2022)

    strapi-plugin-pfapi v2.0.0

    Pfapi plugin provides configurable, secure and fast API services. APIs are configurable through the admin panel with components and dynamic zone. Pfapi uses local and Redis caches to achieve single-digit milliseconds on average API response time. IP allow list, block list and Rate limits mechanisms are included and conveniently accessible.

    Source code(tar.gz)
    Source code(zip)
Owner
null
A high-resolution local database that uses precise algorithms to easily record data in local files within a project with persistent JSON and YAML support designed to be easy to set up and use

About A high-resolution local database that uses precise algorithms to easily record data in local files within a project with persistent JSON and YML

Shuruhatik 5 Dec 28, 2022
A bot to automatically DCA (Dollar-Cost Average) buy cryptocurrency on Binance

Binance DCA (Dollar-Cost-Averaging) Bot This bot allows you to sit back and relax while it automatically invests in cryptocurrency on the Binance exch

Luke Liasi 164 Jan 6, 2023
RedisInsight - Developer GUI for Redis, by Redis.

RedisInsight - Developer GUI for Redis, by Redis. RedisInsight is a visual tool that provides capabilities to design, develop and optimize your Redis

null 2.3k Dec 31, 2022
A fast and powerful http toolkit that take a list of domains to find active domains and other information such as status-code, title, response-time , server, content-type and many other

HTTPFY curently in beta so you may see problems. Please open a Issue on GitHub and report them! A Incredible fast and Powerful HTTP toolkit Report Bug

DevXprite 44 Dec 22, 2022
Improve Response Time 10X by Introducing an Interceptor In NestJS

Improve response times 10x by introducing an interceptor in NestJS This sample project demonstrates the usage of interceptors for improving response t

Dmitry Khorev 1 Oct 28, 2022
A plugin for Strapi Headless CMS that provides the ability to transform the API request or response.

strapi-plugin-transformer A plugin for Strapi that provides the ability to transform the API request and/or response. Requirements The installation re

daedalus 71 Jan 6, 2023
Text Engraving & Extrusion demo based on Three.js is implemented with Typescript and webpack5. Used THREE-CSGMesh as the core tech to achieve engraving and extrusion results

Text Engraving & Extrusion Text Engraving & Extrusion demo is implemented using Three.js, with Typescript and webpack5. Used THREE-CSGMesh as the core

Jiahong Li 3 Oct 12, 2022
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022
Habitapp is a simple app that helps you build positive habits, measure progress and achieve your goals

habitapp Habitapp is a simple app that helps you build positive habits, measure progress and achieve your goals. ✨ Features Working without registrati

null 6 Sep 11, 2022