goaltools / goal

Goal is a toolkit for high productivity web development in Go language in the spirit of Revel Framework that is built around the concept of code generation.
BSD 2-Clause "Simplified" License
87 stars 3 forks source link

handlers folders disappear? #36

Closed xpbliss closed 8 years ago

xpbliss commented 8 years ago

When i execute "goal new mygoal" , it is ok.

But when i execute "goal run mygoal", in the "mygoal/assets", the "handlers" subdirectory and its files are disappeared? before it, the handlers subdirecory and its files are exists!
My "$GOPATH/src" is ok! so the errors are the following: ``

 Running `goal generate handlers --input ./controllers/ --output ./assets/handlers/`...

Removing "./assets/handlers/" directory if already exists...

Error: path "F:\golang/src/mygoal\controllers" is not inside "$GOPATH/src".

Failed to run a command "go build -o ./bin/run.exe mygoal", error: exit status 1

Preparing "./bin/run.exe"...

Starting a new instance of `./bin/run.exe`...

Adding "./views/" to the list of watched directories...

Failed to start a command `./bin/run.exe`, error: exec: "./bin/run.exe": file does not exist.

`` where is the "run.exe"? what's wrong with it?

ghost commented 8 years ago

@xpbliss Thank you for the bug report. Looks like paths are handled incorrectly on Windows. I'll fix it ASAP.

xpbliss commented 8 years ago

1、How to use pongo2 in the goal? 2、How to get the form post data like revel's binder function? 3、Should give a simple example for goal 4、The App.Before is execute after the App's action?

ghost commented 8 years ago

The issue should be fixed now. There are still tests that do not pass on Windows. In the long run I'll sort it out.

@xpbliss Thank you for your interest.

  1. I've published a demo controller and a sample app that uses pongo2. Possibly not the controller anybody would want to use in production. But works as an illustration.
  2. Either access them directly using c.Request. (Form, FormFile, FormValue, etc.) as you do when using standard lib or minimalistic frameworks. Or use Revel style binding:
func (c *App) Index(name string, age int, smthElse uint) http.Handler {
    ...
}

Though as opposed to Revel there are some limitations. Currently, the list of supported types for binding is limited. Read more about them here. That is a temporary limitation though.

  1. I agree with you, example apps are needed. I'll try to invest my time into that activity when I have some.
  2. Before is executed after Initially but before every action. Request life cycle is described here. This is an implementation of Revel's https://github.com/revel/revel/issues/420.
xpbliss commented 8 years ago

all seem to ok, but some errors as the following:

Method "RenderError" in file "F:/golang/src/github.com/colegion/contrib/controll
ers/templates/templates.go" cannot be treated as action because argument "err" i
s of unsupported type "error".
Method "RenderNotFound" in file "F:/golang/src/github.com/colegion/contrib/contr
ollers/templates/templates.go" cannot be treated as action because argument "msg
s" is of unsupported type "...string".
ghost commented 8 years ago

Those are warnings and are not critical, you can safely ignore them if there are no other error messages and http://localhost:8080 works.

xpbliss commented 8 years ago

How to do auto routes?

ghost commented 8 years ago

Currently Goal doesn't provide auto routes or anything special related to routes at all. Instead it can be used with any router (naoina/denco is the default one, every handler function has to be added manually). I'm, however, considering the use of code generation for improving this part (auto routes, reverse routes, etc.).

xpbliss commented 8 years ago

How to save the render() result to a file?

ghost commented 8 years ago

@xpbliss What controller are you using colegion/contrib/controllers/templates, alkchr/pongoal or your custom one? Do you want to show the rendered page to user then save it to file? Or just save it?

xpbliss commented 8 years ago

I use alkchr/pongoal, and want to save the rendered page as a static html page . Another, How to do some initial work, such as connect database, read config file . Where is the suitable position.?

ghost commented 8 years ago

Here is a demo of saving generated page to ./static/pages directory. As you can see it is located inside Init function. Those functions are "magic" in terms of Goal. They will be called automatically right after all parent controllers (in our case Requests, Pongo2, and Sessions) are initialized.

So, the place to locate your controller related init code is Init. For work with a database, create a new controller package (e.g. ./controllers/postgres/postgres.go) and write the necessary code in Initially, Finally, and Init. Here is a sample of using mgutz/dat:

package postgres

import (
    "database/sql"
    "flag"
    "log"

    _ "github.com/lib/pq" // We are using PostgreSQL driver.
    "gopkg.in/mgutz/dat.v1"
    "gopkg.in/mgutz/dat.v1/sqlx-runner"
)

var (
    // Global database (pooling provided by SQL driver).
    DB *runner.DB

    db   = flag.String("postgres:db", "test", "name of a database")
    usr  = flag.String("postgres:usr", "root", "user name for connecting to DB")
    pwd =  flag.String("postgres:pwd", "", "password for connection to a DB")
    host = flag.String("postgres:host", "localhost", "host of PostgreSQL DB")
    port = flag.Int("postgres:port", 5432, "port of PostgreSQL DB")
    ssl  = flag.Bool("posgres:ssl", false, "use SSL to connect to DB") 
)

// Connection is a controller that makes the use of
// PostgreSQL database inside other controllers possible.
type Connection struct {
    Tx *runner.Tx
}

// Initially begins a transaction.
func (c *Connection) Initially(http.ResponseWriter, *http.Request) bool {
    var err error
    c.Tx, err = DB.Begin()
    return err != nil
}

// Finally commits the transaction and rolls it back if neither commit nor
// rollback were called.
func (c *Connection) Finally(http.ResponseWriter, *http.Request) bool {
    defer c.Tx.AutoRollback()
    err := c.Tx.AutoCommit()
    return err != nil
}

// Init starts a new DB connection.
func Init(g config.Getter) {
    // Create a new database connection.
    db, err := sql.Open("postgres", fmt.Sprintf(
        "dbname=%v user=%v password=%v host=%v port=%v sslmode=%v",
        *db, *usr, *pwd, *host, *port, *ssl,
    ))
    if err != nil {
        log.Panic(err)
    }
    DB = runner.NewDB(db, "postgres")
}

Include in your ./controllers/init.go:

type Controllers struct {
    ...
    *Connection
    ...
}

Then use in your actions as follows:

func (c *App) Index() http.Handler {
    c.Tx.Select(...).From(...).Limit(...).QueryStructs(...)
}

The recommended way of configuration is use of standard flag package (see example above or this). How it works is described here.

For initialization of components not related to the controllers you can use whatever file you find the most suitable. E.g. ./main.go, ./init.go or regular init functions inside any file.

xpbliss commented 8 years ago

Under windows, in the goal.yml,when i modified the /views/app/index.html, the "wathch" do nothing? it must restart "goal run mygoal". maybe such as it ,revel do better.

How to get the all controller's name and its action, so that to use the RBAC function?

ghost commented 8 years ago

Command goal run should be fixed on Windows now.

As for the names of controllers and actions, can you provide a demo of how you're going to use it?

xpbliss commented 8 years ago

https://github.com/mikespook/gorbac, such as "rbac.Set("master", []string{"del.article"}, []string{"editor"})"

ghost commented 8 years ago

@xpbliss I still do not quite understand how you're using that information in the code you've showed.

It is not difficult to modify the process of code generation a little bit to make this functionality possible. However, I'd like to make sure that this is really necessary and we're solving the right problem.

xpbliss commented 8 years ago

r.Get("/dxh/:id", h.App.MyIndex), localhost:8080/dxh/aa?bb=123?cc=456

How to get the values of bb and cc in the controller?

ghost commented 8 years ago

@xpbliss You mean your URL is

localhost:8080/dxh/aa?bb=123&cc=456

Right? If so there are a few ways:

func (c *App) MyIndex(id string, bb, cc int) {
}
id := c.Request.FormValue("id")
bb := c.Request.FormValue("bb")
cc := c.Request.FormValue("cc")
q := c.Request.URL.Query()
bb := q.Get("bb")
cc := q.Get("cc")
xpbliss commented 8 years ago

thanks!

xpbliss commented 8 years ago

if the user controllers have such as the "After,Initially" function, and the assets/*.go also have no necessary for this function?

ghost commented 8 years ago

@xpbliss Handlers in the generated package ./assets/handlers include not only action functions but also New, Before, After, Initially, and Finally methods. They are required for internal purposes and were not supposed to be used by user. If I come up with an easy to implement idea of how to get rid of them, I'll do it. Is that what you've asked?

xpbliss commented 8 years ago

thanks! Should add the func as "RenderJson(), RenderText(),RenderXml(), RenderFile"?, like the revel.

xpbliss commented 8 years ago

1. type User struct { Name string Age string } func (c *App) Update(usr User) http.Handler { fmt.Println("the struct is:", usr) return nil } in routes.go r.Post("/update", h.App.Update), It will show errors:

routes\routes.go:17: "github.com/alkchr/pongoal/example/assets/handlers".App.Upd
ate undefined (type "github.com/alkchr/pongoal/example/assets/handlers".tApp has
 no field or method Update)

It doesn't support struct?

2. Should add the func as "RenderJson(), RenderText(),RenderXml(), RenderFile"?,

ghost commented 8 years ago

@xpbliss You're getting the following warning, right?

Method "Update" in file "..." cannot be treated as action because argument "usr" is of unsupported type "User".

As I mentioned earlier in this thread currently only a set of builtin types is supported that includes:

Binding of more complex data types will be implemented when I start working on #28. I didn't want to add support of Revel like binding of structs (func (c *App) ActionName(arg1 ComplexType) revel.Action) because in most cases binding of all fields is not what's needed and sometimes may be unsafe. Related discussion: https://github.com/jgraham909/bloggo/issues/10#issue-29171349

As for the RenderJSON, RenderText, RenderXML, and RenderFile methods, you're right, they are needed indeed. I'll try to devote some time to work on related controllers. Maybe, today or tomorrow.

xpbliss commented 8 years ago

it's more like beego. such as autoform.

ghost commented 8 years ago

I added initial versions of json and text controllers. They can be added to your app by embedding:

import (
    ...
    "github.com/colegion/contrib/controllers/json"
    "github.com/colegion/contrib/controllers/text"
)

type Controllers struct {
    ...
    *text.Text
    *json.JSON
}

That will make RenderText and RenderJSON methods available. Upgrade of goal is required:

go get -u github.com/colegion/goal
xpbliss commented 8 years ago

good! looking forward to " autoform"

xpbliss commented 8 years ago

How to set the Sessions and Cookies expired time ?

ghost commented 8 years ago

Hi, @xpbliss,

Upgrade sessions package:

go get -u github.com/colegion/contrib/controllers/sessions

Then add to your config/app.ini (1):

[sessions]
# The value that will be used for "Expires" attribute.
# Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
cookie.expires.duration = 1h

or (2):

[sessions]
# The value that will be used for "Max-Age" attribute (seconds).
# It is not supported by IE6, IE7, and IE8. But in other browsers it has
# higher priority than "Expires" (if both are defined).
cookie.maxage = 3600
hsluoyz commented 7 years ago

Hi, I'm the author of casbin. It is an authorization library that supports models like ACL, RBAC, ABAC.

Related to RBAC, casbin has several advantages:

  1. roles can be cascaded, aka roles can have roles.
  2. support resource roles, so users have their roles and resource have their roles too. role = group here.
  3. the permission assignments (or policy in casbin's language) can be persisted in files or database.

So please consider using casbin when goal implements RBAC security. Also let me know if there's any question:)