go-bot/main.go
2024-04-03 22:04:03 +08:00

51 lines
1.1 KiB
Go

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
}
// fmt.Println("handlePost:", string(body))
// 解码 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() {
APIURL := config.GlobalConfig.Server.APIURL
// 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)
// })
fmt.Println("Server listening on port 5580...")
// APIURL 为 go-cqhttp 配置的事件上报地址
http.ListenAndServe(APIURL, nil)
}