beego3---gohttp底层实现

简介:
复制代码
package main

//gohttp底层实现,通过gohttp不是通过beego实现的输出
//

import (
    "io"
    "log"
    "net/http"
)

func main() {
    //设置路由
    http.HandleFunc("/", sayHello)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal(err)
    }
}

func sayHello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello vwesion1")
}
复制代码

 

复制代码
package main

//gohttp第二个版本:通过mux,handler实现路由

import (
    "io"
    "log"
    "net/http"
)

func main() {
    mux := http.NewServeMux()      //ServeMux//实现handler注册到ServeMux然后在进行路由的注册
    mux.Handle("/", &myHandler{})  //注册路由和handler
    mux.Handle("/hello", sayHello) //通过函数注册路由

    err := http.ListenAndServe(":8080", mux)
    if err != nil {
        log.Fatal(err)
    }
}

type myHandler struct { //这个handle要实现ServerHTTP方法

}

func (this *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello vwesion2,"+r.URL.String())
}

func sayHello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello vwesion2,"+r.URL.String())
}
复制代码

 

复制代码
package main

//模拟gohttp底层,第三个版本

import (
    "fmt"
    "io"
    "log"
    "net/http"
    "os" //获取静态文件
    "time"
)

func main() {
    server := http.Server{
        Addr:        ":8080",
        Handler:     &myHandler{}, //自定义handler
        ReadTimeout: 5 * time.Second,
    }

    myMux = make(map[string]func(http.ResponseWriter, *http.Request)) //初始化map
    myMux["/"] = sayHello                                             //访问根目录调用sayHellp方法
    myMux["/bye"] = sayBye                                            //访问/bye调用sayBye方法

    err := server.ListenAndServe()
    if err != nil {
        log.Fatal(err)
    }
}

type myHandler struct{}

func (this *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { //handler来解析url,调用不同的方法,
    if h, ok := myMux[r.URL.String()]; ok { //if ok 对map的断言,ok是判断存不存在,h是获取的map的value,
        fmt.Println(h, ok)
        h(w, r)
        return
    }
    io.WriteString(w, "uuuu,"+r.URL.String())
}

var myMux map[string]func(http.ResponseWriter, *http.Request) //自定义mux,根据不同的string调用不同的方法

func sayHello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello "+r.URL.String())
}

func sayBye(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "sayBye "+r.URL.String())
}
复制代码

 

复制代码
package main

//获取静态文件

import (
    "io"
    "log"
    "net/http"
    "os" //获取路径,静态文件服务器要获取绝对路径,根据当前路径定位到绝对路径,
)

func main() {
    mux := http.NewServeMux()      //ServeMux//实现handler注册到ServeMux然后在进行路由的注册
    mux.Handle("/", &myHandler{})  //注册路由和handler
    mux.Handle("/hello", sayHello) //通过函数注册路由

    wd, err := os.Getwd() //wd是当前路径
    if err != nil {
        log.Fatal(err)
    }

    mux.Handle("/static/",
        http.StripPrefix("/static/", http.FileServer(http.Dir(wd)))) //StripPrefix去除static前缀

    err = http.ListenAndServe(":8080", mux)
    if err != nil {
        log.Fatal(err)
    }
}

type myHandler struct { //这个handle要实现ServerHTTP方法

}

func (this *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello vwesion2,"+r.URL.String())
}

func sayHello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello vwesion2,"+r.URL.String())
}
复制代码


相关文章
|
3月前
|
中间件 Go 开发者
Go net http包
Go net http包
48 0
|
10月前
|
Go
Go 使用标准库 net/http 包构建服务器
Go 使用标准库 net/http 包构建服务器
36 0
|
11天前
|
程序员 Go 网络架构
不看就落后了,Go 1.22 中更好的http router
不看就落后了,Go 1.22 中更好的http router
|
11天前
|
JSON 测试技术 Go
Go Kit中读取原始HTTP请求体的方法
Go Kit中读取原始HTTP请求体的方法
|
11天前
|
网络协议 Go
go的net/http有哪些值得关注的细节?
go的net/http有哪些值得关注的细节?
|
12天前
|
网络协议 Go
【go笔记】简单的http服务
【go笔记】简单的http服务
|
21天前
|
JSON Java Serverless
函数计算产品使用问题之如何使用Go SDK从HTTP上下文中提取JSON数据
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
21天前
|
Go
Go中 net/http 使用
Go中 net/http 使用
12 0
|
1月前
|
网络协议 程序员 应用服务中间件
Swoole与Go系列教程之HTTP服务的应用
PHP 曾是Web开发领域佼佼者,随着业务壮大,异步和高并发方面不足显现。Swoole 曾经尝试填补空白,但局限性也比较的明显。Go 语言的崛起,简洁语法和并发优势吸引大厂使用,吸引了大多数程序员的转型。
987 0
Swoole与Go系列教程之HTTP服务的应用
|
3月前
|
中间件 Go API
Golang深入浅出之-Go语言标准库net/http:构建Web服务器
【4月更文挑战第25天】Go语言的`net/http`包是构建高性能Web服务器的核心,提供创建服务器和发起请求的功能。本文讨论了使用中的常见问题和解决方案,包括:使用第三方路由库改进路由设计、引入中间件处理通用逻辑、设置合适的超时和连接管理以防止资源泄露。通过基础服务器和中间件的代码示例,展示了如何有效运用`net/http`包。掌握这些最佳实践,有助于开发出高效、易维护的Web服务。
65 1