1 Star 0 Fork 0

a526757124/database

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
a526757124 提交于 2018-05-11 16:36 . 添加dotweb readme.md文件
package main
import (
"fmt"
"net/http"
"github.com/devfeel/middleware/cors"
"github.com/devfeel/dotweb"
)
type User struct {
Name string `form:"name"`
Age int `form:"age"`
}
func main() {
//r:= http.Response
//r1:= http.ResponseWriter
app := dotweb.New()
app.HttpServer.POST("/user", func(ctx dotweb.Context) error {
user := new(User)
if err := ctx.BindJsonBody(user); err != nil {
return ctx.WriteString("Bind err:" + err.Error())
}
return ctx.WriteStringC(http.StatusOK, "Bind:"+fmt.Sprint(user))
})
userCenterGroup := app.HttpServer.Group("/usercenter")
userCenterGroup.GET("/userinfo", func(ctx dotweb.Context) error {
return nil
})
userCenterGroup.GET("/account", func(ctx dotweb.Context) error {
return nil
})
app.HttpServer.GET("/hello", func(ctx dotweb.Context) error {
return ctx.WriteString("hello " + ctx.QueryString("name"))
})
app.HttpServer.GET("/hello/:name", func(ctx dotweb.Context) error {
fmt.Println(ctx.QueryString("name"))
return ctx.WriteString("hello " + ctx.GetRouterName("name"))
})
app.HttpServer.GET("/news/:category/:newsid", func(ctx dotweb.Context) error {
category := ctx.GetRouterName("category")
newsid := ctx.GetRouterName("newsid")
return ctx.WriteString("news info: category=" + category + " newsid=" + newsid)
})
app.HttpServer.POST("/upload", func(ctx dotweb.Context) error {
ctx.File("upload")
return nil
})
app.HttpServer.Renderer().SetTemplatePath("views/")
app.HttpServer.GET("/", func(ctx dotweb.Context) error {
ctx.ViewData().Set("data", "图书信息")
type BookInfo struct {
Name string
Size int64
}
m := make([]*BookInfo, 5)
m[0] = &BookInfo{Name: "book0", Size: 1}
m[1] = &BookInfo{Name: "book1", Size: 10}
m[2] = &BookInfo{Name: "book2", Size: 100}
m[3] = &BookInfo{Name: "book3", Size: 1000}
m[4] = &BookInfo{Name: "book4", Size: 10000}
ctx.ViewData().Set("Books", m)
err := ctx.View("test/testview.html")
return err
})
app.HttpServer.GET("/redirect", func(ctx dotweb.Context) error {
//内部重定向
ctx.Redirect(http.StatusMovedPermanently, "src/1.html")
//外部的重定向
ctx.Redirect(http.StatusMovedPermanently, "https://www.baidu.com")
return nil
})
app.Use(cors.DefaultMiddleware())
app.HttpServer.ServerFile("/src/*filepath", "var/www")
//app.HttpServer()..ServerFile("/*filepath", "/devfeel/dotweb/public")
app.StartServer(8888)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/a526757124/database.git
[email protected]:a526757124/database.git
a526757124
database
database
master

搜索帮助