go服務端----使用dotweb框架搭建簡易服務

使用dotweb框架搭建簡易服務

go語言web框架挺多的,所謂琳琅滿目,裏面也有不少優秀的,好比echo、beego等,但體驗下來,老是以爲哪裏有點小疙瘩,後來才明白過來,echo太簡單,不少平常使用的基礎模塊不具有,須要額外實現,而beego又是太完善,雖然可定製化,但須要熟悉的過程,因而,二話不說,準備了一款"中間態"go-web框架 - dotweb,聽說,在幾個go比較活躍的羣裏仍是很"非著名"的:)git

也但願更多的人能瞭解到dotweb,可以提出一些建議和思路,感激涕零!github

二話不說,github地址:https://github.com/devfeel/dotwebweb

 

下面貼一下用dotweb搭建一個簡易的應用服務的代碼片斷,很是的簡單,看一下代碼註釋也很容易理解。session

 1 import "github.com/devfeel/dotweb"
 2 
 3 func main() {
 4     //init DotApp
 5     app := dotweb.New()
 6     //set route
 7     app.HttpServer.Router().GET("/index", func(ctx dotweb.Context) error{
 8         return ctx.WriteString("welcome to my dot web!")
10     })
11     //begin server
12     fmt.Println(app.StartServer(80))
13 }

以上是一個很是簡單的Web服務,下面來看一個稍微複雜點的:app

func main() {
    //初始化DotServer
    app := dotweb.New()

    //設置dotserver日誌目錄
    //若是不設置,默認不啓用,且默認爲當前目錄
    app.SetEnabledLog(true)

    //開啓development模式
    app.SetDevelopmentMode()

    //設置gzip開關
    app.HttpServer.SetEnabledGzip(true)

    //設置Session開關
    app.HttpServer.SetEnabledSession(true)

    //設置Session配置
    app.HttpServer.SetSessionConfig(session.NewDefaultRuntimeConfig())

    //設置路由
    InitRoute(app.HttpServer)

    //自定義404輸出
    app.SetNotFoundHandle(func(w http.ResponseWriter, req *http.Request) {
        w.WriteHeader(http.StatusNotFound)
        w.Write([]byte("is't app's not found!"))
    })

    // 開始服務
    port := 8080
    fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
    err := app.StartServer(port)
    fmt.Println("dotweb.StartServer error => ", err)
}

func Hello(ctx dotweb.Context) error {
   ctx.WriteString("hello world!")
   return nil
}

func InitRoute(server *dotweb.HttpServer) {
   server.Router().GET("/hello", Hello)
}

 

這樣讓你們有一個基本的印象。框架

這裏只是快速上手的一些方法,做爲一個web服務框架,功能仍是會比較多的,後續陸續整理,提供給你們。spa

 

另外,歡迎各位加入咱們的go語言QQ羣:193409346日誌

相關文章
相關標籤/搜索