惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
B
Blog
博客园_首页
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
M
MIT News - Artificial intelligence
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
量子位
V
V2EX
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
I
InfoQ
博客园 - 【当耐特】

博客园 - 基点项目师

dotweb now released to Version 1.5 dotweb框架之旅 [四] - 常用对象-HttpContext dotweb框架之旅 [三] - 常用对象-HttpServer dotweb框架之旅 [二] - 常用对象-App(dotweb) 对 dotweb 框架进行统一的自定义错误处理 go服务端----使用dotweb框架搭建简易服务 对Golang有兴趣的朋友,推荐一款go语言Web框架-dotweb Nginx日志文件切割 linux安装PHP-memcache-redis扩展 微信公众平台开发 - 页面自适应 linux 安装 mongodb linux 安装 node.js nginx支持php配置 Linux 安装 php linux 安装nginx 如何设计自适应网页 ThinkDev.Data更新日志 ThinkDev.Logging-Queue模块介绍 Windows准备Node.js运行与开发环境
dotweb框架之旅 [一] - HelloWorld
基点项目师 · 2017-10-14 · via 博客园 - 基点项目师

一直想着,要系统性的写一些dotweb使用的文章,之前拖延了不少时间,今天,下定决定,算是正式的开始,也请大家一起监督。

dotweb,是一款追求简约大方的go web框架,正如其github项目主页的自我介绍一样:Simple and easy go web micro framework”,我相信能够坚持贯彻这一点,给大家提供一个用的舒服用的安心的框架:)

框架地址:https://github.com/devfeel/dotweb

目录:

1、dotweb框架之旅 [一] - HelloWorld

2、dotweb框架之旅 [二] - 常用对象-App(dotweb)

3、dotweb框架之旅 [三] - 常用对象-HttpServer

这一章是开篇,先从著名的“HelloWorld”开始:

1、极客版

package main

import (
    "fmt"
    "github.com/devfeel/dotweb"
)

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

    //注册hello路由
    app.HttpServer.GET("/hello", func (ctx dotweb.Context) error {
        ctx.WriteString("hello world!")
        return nil
    })

    //开始服务
    port := 8080
    err := app.StartServer(port)
    fmt.Println("dotweb.StartServer error => ", err)
}

2、工程版

package main

import (
    "fmt"
    "github.com/devfeel/dotweb"
)

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

    //开启debug模式
    app.SetDevelopmentMode()

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

    //开始服务
    port := 8080
    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)
}

以上两段代码都是实现一样的功能,通过访问http://127.0.0.1:8080/hello 输出“hello world!”字符串。

极客版:一般仅为演示项目何其简介的时候才会这么写,做非常少量的路由时可以这么做,但一般工程项目不建议这么做,会加大维护的难度

工程版:正常项目,请务必剥离路由注册和HttpHandle的实现

项目版:目前为了尽量减少大家在使用dotweb时候的各种纠结,已经启动start项目,可以参考真实项目的一些目录指引 - https://github.com/devfeel/dotweb-start

启动日志:

访问http://127.0.0.1:8080/hello请求情况:

至此,成功达成目标。

如HelloWorld代码,整个Web启动过程分为几步:

1、初始化App容器

2、设置工作模式(development\production

3、注册路由模快

4、设置端口

5、启动服务

以上五个步骤,其中第二步不是必须,默认为development模式。

希望本文能给大家带来一些帮助。

本文代码地址:https://github.com/devfeel/dotweb-example/blob/master/helloworld/main.go

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