沉默之都

沉默,造就了我们的冷酷-程序的威力
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

go服务端----使用dotweb框架搭建简易服务

Posted on 2017-06-07 14:37  基点项目师  阅读(941)  评论(0编辑  收藏  举报

使用dotweb框架搭建简易服务

go语言web框架挺多的,所谓琳琅满目,里面也有很多优秀的,比如echo、beego等,但体验下来,总是觉得哪里有点小疙瘩,后来才明白过来,echo太简单,很多日常使用的基础模块不具备,需要额外实现,而beego又是太完善,虽然可定制化,但需要熟悉的过程,于是,二话不说,准备了一款"中间态"go-web框架 - dotweb,据说,在几个go比较活跃的群里还是很"非著名"的:)

也希望更多的人能了解到dotweb,能够提出一些建议和思路,感激不尽!

二话不说,github地址:https://github.com/devfeel/dotweb

 

下面贴一下用dotweb搭建一个简易的应用服务的代码片段,非常的简单,看一下代码注释也很容易理解。

 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服务,下面来看一个稍微复杂点的:

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服务框架,功能还是会比较多的,后续陆续整理,提供给大家。

 

另外,欢迎各位加入我们的go语言QQ群:193409346