Fetch the repository succeeded.
package main
import (
"fmt"
v1http "gitee.com/scottq/go-framework/src/v1/httpserver"
v1log "gitee.com/scottq/go-framework/src/v1/log"
"net/http"
"os"
"path/filepath"
)
func main() {
name := "example"
logPath := fmt.Sprintf("./runtime/logs/%s.log", filepath.Base(os.Args[0]))
logger := v1log.NewZapLog(name, logPath, nil)
httpServer, err := v1http.NewHttpServer("", ":8080")
if err != nil {
logger.Error("run http server error:%s" + err.Error())
return
}
//添加logger
httpServer.AddLogger(logger)
api := &ApiHandler{}
//注册简单的路由
httpServer.RouteGet("/v1", api.Index, nil)
//使用json返回
httpServer.RouteGet("/v1/users", api.Users, nil)
httpServer.RoutePost("/v1/exception", api.Exception, nil)
httpServer.RouteOptions("/v1/exception", api.Exception, nil)
httpServer.RouteDelete("/v1/exception", api.Exception, nil)
httpServer.RoutePut("/v1/exception", api.Exception, nil)
httpServer.RouteFiles("/static", http.Dir("public"))
logger.Fatal(httpServer.Run().Error())
}
type ApiHandler struct {
}
//http router
func (h *ApiHandler) Index(ctx *v1http.Ctx) {
ctx.Write([]byte("Hello World"))
}
func (h *ApiHandler) Users(ctx *v1http.Ctx) {
users := []map[string]interface{}{
{"uid": 1, "username": "jack"},
{"uid": 2, "username": "lucy"},
}
ctx.SetData(users)
ctx.SetMsg("success")
ctx.Response()
}
func (h *ApiHandler) Exception(ctx *v1http.Ctx) {
ctx.WriteHeader(500)
ctx.Response()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。