Go:Beego和go http服务

时间:Aug. 24, 2018 分类:

目录:

Beego

一个使用Go的思维来帮助您构建并开发Go应用程序的开源框架。

Beego官方文档

安装beego模块

go get github.com/astaxie/beego

如果需要更新可以添加-u参数

使用beego启动http服务

package main

import (
    "github.com/astaxie/beego"
)

type HomeController struct {
    beego.Controller
}

func (this *HomeController) Get() {
    this.Ctx.WriteString("Hello World")
}

func main() {
    beego.Router("/", &HomeController{})
    beego.Run()
}

创建路由,重载创建Get方法

[root@why 18:25:27 go_code]#go run beego_test1.go 
2018/08/09 18:25:32.206 [I]  http server Running on http://:8080
[root@why 18:25:51 go_code]#curl 127.0.0.1:8080/
Hello World

通过go的http服务器进行启动http服务

beego是go http服务器的高级封装

使用默认handler

package main

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 World")
}

http.ListenAndServe的第二个参数nil代表使用默认的handler,如果有err则打印

启动服务

[root@why 18:41:17 go_code]#go run web_test1.go &
[root@why 18:41:29 go_code]#curl 127.0.0.1:8080
Hello World

自己定义handler

package main

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

func main(){
    // 设置路由
    mux := http.NewServeMux()
    mux.Handle("/", &myHandler{})

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

type myHandler struct {}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "URL: "+r.URL.String()+"\n")
}

请求结果

[root@why 12:27:05 go_code]#go run web_test1.go &
[1] 1924
[root@why 12:27:08 go_code]#curl 127.0.0.1:8080/
URL: /
[root@why 12:27:13 go_code]#curl 127.0.0.1:8080/static
URL: /static
[root@why 12:27:16 go_code]#curl 127.0.0.1:8080/page
URL: /page

通过HandleFunc定义更多的路由

package main

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

func main(){
    // 设置路由
    mux := http.NewServeMux()
    mux.Handle("/", &myHandler{})
    mux.HandleFunc("/hello", sayHello)

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

type myHandler struct {}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "URL: "+r.URL.String()+"\n")
}

func sayHello(w http.ResponseWriter, r *http.Request){
    io.WriteString(w, "Hello World")
}

执行结果

[root@why 15:35:05 go_code]#go run web_test1.go &
[1] 10269
[root@why 15:35:10 go_code]#curl 127.0.0.1:8080/
URL: /
[root@why 15:35:16 go_code]#curl 127.0.0.1:8080/hello
Hello World

自定义Server

package main

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

var mux map[string]func(http.ResponseWriter, *http.Request)

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

    mux = make(map[string]func(http.ResponseWriter, *http.Request))
    mux["/hello"] = sayHello
    mux["/byebye"] = sayByebye

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

type myHandler struct {}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if b, ok := mux[r.URL.String()]; ok {
        b(w, r)
        return
    }
    io.WriteString(w, "URL: "+r.URL.String()+"\n")
}

func sayHello(w http.ResponseWriter, r *http.Request){
    io.WriteString(w, "Hello World\n")
}

func sayByebye(w http.ResponseWriter, r *http.Request){
    io.WriteString(w, "Byebye\n")
}

执行结果

[root@why 16:37:37 go_code]#go run web_test1.go & 
[1] 10709
[root@why 16:37:39 go_code]#curl 127.0.0.1:8080/hello
Hello World
[root@why 16:37:42 go_code]#curl 127.0.0.1:8080/byebye
Byebye
[root@why 16:37:51 go_code]#curl 127.0.0.1:8080/
URL: /

静态文件

package main

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


func main(){
    mux := http.NewServeMux()

    mux.Handle("/", &myHandler{})
    mux.HandleFunc("/hello", sayHello)

    wd, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }

    mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(wd))))

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

type myHandler struct {}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "URL: "+r.URL.String()+"\n")
}

func sayHello(w http.ResponseWriter, r *http.Request){
    io.WriteString(w, "Hello World\n")
}

执行结果

[root@why 17:06:16 go_code]#go run web_test1.go &
[1] 10988
[root@why 17:06:18 go_code]#curl 127.0.0.1:8080/static/test.html
test
[root@why 17:06:40 go_code]#curl 127.0.0.1:8080/
URL: /