Skip to content

Commit

Permalink
Merge pull request #25 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
yoyofx authored Dec 12, 2019
2 parents 3e9de94 + 0dd4a1c commit d3b6bff
Show file tree
Hide file tree
Showing 37 changed files with 393 additions and 373 deletions.
8 changes: 4 additions & 4 deletions ResponseRender/Render.go → ActionResult/IActionResult.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ResponseRender
package ActionResult

import "net/http"

// ResponseRender interface is to be implemented by JSON, XML, HTML, YAML and so on.
type ResponseRender interface {
// ResponseRender writes data with custom ContentType.
// ActionResult interface is to be implemented by JSON, XML, HTML, YAML and so on.
type IActionResult interface {
// ActionResult writes data with custom ContentType.
Render(http.ResponseWriter) error
// WriteContentType writes custom ContentType.
WriteContentType(w http.ResponseWriter)
Expand Down
2 changes: 1 addition & 1 deletion ResponseRender/data.go → ActionResult/data.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"io/ioutil"
Expand Down
8 changes: 4 additions & 4 deletions ResponseRender/html.go → ActionResult/html.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"html/template"
Expand All @@ -16,7 +16,7 @@ type Delims struct {
// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.
type HTMLRender interface {
// Instance returns an HTML instance.
Instance(string, interface{}) ResponseRender
Instance(string, interface{}) IActionResult
}

// HTMLProduction contains template reference and its delims.
Expand All @@ -43,7 +43,7 @@ type HTML struct {
var htmlContentType = []string{"text/html; charset=utf-8"}

// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
func (r HTMLProduction) Instance(name string, data interface{}) ResponseRender {
func (r HTMLProduction) Instance(name string, data interface{}) IActionResult {
return HTML{
Template: r.Template,
Name: name,
Expand All @@ -52,7 +52,7 @@ func (r HTMLProduction) Instance(name string, data interface{}) ResponseRender {
}

// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
func (r HTMLDebug) Instance(name string, data interface{}) ResponseRender {
func (r HTMLDebug) Instance(name string, data interface{}) IActionResult {
return HTML{
Template: r.loadTemplate(),
Name: name,
Expand Down
14 changes: 7 additions & 7 deletions ResponseRender/json.go → ActionResult/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"bytes"
Expand Down Expand Up @@ -47,7 +47,7 @@ func writeJSON(w http.ResponseWriter, obj interface{}) error {
return err
}

// ResponseRender (JSON) writes data with custom ContentType.
// ActionResult (JSON) writes data with custom ContentType.
func (d Json) Render(w http.ResponseWriter) (err error) {
if err = writeJSON(w, d.Data); err != nil {
panic(err)
Expand All @@ -59,7 +59,7 @@ func (d Json) WriteContentType(w http.ResponseWriter) {
writeContentType(w, jsonContentType)
}

// ResponseRender (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
// ActionResult (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
func (r IndentedJson) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
Expand All @@ -76,7 +76,7 @@ func (r IndentedJson) WriteContentType(w http.ResponseWriter) {

// SecureJSON 用来防止json劫持。
// 如果给定的结构体是数值型,默认预置“while(1)” 到response body
// ResponseRender (SecureJSON) marshals the given interface object and writes it with custom ContentType.
// ActionResult (SecureJSON) marshals the given interface object and writes it with custom ContentType.
func (r SecureJson) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
jsonBytes, err := json.Marshal(r.Data)
Expand All @@ -99,7 +99,7 @@ func (r SecureJson) WriteContentType(w http.ResponseWriter) {
}

// callback(json)
// ResponseRender (Jsonp JSON) marshals the given interface object and writes it and its callback with custom ContentType.
// ActionResult (Jsonp JSON) marshals the given interface object and writes it and its callback with custom ContentType.
func (r Jsonp) Render(w http.ResponseWriter) (err error) {
r.WriteContentType(w)
ret, err := json.Marshal(r.Data)
Expand Down Expand Up @@ -138,7 +138,7 @@ func (r Jsonp) WriteContentType(w http.ResponseWriter) {
}

// 使用Ascii JSON 生成仅有 ASCII字符的JSON,非ASCII字符将会被转义
// ResponseRender (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
// ActionResult (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
func (r AsciiJson) Render(w http.ResponseWriter) (err error) {
r.WriteContentType(w)
ret, err := json.Marshal(r.Data)
Expand All @@ -164,7 +164,7 @@ func (r AsciiJson) WriteContentType(w http.ResponseWriter) {
}

// 原始字符串,不通过Html编码
// ResponseRender (PureJSON) writes custom ContentType and encodes the given interface object.
// ActionResult (PureJSON) writes custom ContentType and encodes the given interface object.
func (r PureJson) Render(w http.ResponseWriter) error {
r.WriteContentType(w)
encoder := json.NewEncoder(w)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"github.com/ugorji/go/codec"
Expand Down
2 changes: 1 addition & 1 deletion ResponseRender/protobuf.go → ActionResult/protobuf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"github.com/golang/protobuf/proto"
Expand Down
2 changes: 1 addition & 1 deletion ResponseRender/Redirect.go → ActionResult/redirect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion ResponseRender/text.go → ActionResult/text.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion ResponseRender/xml.go → ActionResult/xml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"encoding/xml"
Expand Down
2 changes: 1 addition & 1 deletion ResponseRender/yaml.go → ActionResult/yaml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ResponseRender
package ActionResult

import (
"gopkg.in/yaml.v2"
Expand Down
8 changes: 4 additions & 4 deletions Context/HttpContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/maxzhang1985/yoyogo/ActionResult"
"github.com/maxzhang1985/yoyogo/DependencyInjection"
"github.com/maxzhang1985/yoyogo/ResponseRender"
"github.com/maxzhang1985/yoyogo/Standard"
"github.com/maxzhang1985/yoyogo/Utils"
"io"
Expand Down Expand Up @@ -136,7 +136,7 @@ func (ctx *HttpContext) PostJsonForm() url.Values {
}

func (ctx *HttpContext) GetAllParam() url.Values {
var form url.Values
form := url.Values{}
content_type := ctx.Req.Header.Get("Content-Type")

if strings.HasPrefix(content_type, Std.MIMEApplicationForm) {
Expand Down Expand Up @@ -383,8 +383,8 @@ func bodyAllowedForStatus(status int) bool {
return true
}

// ResponseRender writes the response headers and calls render.ResponseRender to render data.
func (c *HttpContext) Render(code int, r ResponseRender.ResponseRender) {
// ActionResult writes the response headers and calls render.ActionResult to render data.
func (c *HttpContext) Render(code int, r ActionResult.IActionResult) {
c.Status(code)

if !bodyAllowedForStatus(code) {
Expand Down
30 changes: 15 additions & 15 deletions Context/httpcontext_renderfunc.go
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
package Context

import (
"github.com/maxzhang1985/yoyogo/ResponseRender"
"github.com/maxzhang1985/yoyogo/ActionResult"
"net/http"
)

func (ctx *HttpContext) HTML(code int, name string, obj interface{}) {
htmlRender := ResponseRender.HTMLDebug{Files: nil,
htmlRender := ActionResult.HTMLDebug{Files: nil,
Glob: "../Static/template/**",
Delims: ResponseRender.Delims{Left: "{[{", Right: "}]}"},
Delims: ActionResult.Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
instance := htmlRender.Instance(name, obj)
_ = instance.Render(ctx.Resp)
}

func (ctx *HttpContext) IndentedJSON(code int, obj interface{}) {
ctx.Render(code, ResponseRender.IndentedJson{Data: obj})
ctx.Render(code, ActionResult.IndentedJson{Data: obj})
}

func (c *HttpContext) SecureJSON(code int, obj interface{}) {
c.Render(code, ResponseRender.SecureJson{Prefix: "", Data: obj})
c.Render(code, ActionResult.SecureJson{Prefix: "", Data: obj})
}

func (c *HttpContext) JSONP(code int, obj interface{}) {
callback := c.QueryStringOrDefault("callback", "")
if callback == "" {
c.Render(code, ResponseRender.Json{Data: obj})
c.Render(code, ActionResult.Json{Data: obj})
return
}
c.Render(code, ResponseRender.Jsonp{Callback: callback, Data: obj})
c.Render(code, ActionResult.Jsonp{Callback: callback, Data: obj})
}

func (c *HttpContext) JSON(code int, obj interface{}) {
c.Render(code, ResponseRender.Json{Data: obj})
c.Render(code, ActionResult.Json{Data: obj})
}

// AsciiJSON serializes the given struct as JSON into the response body with unicode to ASCII string.
// It also sets the Content-Type as "application/json".
func (c *HttpContext) AsciiJSON(code int, obj interface{}) {
c.Render(code, ResponseRender.AsciiJson{Data: obj})
c.Render(code, ActionResult.AsciiJson{Data: obj})
}

// PureJSON serializes the given struct as JSON into the response body.
// PureJSON, unlike JSON, does not replace special html characters with their unicode entities.
func (c *HttpContext) PureJSON(code int, obj interface{}) {
c.Render(code, ResponseRender.PureJson{Data: obj})
c.Render(code, ActionResult.PureJson{Data: obj})
}

// XML serializes the given struct as XML into the response body.
// It also sets the Content-Type as "application/xml".
func (c *HttpContext) XML(code int, obj interface{}) {
c.Render(code, ResponseRender.XML{Data: obj})
c.Render(code, ActionResult.XML{Data: obj})
}

// YAML serializes the given struct as YAML into the response body.
func (c *HttpContext) YAML(code int, obj interface{}) {
c.Render(code, ResponseRender.YAML{Data: obj})
c.Render(code, ActionResult.YAML{Data: obj})
}

// ProtoBuf serializes the given struct as ProtoBuf into the response body.
func (c *HttpContext) ProtoBuf(code int, obj interface{}) {
c.Render(code, ResponseRender.ProtoBuf{Data: obj})
c.Render(code, ActionResult.ProtoBuf{Data: obj})
}

// String writes the given string into the response body.
func (c *HttpContext) Text(code int, format string, values ...interface{}) {
c.Render(code, ResponseRender.Text{Format: format, Data: values})
c.Render(code, ActionResult.Text{Format: format, Data: values})
}

func (c *HttpContext) File(filepath string) {
http.ServeFile(c.Resp, c.Req, filepath)
}

func (c *HttpContext) FileStream(code int, bytes []byte) {
render := ResponseRender.FormFileStream(bytes)
render := ActionResult.FormFileStream(bytes)
c.Render(code, render)
}
14 changes: 14 additions & 0 deletions Controller/ApiController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package Controller

type ApiController struct {
}

func (c *ApiController) GetView() string {
return ""
}

type IController interface {
GetView() string
//ViewData interface{}
//Data interface{}
}
5 changes: 0 additions & 5 deletions Controller/Controller.go

This file was deleted.

4 changes: 4 additions & 0 deletions Controller/RequestParam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package Controller

type RequestParam struct {
}
31 changes: 0 additions & 31 deletions Controller/UserController.go

This file was deleted.

9 changes: 2 additions & 7 deletions DependencyInjection/DefaultServiceProvider.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package DependencyInjection

import "github.com/maxzhang1985/inject"
import "github.com/defval/inject/v2"

type DefaultServiceProvider struct {
container *inject.Container
}

func (d DefaultServiceProvider) GetService(refObject interface{}) (err error) {
err = d.container.Extract(refObject)
if err != nil {
panic(err)
}
return err
}

func (d DefaultServiceProvider) GetServiceByName(refObject interface{}, name string) (err error) {
err = d.container.Extract(refObject, inject.Name(name))
if err != nil {
panic(err)
}

return err
}
15 changes: 5 additions & 10 deletions DependencyInjection/DefaultServiceProviderFactory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package DependencyInjection

import (
"github.com/maxzhang1985/inject"
"github.com/defval/inject/v2"
)

func (sc ServiceCollection) Build() IServiceProvider {
Expand All @@ -15,21 +15,16 @@ func (sc ServiceCollection) Build() IServiceProvider {
if desc.Name != "" {
providerOptions = append(providerOptions, inject.WithName(desc.Name))
}
if desc.Lifetime == Singleton {
providerOptions = append(providerOptions, inject.Lifetime(0))
} else {
providerOptions = append(providerOptions, inject.Lifetime(2))
//prototype is create a new instance on each call.
if desc.Lifetime != Singleton {
providerOptions = append(providerOptions, inject.Prototype())
}

provider := inject.Provide(desc.Provider, providerOptions...)
providers = append(providers, provider)

}
container, err := inject.New(providers...)
if err != nil {
panic(err)
return nil
}
container := inject.New(providers...)

return &DefaultServiceProvider{container}
}
Loading

0 comments on commit d3b6bff

Please sign in to comment.