diff --git a/app/controllers/api/UserController.go b/app/controllers/api/UserController.go new file mode 100644 index 0000000..4d3836d --- /dev/null +++ b/app/controllers/api/UserController.go @@ -0,0 +1,36 @@ +package api + +import ( + "github.com/revel/revel" +// "encoding/json" +// "gopkg.in/mgo.v2/bson" + . "github.com/leanote/leanote/app/lea" +// "github.com/leanote/leanote/app/info" +// "github.com/leanote/leanote/app/types" +// "io/ioutil" +// "fmt" +// "math" +// "os" +// "path" +// "strconv" +) +type User struct { + *revel.Controller +} + +type ApiUser struct { + *revel.Controller +} + +// 修改用户名, 需要重置session +func (c ApiUser) Info() revel.Result { + Log("APIUser"); + return c.RenderTemplate("home/index.html"); +// return nil; +} + +func (c User) Info() revel.Result { + Log("APIUser"); + return c.RenderTemplate("home/index.html"); +// return nil; +} \ No newline at end of file diff --git a/app/init.go b/app/init.go index 9f4a834..41954ed 100644 --- a/app/init.go +++ b/app/init.go @@ -16,7 +16,8 @@ func init() { // Filters is the default set of global filters. revel.Filters = []revel.Filter{ revel.PanicFilter, // Recover from panics and display an error page instead. - revel.RouterFilter, // Use the routing table to select the right Action + RouterFilter, + // revel.RouterFilter, // Use the routing table to select the right Action // AuthFilter, // Invoke the action. revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters. revel.ParamsFilter, // Parse parameters into Controller.Params. diff --git a/app/lea/Route.go b/app/lea/Route.go new file mode 100644 index 0000000..4681cbb --- /dev/null +++ b/app/lea/Route.go @@ -0,0 +1,60 @@ +package lea + +import ( + "github.com/revel/revel" + "net/url" + "strings" +) + +// overwite revel RouterFilter +// /api/user/Info => ApiUser.Info() +func RouterFilter(c *revel.Controller, fc []revel.Filter) { + // Figure out the Controller/Action + var route *revel.RouteMatch = revel.MainRouter.Route(c.Request.Request) + if route == nil { + c.Result = c.NotFound("No matching route found: " + c.Request.RequestURI) + return + } + + // The route may want to explicitly return a 404. + if route.Action == "404" { + c.Result = c.NotFound("(intentionally)") + return + } + + //---------- + // life start + path := c.Request.Request.URL.Path + // Log(c.Request.Request.URL.Host) + if strings.HasPrefix(path, "/api") || strings.HasPrefix(path, "api") { + route.ControllerName = "Api" + route.ControllerName + } + // end + + // Set the action. + if err := c.SetAction(route.ControllerName, route.MethodName); err != nil { + c.Result = c.NotFound(err.Error()) + return + } + + // Add the route and fixed params to the Request Params. + c.Params.Route = route.Params + + // Add the fixed parameters mapped by name. + // TODO: Pre-calculate this mapping. + for i, value := range route.FixedParams { + if c.Params.Fixed == nil { + c.Params.Fixed = make(url.Values) + } + if i < len(c.MethodType.Args) { + arg := c.MethodType.Args[i] + c.Params.Fixed.Set(arg.Name, value) + } else { + revel.WARN.Println("Too many parameters to", route.Action, "trying to add", value) + break + } + } + + fc[0](c, fc[1:]) +} +