2024-04-01 09:42:29 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"go-bot/config"
|
|
|
|
"go-bot/utils"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handlePost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Error reading request body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-04-03 22:04:03 +08:00
|
|
|
// fmt.Println("handlePost:", string(body))
|
2024-04-01 09:42:29 +08:00
|
|
|
// 解码 JSON 数据到 map[string]interface{} 类型的变量
|
|
|
|
var data map[string]interface{}
|
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error decoding JSON:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
utils.Router(data)
|
|
|
|
w.Write([]byte("JSON data received successfully!"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-04-03 21:53:57 +08:00
|
|
|
APIURL := config.GlobalConfig.Server.APIURL
|
|
|
|
|
2024-04-03 22:02:08 +08:00
|
|
|
// PORT := config.GlobalConfig.Server.Port
|
|
|
|
// fmt.Println(APIURL)
|
|
|
|
// fmt.Println(PORT)
|
|
|
|
http.HandleFunc("/", handlePost)
|
|
|
|
// 协程支持
|
|
|
|
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// go handlePost(w, r)
|
|
|
|
// })
|
2024-04-01 09:42:29 +08:00
|
|
|
|
|
|
|
fmt.Println("Server listening on port 5580...")
|
2024-04-03 21:53:57 +08:00
|
|
|
// APIURL 为 go-cqhttp 配置的事件上报地址
|
|
|
|
http.ListenAndServe(APIURL, nil)
|
2024-04-01 09:42:29 +08:00
|
|
|
}
|