Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func StartServer() error {
app.SetLogPath("/home/logs/wwwroot/")
//set route
app.HttpServer.GET("/index", func(ctx dotweb.Context) error{
_, err := ctx.WriteString("welcome to my first web!")
return err
return ctx.WriteString("welcome to my first web!")
})
//begin server
err := app.StartServer(80)
Expand Down Expand Up @@ -115,8 +114,7 @@ import (
func main() {
dotapp := dotweb.New()
dotapp.HttpServer.GET("/hello", func(ctx dotweb.Context) error{
ctx.WriteString("hello world!")
return nil
return ctx.WriteString("hello world!")
})
dotapp.StartServer(80)
}
Expand All @@ -135,14 +133,12 @@ import (
func main() {
dotapp := dotweb.New()
dotapp.HttpServer.GET("/hello/:name", func(ctx dotweb.Context) error{
_, err := ctx.WriteString("hello " + ctx.GetRouterName("name"))
return err
return ctx.WriteString("hello " + ctx.GetRouterName("name"))
})
   dotapp.HttpServer.GET("/news/:category/:newsid", func(ctx dotweb.Context) error{
category := ctx.GetRouterName("category")
newsid := ctx.GetRouterName("newsid")
       _, err := ctx.WriteString("news info: category=" + category + " newsid=" + newsid)
return err
       return ctx.WriteString("news info: category=" + category + " newsid=" + newsid)
})
dotapp.StartServer(80)
}
Expand Down Expand Up @@ -173,11 +169,10 @@ type UserInfo struct {
func TestBind(ctx dotweb.HttpContext) error{
user := new(UserInfo)
if err := ctx.Bind(user); err != nil {
ctx.WriteString("err => " + err.Error())
return ctx.WriteString("err => " + err.Error())
}else{
ctx.WriteString("TestBind " + fmt.Sprint(user))
return ctx.WriteString("TestBind " + fmt.Sprint(user))
}
return nil
}

```
Expand Down
107 changes: 71 additions & 36 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"path/filepath"
"time"
"strconv"
)

const (
Expand Down Expand Up @@ -52,6 +53,8 @@ type (
IsEnd() bool
Redirect(code int, targetUrl string) error
QueryString(key string) string
QueryInt(key string) int
QueryInt64(key string) int64
FormValue(key string) string
PostFormValue(key string) string
File(file string) (err error)
Expand All @@ -70,18 +73,18 @@ type (
View(name string) error
ViewC(code int, name string) error
Write(code int, content []byte) (int, error)
WriteString(contents ...interface{}) (int, error)
WriteStringC(code int, contents ...interface{}) (int, error)
WriteHtml(contents ...interface{}) (int, error)
WriteHtmlC(code int, contents ...interface{}) (int, error)
WriteBlob(contentType string, b []byte) (int, error)
WriteBlobC(code int, contentType string, b []byte) (int, error)
WriteJson(i interface{}) (int, error)
WriteJsonC(code int, i interface{}) (int, error)
WriteJsonBlob(b []byte) (int, error)
WriteJsonBlobC(code int, b []byte) (int, error)
WriteJsonp(callback string, i interface{}) (int, error)
WriteJsonpBlob(callback string, b []byte) (size int, err error)
WriteString(contents ...interface{}) error
WriteStringC(code int, contents ...interface{}) error
WriteHtml(contents ...interface{}) error
WriteHtmlC(code int, contents ...interface{}) error
WriteBlob(contentType string, b []byte) error
WriteBlobC(code int, contentType string, b []byte) error
WriteJson(i interface{}) error
WriteJsonC(code int, i interface{}) error
WriteJsonBlob(b []byte) error
WriteJsonBlobC(code int, b []byte) error
WriteJsonp(callback string, i interface{}) error
WriteJsonpBlob(callback string, b []byte) error
}

HttpContext struct {
Expand Down Expand Up @@ -320,6 +323,35 @@ func (ctx *HttpContext) QueryString(key string) string {
return ctx.request.QueryString(key)
}

// QueryInt get query key with int format
// if not exists or not int type, return 0
func (ctx *HttpContext) QueryInt(key string) int {
param := ctx.request.QueryString(key)
if param == "" {
return 0
}
val, err:=strconv.Atoi(param)
if err != nil{
return 0
}
return val
}

// QueryInt64 get query key with int64 format
// if not exists or not int64 type, return 0
func (ctx *HttpContext) QueryInt64(key string) int64 {
param := ctx.request.QueryString(key)
if param == "" {
return 0
}
val, err:=strconv.ParseInt(param, 10, 64)
if err != nil{
return 0
}
return val
}


/*
* 根据指定key获取包括在post、put和get内的值
*/
Expand Down Expand Up @@ -484,94 +516,97 @@ func (ctx *HttpContext) Write(code int, content []byte) (int, error) {
}

// WriteString write (200, string, text/plain) to response
func (ctx *HttpContext) WriteString(contents ...interface{}) (int, error) {
func (ctx *HttpContext) WriteString(contents ...interface{}) error {
return ctx.WriteStringC(defaultHttpCode, contents...)
}

// WriteStringC write (httpCode, string, text/plain) to response
func (ctx *HttpContext) WriteStringC(code int, contents ...interface{}) (int, error) {
func (ctx *HttpContext) WriteStringC(code int, contents ...interface{}) error {
content := fmt.Sprint(contents...)
return ctx.WriteBlobC(code, MIMETextPlainCharsetUTF8, []byte(content))
}

// WriteString write (200, string, text/html) to response
func (ctx *HttpContext) WriteHtml(contents ...interface{}) (int, error) {
func (ctx *HttpContext) WriteHtml(contents ...interface{}) error {
return ctx.WriteHtmlC(defaultHttpCode, contents...)
}

// WriteHtmlC write (httpCode, string, text/html) to response
func (ctx *HttpContext) WriteHtmlC(code int, contents ...interface{}) (int, error) {
func (ctx *HttpContext) WriteHtmlC(code int, contents ...interface{}) error {
content := fmt.Sprint(contents...)
return ctx.WriteBlobC(code, MIMETextHTMLCharsetUTF8, []byte(content))
}

// WriteBlob write []byte content to response
func (ctx *HttpContext) WriteBlob(contentType string, b []byte) (int, error) {
func (ctx *HttpContext) WriteBlob(contentType string, b []byte) error {
return ctx.WriteBlobC(defaultHttpCode, contentType, b)
}

// WriteBlobC write (httpCode, []byte) to response
func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) (int, error) {
func (ctx *HttpContext) WriteBlobC(code int, contentType string, b []byte) error {
if contentType != "" {
ctx.response.SetContentType(contentType)
}
if ctx.IsHijack() {
return ctx.hijackConn.WriteBlob(b)
_, err := ctx.hijackConn.WriteBlob(b)
return err
} else {
return ctx.response.Write(code, b)
_, err := ctx.response.Write(code, b)
return err
}
}

// WriteJson write (httpCode, json string) to response
// auto convert interface{} to json string
func (ctx *HttpContext) WriteJson(i interface{}) (int, error) {
func (ctx *HttpContext) WriteJson(i interface{}) error {
return ctx.WriteJsonC(defaultHttpCode, i)
}

// WriteJsonC write (httpCode, json string) to response
// auto convert interface{} to json string
func (ctx *HttpContext) WriteJsonC(code int, i interface{}) (int, error) {
func (ctx *HttpContext) WriteJsonC(code int, i interface{}) error{
b, err := json.Marshal(i)
if err != nil {
return 0, err
return err
}
return ctx.WriteJsonBlobC(code, b)
}

// WriteJsonBlob write json []byte to response
func (ctx *HttpContext) WriteJsonBlob(b []byte) (int, error) {
func (ctx *HttpContext) WriteJsonBlob(b []byte) error {
return ctx.WriteJsonBlobC(defaultHttpCode, b)
}

// WriteJsonBlobC write (httpCode, json []byte) to response
func (ctx *HttpContext) WriteJsonBlobC(code int, b []byte) (int, error) {
func (ctx *HttpContext) WriteJsonBlobC(code int, b []byte) error {
return ctx.WriteBlobC(code, MIMEApplicationJSONCharsetUTF8, b)
}

// WriteJsonp write jsonp string to response
func (ctx *HttpContext) WriteJsonp(callback string, i interface{}) (int, error) {
func (ctx *HttpContext) WriteJsonp(callback string, i interface{}) error {
b, err := json.Marshal(i)
if err != nil {
return 0, err
return err
}
return ctx.WriteJsonpBlob(callback, b)
}

// WriteJsonpBlob write jsonp string as []byte to response
func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) (size int, err error) {
func (ctx *HttpContext) WriteJsonpBlob(callback string, b []byte) error {
var err error
ctx.response.SetContentType(MIMEApplicationJavaScriptCharsetUTF8)
//特殊处理,如果为hijack,需要先行WriteBlob头部
if ctx.IsHijack() {
if size, err = ctx.hijackConn.WriteBlob([]byte(ctx.hijackConn.header + "\r\n")); err != nil {
return
if _, err = ctx.hijackConn.WriteBlob([]byte(ctx.hijackConn.header + "\r\n")); err != nil {
return err
}
}
if size, err = ctx.WriteBlob("", []byte(callback+"(")); err != nil {
return
if err = ctx.WriteBlob("", []byte(callback+"(")); err != nil {
return err
}
if size, err = ctx.WriteBlob("", b); err != nil {
return
if err = ctx.WriteBlob("", b); err != nil {
return err
}
size, err = ctx.WriteBlob("", []byte(");"))
return
err = ctx.WriteBlob("", []byte(");"))
return err
}
6 changes: 2 additions & 4 deletions example/appcontext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ func Index(ctx dotweb.Context) error {
gint := ctx.AppContext().GetInt("gint")
ctx.AppContext().Set("index", "index-v")
ctx.AppContext().Set("user", "user-v")
_, err := ctx.WriteString("index -> " + gstring + ";" + strconv.Itoa(gint))
return err
return ctx.WriteString("index -> " + gstring + ";" + strconv.Itoa(gint))
}

//you can curl http://127.0.0.1:8080/2
func Index2(ctx dotweb.Context) error {
gindex := ctx.AppContext().GetString("index")
ctx.AppContext().Remove("index")
user, _ := ctx.AppContext().Once("user")
_, err := ctx.WriteString("index -> " + gindex + ";" + fmt.Sprint(user))
return err
return ctx.WriteString("index -> " + gindex + ";" + fmt.Sprint(user))
}

func InitRoute(server *dotweb.HttpServer) {
Expand Down
9 changes: 3 additions & 6 deletions example/bind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func TestBind(ctx dotweb.Context) error {

}

_, err := ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user))
return err
return ctx.WriteString("TestBind [" + errstr + "] " + fmt.Sprint(user))
}

func GetBind(ctx dotweb.Context) error {
Expand All @@ -69,8 +68,7 @@ func GetBind(ctx dotweb.Context) error {

}

_, err := ctx.WriteString("GetBind [" + errstr + "] " + fmt.Sprint(user))
return err
return ctx.WriteString("GetBind [" + errstr + "] " + fmt.Sprint(user))
}

func PostJsonBind(ctx dotweb.Context) error{
Expand All @@ -86,8 +84,7 @@ func PostJsonBind(ctx dotweb.Context) error{

}

_, err := ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user))
return err
return ctx.WriteString("PostBind [" + errstr + "] " + fmt.Sprint(user))
}

func InitRoute(server *dotweb.HttpServer) {
Expand Down
6 changes: 2 additions & 4 deletions example/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func One(ctx dotweb.Context) error {
g = err.Error()
}
_, err = ctx.Cache().Incr("count")
_, err = ctx.WriteString("One [" + g + "] " + fmt.Sprint(err))
return err
return ctx.WriteString("One [" + g + "] " + fmt.Sprint(err))
}

func Two(ctx dotweb.Context) error {
Expand All @@ -61,8 +60,7 @@ func Two(ctx dotweb.Context) error {
}
_, err = ctx.Cache().Incr("count")
c, _ := ctx.Cache().GetString("count")
_, err = ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err))
return err
return ctx.WriteString("Two [" + g + "] [" + c + "] " + fmt.Sprint(err))
}

func InitRoute(server *dotweb.HttpServer) {
Expand Down
12 changes: 4 additions & 8 deletions example/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ func main() {

func Index(ctx dotweb.Context) error {
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
_, err := ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
return err
return ctx.WriteString("index => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
}

func GetAppSet(ctx dotweb.Context) error {
key := ctx.QueryString("key")
_, err := ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key))
return err
return ctx.WriteString(ctx.Request().Url(), " => key = ", ctx.AppSetConfig().GetString(key))
}

func DefaultPanic(ctx dotweb.Context) error {
Expand All @@ -65,13 +63,11 @@ func Redirect(ctx dotweb.Context) error {
}

func Login(ctx dotweb.Context) error {
_, err := ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
return err
return ctx.WriteString("login => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
}

func Logout(ctx dotweb.Context) error {
_, err := ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
return err
return ctx.WriteString("logout => ", fmt.Sprint(ctx.RouterNode().Middlewares()))
}

func RegisterHandler(server *dotweb.HttpServer) {
Expand Down
3 changes: 1 addition & 2 deletions example/httpmodule/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func main() {
func Index(ctx dotweb.Context) error {
ctx.Items().Set("count", 2)
ctx.WriteString(ctx.Request().Path() + ":Items.Count=> " + ctx.Items().GetString("count"))
_, err := ctx.WriteString("\r\n")
return err
return ctx.WriteString("\r\n")
}

func WHtml(ctx dotweb.Context) error {
Expand Down
Loading