feat: add settings file
This commit is contained in:
parent
d0c896de88
commit
4dc4ec24ae
|
@ -39,7 +39,7 @@ https://count.dsrkafuu.net/{id}?theme={asoul,gelbooru,moebooru,rule34}&length={1
|
|||
3. `{&length}`: Number between `1-10` (default: `7`) or string `auto`
|
||||
4. `{&add}`: Controls whether make the counter count or not (default: `1`)
|
||||
|
||||
Make a pull request to add your id in `ids.json` to use the free public counter. Recommend to use `user:usage` like string as ID for better management and potential issue tracking.
|
||||
Make a pull request to add your id in `settings.json` to use the free public counter. Recommend to use `user:usage` like string as ID for better management and potential issue tracking.
|
||||
|
||||
**API Endpoints**
|
||||
|
||||
|
@ -63,7 +63,8 @@ DELETE is not enabled in public counter, create a issue if you need to use it.
|
|||
1. Create a Cloudflare Workers worker
|
||||
2. Create a Cloudflare Workers KV store
|
||||
3. Create your own `wrangler.toml` based on the `wrangler.example.toml`
|
||||
4. Build the worker and publish it using `wrangler publish`
|
||||
4. Modify `settings.json` in your preferred way
|
||||
5. Build the worker and publish it using `wrangler publish`
|
||||
|
||||
## Credits
|
||||
|
||||
|
|
4
ids.json
4
ids.json
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"dsrkafuu:demo": "Demo for GitHub & preview site.",
|
||||
"dsrkafuu:home": "Personal blog & GitHub page."
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"private": true,
|
||||
"name": "moe-counter-cf",
|
||||
"version": "2.2.0",
|
||||
"version": "2.3.0",
|
||||
"description": "Fork of Moe Counter for fast global access powered by Cloudflare Workers.",
|
||||
"author": "DSRKafuU <dsrkafuu@outlook.com> (https://dsrkafuu.net)",
|
||||
"license": "MIT",
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"defaults": {
|
||||
"theme": "gelbooru",
|
||||
"length": 7
|
||||
},
|
||||
"api": {
|
||||
"get": true,
|
||||
"delete": false
|
||||
},
|
||||
"index": {
|
||||
"enabled": true,
|
||||
"analytics": "G-0000000000"
|
||||
},
|
||||
"ids": {
|
||||
"dsrkafuu:demo": "Demo for GitHub & preview site.",
|
||||
"dsrkafuu:home": "Personal blog & GitHub page."
|
||||
}
|
||||
}
|
17
src/index.js
17
src/index.js
|
@ -1,3 +1,4 @@
|
|||
import settings from '../settings.json';
|
||||
import { Router } from 'itty-router';
|
||||
import { genErrorResponse, ResError } from './response';
|
||||
import { withCORS } from './middlewares';
|
||||
|
@ -9,14 +10,22 @@ import * as api from './routes/api';
|
|||
|
||||
const router = Router();
|
||||
router.options('*', withCORS);
|
||||
router.get('/', index.get);
|
||||
router.get('/favicon.ico', favicon.get);
|
||||
router.get('/robots.txt', robots.get);
|
||||
|
||||
// enable index page
|
||||
if (settings.index) {
|
||||
router.get('/favicon.ico', favicon.get);
|
||||
router.get('/', index.get);
|
||||
}
|
||||
|
||||
// routes
|
||||
router.get('/:id', image.get);
|
||||
router.get('/api/:id', api.get);
|
||||
// router.delete('/api/:id', api.del);
|
||||
if (settings.api.get) {
|
||||
router.get('/api/:id', api.get);
|
||||
}
|
||||
if (settings.api.delete) {
|
||||
router.delete('/api/:id', api.del);
|
||||
}
|
||||
|
||||
// 404
|
||||
router.all('*', async () => {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/* global KV */
|
||||
import settings from '../../settings.json';
|
||||
import themes from '../../themes';
|
||||
import { genResponse } from '../response';
|
||||
import { validateID, minify } from '../utils';
|
||||
|
@ -43,10 +44,10 @@ function genImage(count, theme, length) {
|
|||
export async function get(req, event) {
|
||||
const id = validateID(req.params.id);
|
||||
let { theme, length, add } = req.query;
|
||||
if (!themes[theme]) {
|
||||
theme = 'gelbooru';
|
||||
if (!theme || !themes[theme]) {
|
||||
theme = settings.defaults.theme;
|
||||
}
|
||||
let _length = length;
|
||||
let _length = length || settings.defaults.length;
|
||||
if (length === 'auto') {
|
||||
_length = 'auto';
|
||||
} else if (!length || length <= 0 || length > 10) {
|
||||
|
|
|
@ -61,6 +61,18 @@
|
|||
margin-top: 16px;
|
||||
}
|
||||
</style>
|
||||
<script
|
||||
async
|
||||
src="https://www.googletagmanager.com/gtag/js?id={{GAID}}"
|
||||
></script>
|
||||
<script>
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag() {
|
||||
dataLayer.push(arguments);
|
||||
}
|
||||
gtag('js', new Date());
|
||||
gtag('config', '{{GAID}}');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
|
@ -1,4 +1,5 @@
|
|||
import html from '../index.html';
|
||||
import settings from '../../settings.json';
|
||||
import html from './index.html';
|
||||
import { genResponse } from '../response';
|
||||
import { minify } from '../utils';
|
||||
|
||||
|
@ -6,10 +7,14 @@ import { minify } from '../utils';
|
|||
* @param {Request} req
|
||||
*/
|
||||
export async function get(req) {
|
||||
return await genResponse(req, minify(html), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'text/html; charset=utf-8',
|
||||
},
|
||||
});
|
||||
return await genResponse(
|
||||
req,
|
||||
minify(html).replace(/{{GAID}}/g, settings.index.analytics),
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'text/html; charset=utf-8',
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import settings from '../settings.json';
|
||||
import { encode } from 'base64-arraybuffer';
|
||||
import { ResError } from './response';
|
||||
import ids from '../ids.json';
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
|
@ -19,7 +19,7 @@ export function validateID(id) {
|
|||
if (!/^[a-z0-9:.@_-]{1,256}$/i.test(id)) {
|
||||
throw new ResError(400, 'Invalid Counter ID');
|
||||
}
|
||||
if (!ids[id]) {
|
||||
if (!settings.ids[id]) {
|
||||
throw new ResError(400, 'Unregistered Counter ID');
|
||||
}
|
||||
return id;
|
||||
|
|
Loading…
Reference in New Issue