feat: add settings file

This commit is contained in:
dsrkafuu 2022-03-17 16:08:50 +08:00
parent d0c896de88
commit 4dc4ec24ae
9 changed files with 65 additions and 23 deletions

View File

@ -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

View File

@ -1,4 +0,0 @@
{
"dsrkafuu:demo": "Demo for GitHub & preview site.",
"dsrkafuu:home": "Personal blog & GitHub page."
}

View File

@ -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",

18
settings.json Normal file
View File

@ -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."
}
}

View File

@ -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 () => {

View File

@ -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) {

View File

@ -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>

View File

@ -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',
},
}
);
}

View File

@ -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;