golang中的http服务器

简介

http WEB服务器:

	1.  注册回调函数:http.HandleFunc("/", myHandler)

		func myHandler(w http.ResponseWriter, r *http.Request)

			w:给客户端回发数据

			r:从客户端读到的数据

		type ResponseWriter interface {
   			Header() Header			
   			Write([]byte) (int, error)	
   			WriteHeader(int)			
		}

		type Request struct {
			Method string		// 浏览器请求方法 GET、POST⋯
			URL *url.URL		// 浏览器请求的访问路径
			⋯⋯
			Header Header		// 请求头部
			Body io.ReadCloser	// 请求包体
			RemoteAddr string	// 浏览器地址
			⋯⋯
    			ctx context.Context
		}

	2. 绑定服务器地址结构:http.ListenAndServe("127.0.0.1:8000", nil)

		参2:通常传 ni 。 表示  让服务端 调用默认的 http.DefaultServeMux 进行处理

使用

func myHandler(w http.ResponseWriter, r *http.Request) {
   fmt.Println("r.Method = ", r.Method)
   fmt.Println("r.URL = ", r.URL)
   fmt.Println("r.Header = ", r.Header)
   fmt.Println("r.Body = ", r.Body)

   //回复
   io.WriteString(w, "hello world\n")
}

func main() {
   //第一个参数是url的
   http.HandleFunc("/", myHandler)

   //用于指定的tcp网络地址监听
   //第一个参数是监听地址,第二个参数是服务端处理程序,通常为空,为空表示服务端调用http.DefaultServeMux处理
   err := http.ListenAndServe("127.0.0.1:8183", nil)
   if err != nil {
      fmt.Println("有错误: ", err)
   }
}

默认 Handler

  • NotFountHandler
http.ListenAndServe(":8081", http.NotFoundHandler())
  • RedirectHandler
http.ListenAndServe(":8081", http.RedirectHandler("/test", 302))
  • FileServer
http.ListenAndServe(":8081", http.FileServer(http.Dir("./")))

get

func myHandler(w http.ResponseWriter, r *http.Request) {
   //解析参数,默认是不会解析的
   r.ParseForm()
   fmt.Fprintf(w, "%v\n", r.Form)
   fmt.Fprintf(w, "path:%s\n", r.URL.Path)
   fmt.Fprintf(w, "schema:%s\n", r.URL.Scheme)
   //get查询字符串
   fmt.Fprintf(w, "form:%s\n", r.Form)
   //控制台打印
   for k, v := range r.Form {
      fmt.Println("key: ", k)
      fmt.Println("value: ", strings.Join(v, ""))
   }

   fmt.Fprintf(w, "hello world\n")
}

func main() {
   //第一个参数是url的
   http.HandleFunc("/", myHandler)

   //用于指定的tcp网络地址监听
   //第一个参数是监听地址,第二个参数是服务端处理程序,通常为空,为空表示服务端调用http.DefaultServeMux处理
   err := http.ListenAndServe("127.0.0.1:8183", nil)
   if err != nil {
      fmt.Println("有错误: ", err)
   }
}

post

func myHandler(w http.ResponseWriter, r *http.Request) {
   method := r.Method
   if method == "GET" {
      t, err := template.ParseFiles("./index.html")
      if err != nil {
         fmt.Fprintf(w, "载入页面错误")
         return
      }
      t.Execute(w, nil)
   } else if method == "POST" {
      r.ParseForm()
      fmt.Printf("username: %s\n", r.FormValue("username"))
      fmt.Printf("password: %s\n", r.FormValue("password"))
      fmt.Fprintf(w, "username: %s, password: %s\n", r.FormValue("username"), r.FormValue("password"))
   }
}

func main() {
   //第一个参数是url的
   http.HandleFunc("/", myHandler)

   //用于指定的tcp网络地址监听
   //第一个参数是监听地址,第二个参数是服务端处理程序,通常为空,为空表示服务端调用http.DefaultServeMux处理
   err := http.ListenAndServe("127.0.0.1:8183", nil)
   if err != nil {
      fmt.Println("有错误: ", err)
   }
}

模板

"html/template"
func myHandler(w http.ResponseWriter, r *http.Request) {
   t, err := template.ParseFiles("./index.html")
   if err != nil {
      fmt.Fprintf(w, "载入页面错误")
      return
   }
   //第二个参数就是模板那的
   t.Execute(w, "mary")
}

func main() {
   //第一个参数是url的
   http.HandleFunc("/", myHandler)

   //用于指定的tcp网络地址监听
   //第一个参数是监听地址,第二个参数是服务端处理程序,通常为空,为空表示服务端调用http.DefaultServeMux处理
   err := http.ListenAndServe("127.0.0.1:8183", nil)
   if err != nil {
      fmt.Println("有错误: ", err)
   }
}

页面上

<h1>{
   
   {.}}</h1>

模板中的结构体

type UserInfo struct {
   Name string
   Sex  string
   Age  int
}

func myHandler(w http.ResponseWriter, r *http.Request) {
   t, err := template.ParseFiles("./index.html")
   if err != nil {
      fmt.Fprintf(w, "载入页面错误")
      return
   }
   user := UserInfo{
      Name: "Mary",
      Sex:  "男",
      Age:  18,
   }
   //第二个参数就是模板那的
   t.Execute(w, user)
}

html

<h1>{
   
   {.}}</h1>
<h1>{
   
   {.Name}}</h1>
<h1>{
   
   {.Sex}}</h1>
<h1>{
   
   {.Age}}</h1>

模板中的map

t, err := template.ParseFiles("./index.html")
if err != nil {
   fmt.Fprintf(w, "载入页面错误")
   return
}
m := make(map[string]interface{})
m["name"] = "mary"
m["sex"] = "男"
m["age"] = 18
//第二个参数就是模板那的
t.Execute(w, m)

html

<h1>{
   
   {.}}</h1>
<h1>{
   
   {.name}}</h1>
<h1>{
   
   {.sex}}</h1>
<h1>{
   
   {.age}}</h1>

判断

{
   
   {if gt .age 18}}
    <p>hello, old man, {
   
   {.name}}</p>
{
   
   {else}}
    <p>hello, young man, {
   
   {.name}}</p>
{
   
   {end}}

猜你喜欢

转载自blog.csdn.net/ma2595162349/article/details/112911939