Merge pull request #3 from d1y/master

add default mongodb url && return json data REST api
This commit is contained in:
Jad 2020-08-22 21:23:16 +08:00 committed by GitHub
commit 7e956bc45d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 15 deletions

View File

@ -3,7 +3,10 @@
const mongoose = require('mongoose') const mongoose = require('mongoose')
const schema = require('./schema') const schema = require('./schema')
mongoose.connect(process.env.DB_URL, { // the default mongodb url (local server)
const mongodbURL = process.env.DB_URL || 'mongodb://127.0.0.1:27017'
mongoose.connect(mongodbURL, {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
useFindAndModify: false useFindAndModify: false
@ -33,4 +36,4 @@ module.exports = {
getNum, getNum,
getAll, getAll,
setNum setNum
} }

View File

@ -11,6 +11,7 @@ const themify = require('./utils/themify')
const PLACES = 7 const PLACES = 7
const app = express() const app = express()
app.use(express.static('assets')) app.use(express.static('assets'))
app.use(compression()) app.use(compression())
app.set('view engine', 'pug') app.set('view engine', 'pug')
@ -21,9 +22,9 @@ app.get('/', (req, res) => {
// get the image // get the image
app.get('/get/@:name', async (req, res) => { app.get('/get/@:name', async (req, res) => {
const name = req.params.name const { name } = req.params
const theme = req.query.theme || 'moebooru' const { theme = 'moebooru' } = req.query
let length = PLACES, count = 0 let length = PLACES
// This helps with GitHub's image cache // This helps with GitHub's image cache
res.set({ res.set({
@ -31,23 +32,29 @@ app.get('/get/@:name', async (req, res) => {
'cache-control': 'max-age=0, no-cache, no-store, must-revalidate' 'cache-control': 'max-age=0, no-cache, no-store, must-revalidate'
}) })
const data = await getCountByName(name)
if (name === 'demo') { if (name === 'demo') {
res.set({ res.set({
'cache-control': 'max-age=31536000' 'cache-control': 'max-age=31536000'
}) })
count = '0123456789'
length = 10 length = 10
} else {
const counter = await db.getNum(name) || { name, num: 0 }
count = counter.num + 1
db.setNum(counter.name, count)
console.log(counter, `theme: ${theme}`)
} }
// Send the generated SVG as the result // Send the generated SVG as the result
res.send(themify.getCountImage({ count, theme, length })) const renderSvg = themify.getCountImage({ count: data.num, theme, length })
res.send(renderSvg)
console.log(data, `theme: ${theme}`)
})
// JSON record
app.get('/record/@:name', async (req, res) => {
const { name } = req.params
const data = await getCountByName(name)
res.json(data)
}) })
app.get('/heart-beat', (req, res) => { app.get('/heart-beat', (req, res) => {
@ -59,6 +66,24 @@ app.get('/heart-beat', (req, res) => {
console.log('heart-beat') console.log('heart-beat')
}); });
const listener = app.listen(config.app.port, () => { const listener = app.listen(config.app.port || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port) console.log('Your app is listening on port ' + listener.address().port)
}) })
async function getCountByName(name) {
const defaultCount = { name, num: 0 }
if (name === 'demo') return { name, num: '0123456789' }
try {
const counter = await db.getNum(name) || defaultCount
const num = counter.num + 1
db.setNum(counter.name, num)
return counter
} catch (error) {
console.log("get count by name is error: ", error)
return defaultCount
}
}