// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package new
import (
"fmt"
"github.com/beego/bee/cmd/commands/version"
"os"
path "path/filepath"
"runtime"
"strings"
"github.com/beego/bee/cmd/commands"
"github.com/beego/bee/logger"
"github.com/beego/bee/logger/colors"
"github.com/beego/bee/utils"
)
var module utils.DocValue
var beegoVersion utils.DocValue
var CmdNew = &commands.Command{
UsageLine: "new [appname] [-module=true] [-beego=v1.12.1]",
Short: "Creates a Beego application",
Long: `
Creates a Beego application for the given app name in the current directory.
now supoort generate a go modules project
The command 'new' creates a folder named [appname] [-module=true] [-beego=v1.12.1] and generates the following structure:
├── main.go
├── go.mod
├── {{"conf"|foldername}}
│ └── app.conf
├── {{"controllers"|foldername}}
│ └── default.go
├── {{"models"|foldername}}
├── {{"routers"|foldername}}
│ └── router.go
├── {{"tests"|foldername}}
│ └── default_test.go
├── {{"static"|foldername}}
│ └── {{"js"|foldername}}
│ └── {{"css"|foldername}}
│ └── {{"img"|foldername}}
└── {{"views"|foldername}}
└── index.tpl
`,
PreRun: nil,
Run: CreateApp,
}
var appconf = `appname = {{.Appname}}
httpport = 8080
runmode = dev
`
var maingo = `package main
import (
_ "{{.Appname}}/routers"
"github.com/astaxie/beego"
)
func main() {
beego.Run()
}
`
var router = `package routers
import (
"{{.Appname}}/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
}
`
var goMod = `
module %s
go %s
require github.com/astaxie/beego %s
require github.com/smartystreets/goconvey v1.6.4
`
var test = `package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "{{.Appname}}/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
`
var controllers = `package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "astaxie@gmail.com"
c.TplName = "index.tpl"
}
`
var indextpl = `
Beego
Welcome to Beego
Beego is a simple & powerful Go web framework which is inspired by tornado and sinatra.