WXGame/sever/server.go
DESKTOP-DDTRVOR\asus 9c94786d7c 保存服务器代码
2025-01-08 15:52:20 +08:00

42 lines
975 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"net/http"
)
func main() {
// 设置路由和处理函数
http.HandleFunc("/get-value", func(w http.ResponseWriter, r *http.Request) {
// 检查请求方法是否为GET
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
// 从请求参数中获取"key"的值
key := r.URL.Query().Get("key")
if key == "" {
http.Error(w, "Missing 'key' parameter", http.StatusBadRequest)
return
}
// 根据key返回相应的值这里可以替换为实际的业务逻辑
value := fmt.Sprintf("Value for key '%s'", key)
// 设置响应头
w.Header().Set("Content-Type", "text/plain")
// 返回响应值
w.WriteHeader(http.StatusOK)
w.Write([]byte(value))
})
// 启动HTTP服务器
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}