mirror of
https://github.com/beego/bee.git
synced 2024-11-24 08:30:53 +00:00
RegisterController changes to Router
This commit is contained in:
parent
714bd493fe
commit
dac1e5ea5e
326
createapp.go
326
createapp.go
@ -1,163 +1,163 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdCreate = &Command{
|
var cmdCreate = &Command{
|
||||||
UsageLine: "create [appname]",
|
UsageLine: "create [appname]",
|
||||||
Short: "create an application base on beego framework",
|
Short: "create an application base on beego framework",
|
||||||
Long: `
|
Long: `
|
||||||
create an application base on beego framework
|
create an application base on beego framework
|
||||||
|
|
||||||
In the current path, will create a folder named [appname]
|
In the current path, will create a folder named [appname]
|
||||||
|
|
||||||
In the appname folder has the follow struct:
|
In the appname folder has the follow struct:
|
||||||
|
|
||||||
|- main.go
|
|- main.go
|
||||||
|- conf
|
|- conf
|
||||||
|- app.conf
|
|- app.conf
|
||||||
|- controllers
|
|- controllers
|
||||||
|- default.go
|
|- default.go
|
||||||
|- models
|
|- models
|
||||||
|- static
|
|- static
|
||||||
|- js
|
|- js
|
||||||
|- css
|
|- css
|
||||||
|- img
|
|- img
|
||||||
|- views
|
|- views
|
||||||
index.tpl
|
index.tpl
|
||||||
|
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdCreate.Run = createapp
|
cmdCreate.Run = createapp
|
||||||
}
|
}
|
||||||
|
|
||||||
func createapp(cmd *Command, args []string) {
|
func createapp(cmd *Command, args []string) {
|
||||||
crupath, _ := os.Getwd()
|
crupath, _ := os.Getwd()
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
fmt.Println("error args")
|
fmt.Println("error args")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
gopath := os.Getenv("GOPATH")
|
gopath := os.Getenv("GOPATH")
|
||||||
if gopath == "" {
|
if gopath == "" {
|
||||||
fmt.Println("you should set GOPATH in the env")
|
fmt.Println("you should set GOPATH in the env")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
haspath := false
|
haspath := false
|
||||||
if crupath != path.Join(gopath, "src") {
|
if crupath != path.Join(gopath, "src") {
|
||||||
wgopath := strings.Split(gopath, ";")
|
wgopath := strings.Split(gopath, ";")
|
||||||
if len(wgopath) >= 1 {
|
if len(wgopath) >= 1 {
|
||||||
for _, wg := range wgopath {
|
for _, wg := range wgopath {
|
||||||
wg = wg + `\src`
|
wg = wg + `\src`
|
||||||
if crupath == wg {
|
if crupath == wg {
|
||||||
haspath = true
|
haspath = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !haspath {
|
if !haspath {
|
||||||
lgopath := strings.Split(gopath, ":")
|
lgopath := strings.Split(gopath, ":")
|
||||||
if len(lgopath) >= 1 {
|
if len(lgopath) >= 1 {
|
||||||
for _, wg := range lgopath {
|
for _, wg := range lgopath {
|
||||||
if crupath == path.Join(wg, "src") {
|
if crupath == path.Join(wg, "src") {
|
||||||
haspath = true
|
haspath = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
haspath = true
|
haspath = true
|
||||||
}
|
}
|
||||||
if !haspath {
|
if !haspath {
|
||||||
fmt.Println("can't create application outside of GOPATH")
|
fmt.Println("can't create application outside of GOPATH")
|
||||||
fmt.Println("you first should `cd $GOPATH/src` then use create")
|
fmt.Println("you first should `cd $GOPATH/src` then use create")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
apppath := path.Join(crupath, args[0])
|
apppath := path.Join(crupath, args[0])
|
||||||
os.Mkdir(apppath, 0755)
|
os.Mkdir(apppath, 0755)
|
||||||
fmt.Println("create app folder:", apppath)
|
fmt.Println("create app folder:", apppath)
|
||||||
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
||||||
fmt.Println("create conf:", path.Join(apppath, "conf"))
|
fmt.Println("create conf:", path.Join(apppath, "conf"))
|
||||||
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
||||||
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
|
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
|
||||||
os.Mkdir(path.Join(apppath, "models"), 0755)
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
||||||
fmt.Println("create models:", path.Join(apppath, "models"))
|
fmt.Println("create models:", path.Join(apppath, "models"))
|
||||||
os.Mkdir(path.Join(apppath, "static"), 0755)
|
os.Mkdir(path.Join(apppath, "static"), 0755)
|
||||||
fmt.Println("create static:", path.Join(apppath, "static"))
|
fmt.Println("create static:", path.Join(apppath, "static"))
|
||||||
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
|
os.Mkdir(path.Join(apppath, "static", "js"), 0755)
|
||||||
fmt.Println("create static js:", path.Join(apppath, "static", "js"))
|
fmt.Println("create static js:", path.Join(apppath, "static", "js"))
|
||||||
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
|
os.Mkdir(path.Join(apppath, "static", "css"), 0755)
|
||||||
fmt.Println("create static css:", path.Join(apppath, "static", "css"))
|
fmt.Println("create static css:", path.Join(apppath, "static", "css"))
|
||||||
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
|
os.Mkdir(path.Join(apppath, "static", "img"), 0755)
|
||||||
fmt.Println("create static img:", path.Join(apppath, "static", "img"))
|
fmt.Println("create static img:", path.Join(apppath, "static", "img"))
|
||||||
fmt.Println("create views:", path.Join(apppath, "views"))
|
fmt.Println("create views:", path.Join(apppath, "views"))
|
||||||
os.Mkdir(path.Join(apppath, "views"), 0755)
|
os.Mkdir(path.Join(apppath, "views"), 0755)
|
||||||
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
|
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
|
||||||
writetofile(path.Join(apppath, "conf", "app.conf"), "appname = "+args[0])
|
writetofile(path.Join(apppath, "conf", "app.conf"), "appname = "+args[0])
|
||||||
|
|
||||||
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
||||||
writetofile(path.Join(apppath, "controllers", "default.go"), controllers)
|
writetofile(path.Join(apppath, "controllers", "default.go"), controllers)
|
||||||
|
|
||||||
fmt.Println("create views index.tpl:", path.Join(apppath, "views", "index.tpl"))
|
fmt.Println("create views index.tpl:", path.Join(apppath, "views", "index.tpl"))
|
||||||
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)
|
writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)
|
||||||
|
|
||||||
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
||||||
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", args[0], -1))
|
writetofile(path.Join(apppath, "main.go"), strings.Replace(maingo, "{{.Appname}}", args[0], -1))
|
||||||
}
|
}
|
||||||
|
|
||||||
var maingo = `package main
|
var maingo = `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"{{.Appname}}/controllers"
|
"{{.Appname}}/controllers"
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beego.RegisterController("/", &controllers.MainController{})
|
beego.Router("/", &controllers.MainController{})
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
var controllers = `package controllers
|
var controllers = `package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MainController struct {
|
type MainController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *MainController) Get() {
|
func (this *MainController) Get() {
|
||||||
this.Data["Username"] = "astaxie"
|
this.Data["Username"] = "astaxie"
|
||||||
this.Data["Email"] = "astaxie@gmail.com"
|
this.Data["Email"] = "astaxie@gmail.com"
|
||||||
this.TplNames = "index.tpl"
|
this.TplNames = "index.tpl"
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
var indextpl = `<!DOCTYPE html>
|
var indextpl = `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>beego welcome template</title>
|
<title>beego welcome template</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Hello, world!{{.Username}},{{.Email}}</h1>
|
<h1>Hello, world!{{.Username}},{{.Email}}</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
|
|
||||||
func writetofile(filename, content string) {
|
func writetofile(filename, content string) {
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
f.WriteString(content)
|
f.WriteString(content)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user