func ApiHandle(w http.ResponseWriter, r *http.Request) { //fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) if r.Method == "POST" { /* b, err := ioutil.ReadAll(r.Body) if err != nil { log.Println("Read failed:", err) } defer r.Body.Close() value := gjson.Get(string(b), "COOKIE") println(value.String()) */ // cmd := &Cmd{} // err = json.Unmarshal(b, cmd) // if err != nil { // log.Println("json format error:", err) // } // log.Println("cmd:", cmd) } else { //log.Println("ONly support Post") fmt.Fprintf(w, "Only support post") } 代码含义
时间: 2024-04-11 21:29:57 浏览: 133
这段代码是 `ApiHandle` 函数的实现部分。根据代码,函数接受两个参数 `w http.ResponseWriter` 和 `r *http.Request`,分别表示 HTTP 响应写入器和 HTTP 请求。
函数首先检查请求的方法是否为 POST,如果是 POST 方法,则可以执行一些处理逻辑。但是,根据代码的注释,这部分处理逻辑被注释掉了。在实际使用时,可以根据需要解析请求的主体内容、对数据进行处理或执行其他逻辑操作。
如果请求的方法不是 POST,则函数会向客户端返回一个字符串 "Only support post",并通过 `fmt.Fprintf` 函数将该字符串写入 HTTP 响应写入器 `w`,最终返回给客户端。
需要注意的是,这段代码中的注释部分包含了一些可能是实际处理请求的逻辑,但由于被注释掉了,无法确定具体的处理内容。
相关问题
package main import ( "fmt" "html/template" "log" "net/http" "strings" ) func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) if r.Method == "GET" { t, err := template.ParseFiles("H:\\Goproject\\src\\web\\login.html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := t.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { _ = r.ParseForm() fmt.Println("username:", r.Form["username"]) fmt.Println("password:", r.Form["password"]) if pwd := r.Form.Get("password"); pwd == "123456" { fmt.Fprintf(w, "欢迎登陆,Hello %s!", r.Form.Get("username")) } else { fmt.Fprintf(w, "密码错误!") } } } func sayHello(w http.ResponseWriter, r *http.Request) { _ = r.ParseForm() //3 解析参数,默认不解析 fmt.Println(r.Form) //4 输出到服务器端的打印信息 fmt.Println("Path:", r.URL.Path) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("value", strings.Join(v, "")) } fmt.Fprintf(w, "Hello aoho") //5 写入到w的是输出到客户端的内容 } func main() { http.HandleFunc("/", login) http.HandleFunc("/sayHello", sayHello) //1 设置访问的路由 err := http.ListenAndServe(":8080", nil) //2 设置监听的端口 if err != nil { log.Fatal("ListenSever:", err) } } 修改为绝对路径可以运行,但是如何修改为相对路径
要将路径修改为相对路径,您需要使用`http.Dir`函数将文件夹路径转换为`http.FileServer`类型。这样可以指定要提供给用户的静态文件所在的文件夹。以下是对代码的修改:
```go
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
t, err := template.ParseFiles("login.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := t.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else {
_ = r.ParseForm()
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
if pwd := r.Form.Get("password"); pwd == "123456" {
fmt.Fprintf(w, "欢迎登陆,Hello %s!", r.Form.Get("username"))
} else {
fmt.Fprintf(w, "密码错误!")
}
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm() //3 解析参数,默认不解析
fmt.Println(r.Form) //4 输出到服务器端的打印信息
fmt.Println("Path:", r.URL.Path)
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("value", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello aoho") //5 写入到w的是输出到客户端的内容
}
func main() {
http.HandleFunc("/", login)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/sayHello", sayHello) //1 设置访问的路由
err := http.ListenAndServe(":8080", nil) //2 设置监听的端口
if err != nil {
log.Fatal("ListenSever:", err)
}
}
```
这里假设您的静态资源文件夹名为`static`,并且在同一级目录下。使用`http.Handle`和`http.StripPrefix`函数来处理静态文件路由,并指定静态文件夹的相对路径。修改后,您应该能够使用相对路径来访问静态文件。
package main import ( "fmt" "log" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, world!") } func main() { mux := http.NewServeMux() mux.HandleFunc("/", handler) go func() { err := http.ListenAndServe(":8080", mux) if err != nil { log.Fatal(err) } }() go func() { err := http.ListenAndServe(":8081", mux) if err != nil { log.Fatal(err) } }() select {} }怎么用一个http请求结束这段代码
你可以使用 context 包来实现这个目的。具体实现可以参考以下代码:
```
package main
import (
"context"
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
srv := &http.Server{Addr: ":8080", Handler: mux}
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %v", err)
}
}()
ctx := context.Background()
// 等待一个 http 请求
<-ctx.Done()
// 关闭服务器
if err := srv.Shutdown(context.Background()); err != nil {
log.Fatalf("Shutdown(): %v", err)
}
}
```
这个程序会启动一个 HTTP 服务器,监听 8080 端口。当程序收到一个 http 请求时,会关闭服务器并退出程序。
阅读全文
相关推荐
















