mirror of
https://github.com/easychen/pushdeer.git
synced 2025-02-23 00:14:56 +08:00
添加mqtt的dockerfile
This commit is contained in:
parent
7677c81ce8
commit
2310265c77
20
docker/mqtt/Dockerfile
Normal file
20
docker/mqtt/Dockerfile
Normal file
@ -0,0 +1,20 @@
|
||||
FROM node:16-alpine3.15
|
||||
|
||||
RUN apk --no-cache add mosquitto mosquitto-clients
|
||||
RUN npm install -g forever
|
||||
|
||||
ADD mosquitto.conf /mosquitto.conf
|
||||
RUN /usr/sbin/mosquitto -c /mosquitto.conf -d
|
||||
|
||||
# 测试时注释掉下一行
|
||||
COPY api /api
|
||||
|
||||
COPY init.sh /init.sh
|
||||
RUN chmod +x /init.sh
|
||||
EXPOSE 1883
|
||||
EXPOSE 80
|
||||
|
||||
ENTRYPOINT ["/bin/sh", "/init.sh"]
|
||||
|
||||
|
||||
# ENTRYPOINT ["/usr/sbin/mosquitto", "-c", "/mosquitto.conf",""]
|
20
docker/mqtt/Dockerfile.dev
Normal file
20
docker/mqtt/Dockerfile.dev
Normal file
@ -0,0 +1,20 @@
|
||||
FROM node:16-alpine3.15
|
||||
|
||||
RUN apk --no-cache add mosquitto mosquitto-clients
|
||||
RUN npm install -g forever
|
||||
|
||||
ADD mosquitto.conf /mosquitto.conf
|
||||
RUN /usr/sbin/mosquitto -c /mosquitto.conf -d
|
||||
|
||||
# 测试时注释掉下一行
|
||||
# COPY api /api
|
||||
|
||||
COPY init.sh /init.sh
|
||||
RUN chmod +x /init.sh
|
||||
EXPOSE 1883
|
||||
EXPOSE 80
|
||||
|
||||
ENTRYPOINT ["/bin/sh", "/init.sh"]
|
||||
|
||||
|
||||
# ENTRYPOINT ["/usr/sbin/mosquitto", "-c", "/mosquitto.conf",""]
|
16
docker/mqtt/RoboFile.php
Normal file
16
docker/mqtt/RoboFile.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* This is project's console commands configuration for Robo task runner.
|
||||
*
|
||||
* @see http://robo.li/
|
||||
*/
|
||||
class RoboFile extends \Robo\Tasks
|
||||
{
|
||||
// define public methods as commands
|
||||
public function buildImage()
|
||||
{
|
||||
$this->_exec("docker build -t pushdeeresp ./ ");
|
||||
$this->_exec("docker tag pushdeeresp ccr.ccs.tencentyun.com/ftqq/pushdeeresp");
|
||||
$this->_exec("docker push ccr.ccs.tencentyun.com/ftqq/pushdeeresp");
|
||||
}
|
||||
}
|
1
docker/mqtt/api/.gitignore
vendored
Normal file
1
docker/mqtt/api/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
68
docker/mqtt/api/index.js
Normal file
68
docker/mqtt/api/index.js
Normal file
@ -0,0 +1,68 @@
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const timeout = require('connect-timeout')
|
||||
const app = express();
|
||||
|
||||
var multer = require('multer');
|
||||
var forms = multer();
|
||||
const bodyParser = require('body-parser')
|
||||
app.use(bodyParser.json());
|
||||
app.use(forms.array());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
app.get(`/`, (req, res) => {
|
||||
res.sendFile(path.join(__dirname, 'index.html'));
|
||||
});
|
||||
|
||||
function haltOnTimedout (req, res, next) {
|
||||
if (!req.timedout) next()
|
||||
}
|
||||
|
||||
app.all('/send', timeout('3s'), haltOnTimedout, async (req, res) => {
|
||||
|
||||
console.log( process.env , req.body, req.query );
|
||||
if( process.env.API_KEY )
|
||||
{
|
||||
const in_key = req.body.key||req.query.key || "";
|
||||
if( in_key != process.env.API_KEY ) return res.status(403).send(`api key error `+ in_key );
|
||||
}
|
||||
|
||||
const msg_type = (req.body.type || req.query.type) == "bg_url" ? "bg_url" : "text";
|
||||
let msg_content = req.body.content || req.query.content || "";
|
||||
if( msg_type== 'bg_url' && msg_content.length < 1 ) res.status(500).send('Bg url cannot be empty');
|
||||
if( msg_type== 'bg_url' ) msg_content = msg_content.replace("https://","http://");
|
||||
const msg_topic = req.body.topic || req.query.topic || process.env.MQTT_BASE_TOPIC;
|
||||
|
||||
|
||||
const mqtt = require('async-mqtt') // require mqtt
|
||||
const mqtt_url = 'mqtt://'+process.env.MQTT_USER+':'+ process.env.MQTT_PASSWORD +'@127.0.0.1:'+process.env.MQTT_PORT;
|
||||
console.log( mqtt_url );
|
||||
const client = mqtt.connect('mqtt://127.0.0.1:'+(process.env.MQTT_PORT||'1883'), {
|
||||
clean: true,
|
||||
connectTimeout: 2800,
|
||||
clientId: 'DeerHttpApi',
|
||||
username: process.env.MQTT_USER||"",
|
||||
password: process.env.MQTT_PASSWORD||"",
|
||||
});
|
||||
try {
|
||||
await client.publish(msg_topic+'_'+msg_type,msg_content);
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}finally {
|
||||
await client.end();
|
||||
}
|
||||
|
||||
|
||||
return res.status(200).json('done');
|
||||
});
|
||||
|
||||
// Error handler
|
||||
app.use(function (err, req, res, next) {
|
||||
console.error(err);
|
||||
res.status(500).send('Internal Serverless Error');
|
||||
});
|
||||
|
||||
app.listen(80, () => {
|
||||
console.log(`Server start on http://localhost`);
|
||||
});
|
16
docker/mqtt/api/package.json
Normal file
16
docker/mqtt/api/package.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "api",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"async-mqtt": "^2.6.2",
|
||||
"body-parser": "^1.19.1",
|
||||
"connect-timeout": "^1.9.0",
|
||||
"express": "^4.17.2",
|
||||
"multer": "^1.4.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.15"
|
||||
}
|
||||
}
|
1496
docker/mqtt/api/yarn.lock
Normal file
1496
docker/mqtt/api/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
17
docker/mqtt/docker-compose.dev.yml
Normal file
17
docker/mqtt/docker-compose.dev.yml
Normal file
@ -0,0 +1,17 @@
|
||||
version: '2'
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: ./
|
||||
dockerfile: ./Dockerfile.dev
|
||||
volumes:
|
||||
- './api:/api'
|
||||
ports:
|
||||
- '80:80'
|
||||
- '1883:1883'
|
||||
environment:
|
||||
- API_KEY=aPiKe1
|
||||
- MQTT_USER=easy
|
||||
- MQTT_PASSWORD=y0urp@ss
|
||||
- MQTT_PORT=1883
|
||||
- MQTT_BASE_TOPIC=default
|
17
docker/mqtt/docker-compose.yml
Normal file
17
docker/mqtt/docker-compose.yml
Normal file
@ -0,0 +1,17 @@
|
||||
version: '2'
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: ./
|
||||
dockerfile: ./Dockerfile
|
||||
# volumes:
|
||||
# - './api:/api'
|
||||
ports:
|
||||
- '80:80'
|
||||
- '1883:1883'
|
||||
environment:
|
||||
- API_KEY=aPiKe1
|
||||
- MQTT_USER=easy
|
||||
- MQTT_PASSWORD=y0urp@ss
|
||||
- MQTT_PORT=1883
|
||||
- MQTT_BASE_TOPIC=default
|
10
docker/mqtt/init.sh
Normal file
10
docker/mqtt/init.sh
Normal file
@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
|
||||
# 生成密码文件
|
||||
echo "$MQTT_USER:$MQTT_PASSWORD" > '/mospass.txt'
|
||||
mosquitto_passwd -U /mospass.txt
|
||||
|
||||
# 启动 express
|
||||
forever start /api/index.js
|
||||
# mosquitto -c /mosquitto.conf -v
|
||||
mosquitto -c /mosquitto.conf
|
4
docker/mqtt/mosquitto.conf
Normal file
4
docker/mqtt/mosquitto.conf
Normal file
@ -0,0 +1,4 @@
|
||||
listener 1883
|
||||
allow_anonymous false
|
||||
log_dest stdout
|
||||
password_file /mospass.txt
|
Loading…
x
Reference in New Issue
Block a user