From 8b01334faf587edc47cb0015b14cde2a4b5b6402 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Sat, 20 May 2017 13:06:25 +0800 Subject: [PATCH 01/14] =?UTF-8?q?fix=20#440=20only=20get=20these=20tables?= =?UTF-8?q?=20information=20in=20custom=20the=20option=20=E2=80=98-tables?= =?UTF-8?q?=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/g_appcode.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index f575568..421b407 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -298,7 +298,14 @@ func gen(dbms, connStr string, mode byte, selectedTableNames map[string]bool, ap defer db.Close() if trans, ok := dbDriver[dbms]; ok { beeLogger.Log.Info("Analyzing database tables...") - tableNames := trans.GetTableNames(db) + var tableNames []string + if len(selectedTableNames) != 0 { + for tableName := range selectedTableNames { + tableNames = append(tableNames, tableName) + } + } else { + tableNames = trans.GetTableNames(db) + } tables := getTableObjects(tableNames, db, trans) mvcPath := new(MvcPath) mvcPath.ModelPath = path.Join(apppath, "models") From f657d22fc7ab3d729d149a8bb63dc62242707c05 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Fri, 26 May 2017 13:30:28 +0800 Subject: [PATCH 02/14] fix tag description,add tag description by getting database table information column comment --- generate/g_appcode.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index 421b407..c1cf068 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -182,6 +182,7 @@ type OrmTag struct { RelFk bool ReverseMany bool RelM2M bool + Comment string //字段注解 } // String returns the source code string for the Table struct @@ -255,7 +256,7 @@ func (tag *OrmTag) String() string { if len(ormOptions) == 0 { return "" } - return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";")) + return fmt.Sprintf("`orm:\"%s\" description:\"%s\"`", strings.Join(ormOptions, ";"), tag.Comment) } func GenerateAppcode(driver, connStr, level, tables, currpath string) { @@ -410,7 +411,7 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin // retrieve columns colDefRows, err := db.Query( `SELECT - column_name, data_type, column_type, is_nullable, column_default, extra + column_name, data_type, column_type, is_nullable, column_default, extra, column_comment FROM information_schema.columns WHERE @@ -423,12 +424,12 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin for colDefRows.Next() { // datatype as bytes so that SQL values can be retrieved - var colNameBytes, dataTypeBytes, columnTypeBytes, isNullableBytes, columnDefaultBytes, extraBytes []byte - if err := colDefRows.Scan(&colNameBytes, &dataTypeBytes, &columnTypeBytes, &isNullableBytes, &columnDefaultBytes, &extraBytes); err != nil { + var colNameBytes, dataTypeBytes, columnTypeBytes, isNullableBytes, columnDefaultBytes, extraBytes, columnCommentBytes []byte + if err := colDefRows.Scan(&colNameBytes, &dataTypeBytes, &columnTypeBytes, &isNullableBytes, &columnDefaultBytes, &extraBytes, &columnCommentBytes); err != nil { beeLogger.Log.Fatal("Could not query INFORMATION_SCHEMA for column information") } - colName, dataType, columnType, isNullable, columnDefault, extra := - string(colNameBytes), string(dataTypeBytes), string(columnTypeBytes), string(isNullableBytes), string(columnDefaultBytes), string(extraBytes) + colName, dataType, columnType, isNullable, columnDefault, extra, columnComment := + string(colNameBytes), string(dataTypeBytes), string(columnTypeBytes), string(isNullableBytes), string(columnDefaultBytes), string(extraBytes), string(columnCommentBytes) // create a column col := new(Column) @@ -441,6 +442,7 @@ func (mysqlDB *MysqlDB) GetColumns(db *sql.DB, table *Table, blackList map[strin // Tag info tag := new(OrmTag) tag.Column = colName + tag.Comment = columnComment if table.Pk == colName { col.Name = "Id" col.Type = "int" From b4bee5ccebc3c4153110926bd6fae246f4b57659 Mon Sep 17 00:00:00 2001 From: Carlos Salguero Date: Mon, 29 May 2017 15:48:57 -0300 Subject: [PATCH 03/14] Added MySQL year data type --- generate/g_appcode.go | 1 + 1 file changed, 1 insertion(+) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index f575568..7da2e69 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -101,6 +101,7 @@ var typeMappingMysql = map[string]string{ "decimal": "float64", "binary": "string", // binary "varbinary": "string", + "year": "int16", } // typeMappingPostgres maps SQL data type to corresponding Go data type From f5471680e4b19b0bddf5734c8861a4d75fe31970 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Tue, 30 May 2017 16:21:45 +0800 Subject: [PATCH 04/14] fix tag description,add tag description by getting database table information column comment --- generate/g_appcode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index c1cf068..a514263 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -182,7 +182,7 @@ type OrmTag struct { RelFk bool ReverseMany bool RelM2M bool - Comment string //字段注解 + Comment string //column comment } // String returns the source code string for the Table struct From 812b8c4e5b824d25b722fddb183b2f9d923685d9 Mon Sep 17 00:00:00 2001 From: hudangwei Date: Tue, 30 May 2017 19:43:40 +0800 Subject: [PATCH 05/14] if tag.Comment is empty, the ORM tag string will not contain description --- generate/g_appcode.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/generate/g_appcode.go b/generate/g_appcode.go index a514263..42c5445 100644 --- a/generate/g_appcode.go +++ b/generate/g_appcode.go @@ -256,7 +256,10 @@ func (tag *OrmTag) String() string { if len(ormOptions) == 0 { return "" } - return fmt.Sprintf("`orm:\"%s\" description:\"%s\"`", strings.Join(ormOptions, ";"), tag.Comment) + if tag.Comment != "" { + return fmt.Sprintf("`orm:\"%s\" description:\"%s\"`", strings.Join(ormOptions, ";"), tag.Comment) + } + return fmt.Sprintf("`orm:\"%s\"`", strings.Join(ormOptions, ";")) } func GenerateAppcode(driver, connStr, level, tables, currpath string) { From 681fc57e1605f157b8070915232d569556b37bf8 Mon Sep 17 00:00:00 2001 From: qida Date: Mon, 5 Jun 2017 09:34:32 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E8=AF=BB=E5=8F=96=20ref(pk)=20=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate/g_model.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generate/g_model.go b/generate/g_model.go index e9b917c..abb6285 100644 --- a/generate/g_model.go +++ b/generate/g_model.go @@ -167,7 +167,7 @@ func Add{{modelName}}(m *{{modelName}}) (id int64, err error) { func Get{{modelName}}ById(id int64) (v *{{modelName}}, err error) { o := orm.NewOrm() v = &{{modelName}}{Id: id} - if err = o.Read(v); err == nil { + if err = o.QueryTable(new({{modelName}})).Filter("Id", id).RelatedSel().One(v); err == nil { return v, nil } return nil, err @@ -225,7 +225,7 @@ func GetAll{{modelName}}(query map[string]string, fields []string, sortby []stri } var l []{{modelName}} - qs = qs.OrderBy(sortFields...) + qs = qs.OrderBy(sortFields...).RelatedSel() if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { if len(fields) == 0 { for _, v := range l { From 0099ef2f92628986ebdaed2ead1a9eb9e81112c3 Mon Sep 17 00:00:00 2001 From: Eyal Post Date: Mon, 5 Jun 2017 18:19:45 +0300 Subject: [PATCH 07/14] support multiple http methods --- generate/swaggergen/g_docs.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index ae8f297..7f882f7 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -707,21 +707,23 @@ func parserComments(f *ast.FuncDecl, controllerName, pkgpath string) error { controllerList[pkgpath+controllerName] = make(map[string]*swagger.Item) item = &swagger.Item{} } - switch HTTPMethod { - case "GET": - item.Get = &opts - case "POST": - item.Post = &opts - case "PUT": - item.Put = &opts - case "PATCH": - item.Patch = &opts - case "DELETE": - item.Delete = &opts - case "HEAD": - item.Head = &opts - case "OPTIONS": - item.Options = &opts + for _, hm := range strings.Split(HTTPMethod, ",") { + switch hm { + case "GET": + item.Get = &opts + case "POST": + item.Post = &opts + case "PUT": + item.Put = &opts + case "PATCH": + item.Patch = &opts + case "DELETE": + item.Delete = &opts + case "HEAD": + item.Head = &opts + case "OPTIONS": + item.Options = &opts + } } controllerList[pkgpath+controllerName][routerPath] = item } From 416fb2ec9d72dee8a8492971595a9911da48d953 Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Wed, 12 Jul 2017 17:23:53 +0300 Subject: [PATCH 08/14] Fix: docs generator skips everything containing 'vendor' in path which is wrong. Fixed that to skip only the 'vendor' dir directly under the project root. Also don't even scan a dir beginning with a '.'. --- generate/swaggergen/g_docs.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/generate/swaggergen/g_docs.go b/generate/swaggergen/g_docs.go index ae8f297..92c7ed8 100644 --- a/generate/swaggergen/g_docs.go +++ b/generate/swaggergen/g_docs.go @@ -104,7 +104,11 @@ func ParsePackagesFromDir(dirpath string) { return nil } - if !strings.Contains(fpath, "vendor") && !strings.Contains(fpath, "tests") { + // 7 is length of 'vendor' (6) + length of file path separator (1) + // so we skip dir 'vendor' which is directly under dirpath + if !(len(fpath) == len(dirpath)+7 && strings.HasSuffix(fpath, "vendor")) && + !strings.Contains(fpath, "tests") && + !(len(fpath) > len(dirpath) && fpath[len(dirpath)+1] == '.') { err = parsePackageFromDir(fpath) if err != nil { // Send the error to through the channel and continue walking From ea63bf253e8006c54beaf276cab10eccb6754332 Mon Sep 17 00:00:00 2001 From: Gnanakeethan Balasubramaniam Date: Thu, 13 Jul 2017 20:30:06 +0530 Subject: [PATCH 09/14] UPDATE: creating ddl migration spec is now possible SUMMARY: The DDL migration can now be generated by adding a `-ddl` and a proper "alter" or "create" as argument value. Migration generation had been modified to accommodate the complementary code for ddl generation Signed-off-by: Gnanakeethan Balasubramaniam --- cmd/commands/generate/generate.go | 3 +- generate/g.go | 1 + generate/g_migration.go | 111 +++++++++++++++++++++--------- 3 files changed, 80 insertions(+), 35 deletions(-) diff --git a/cmd/commands/generate/generate.go b/cmd/commands/generate/generate.go index ce874ff..2fa8cbe 100644 --- a/cmd/commands/generate/generate.go +++ b/cmd/commands/generate/generate.go @@ -22,7 +22,7 @@ import ( "github.com/beego/bee/config" "github.com/beego/bee/generate" "github.com/beego/bee/generate/swaggergen" - beeLogger "github.com/beego/bee/logger" + "github.com/beego/bee/logger" "github.com/beego/bee/utils" ) @@ -71,6 +71,7 @@ func init() { CmdGenerate.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the SQLDriver to connect to a database instance.") CmdGenerate.Flag.Var(&generate.Level, "level", "Either 1, 2 or 3. i.e. 1=models; 2=models and controllers; 3=models, controllers and routers.") CmdGenerate.Flag.Var(&generate.Fields, "fields", "List of table Fields.") + CmdGenerate.Flag.Var(&generate.DDL, "ddl", "Generate DDL Migration") commands.AvailableCommands = append(commands.AvailableCommands, CmdGenerate) } diff --git a/generate/g.go b/generate/g.go index e4260b2..691acc7 100644 --- a/generate/g.go +++ b/generate/g.go @@ -21,3 +21,4 @@ var SQLConn utils.DocValue var Level utils.DocValue var Tables utils.DocValue var Fields utils.DocValue +var DDL utils.DocValue diff --git a/generate/g_migration.go b/generate/g_migration.go index 56ec063..3759f57 100644 --- a/generate/g_migration.go +++ b/generate/g_migration.go @@ -21,7 +21,7 @@ import ( "strings" "time" - beeLogger "github.com/beego/bee/logger" + "github.com/beego/bee/logger" "github.com/beego/bee/logger/colors" "github.com/beego/bee/utils" ) @@ -203,11 +203,31 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { fpath := path.Join(migrationFilePath, fmt.Sprintf("%s_%s.go", today, mname)) if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer utils.CloseFile(f) - content := strings.Replace(MigrationTPL, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) - content = strings.Replace(content, "{{CurrTime}}", today, -1) - content = strings.Replace(content, "{{UpSQL}}", upsql, -1) - content = strings.Replace(content, "{{DownSQL}}", downsql, -1) - f.WriteString(content) + ddlSpec := "" + spec := "" + up := "" + down := "" + if DDL != "" { + ddlSpec = "m.ddlSpec()" + switch strings.Title(DDL.String()) { + case "Create": + spec = strings.Replace(DDLSpecCreate, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) + break + case "Alter": + spec = strings.Replace(DDLSpecAlter, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) + } + spec = strings.Replace(spec, "{{tableName}}", mname, -1) + } else { + up = strings.Replace(MigrationUp, "{{UpSQL}}", upsql, -1) + up = strings.Replace(up, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) + down = strings.Replace(MigrationDown, "{{DownSQL}}", downsql, -1) + down = strings.Replace(down, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) + } + + header := strings.Replace(MigrationHeader, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) + header = strings.Replace(header, "{{ddlSpec}}", ddlSpec, -1) + header = strings.Replace(header, "{{CurrTime}}", today, -1) + f.WriteString(header + spec + up + down) // Run 'gofmt' on the generated source code utils.FormatSourceCode(fpath) fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", fpath, "\x1b[0m") @@ -216,33 +236,56 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { } } -const MigrationTPL = `package main +const ( + MigrationHeader = `package main + import ( + "github.com/astaxie/beego/migration" + ) -import ( - "github.com/astaxie/beego/migration" + // DO NOT MODIFY + type {{StructName}} struct { + migration.Migration + } + + // DO NOT MODIFY + func init() { + m := &{{StructName}}{} + m.Created = "{{CurrTime}}" + {{ddlSpec}} + migration.Register("{{StructName}}", m) + } + ` + + DDLSpecCreate = ` + /* + refer beego/migration/doc.go + */ + func(m *{{StructName}}) ddlSpec(){ + m.CreateTable("{{tableName}}", "InnoDB", "utf8") + m.PriCol("id").SetAuto(true).SetNullable(false).SetDataType("INT(10)").SetUnsigned(true) + + } + ` + DDLSpecAlter = ` + /* + refer beego/migration/doc.go + */ + func(m *{{StructName}}) ddlSpec(){ + m.AlterTable("{{tableName}}") + + } + ` + MigrationUp = ` + // Run the migrations + func (m *{{StructName}}) Up() { + // use m.SQL("CREATE TABLE ...") to make schema update + {{UpSQL}} + }` + MigrationDown = ` + // Reverse the migrations + func (m *{{StructName}}) Down() { + // use m.SQL("DROP TABLE ...") to reverse schema update + {{DownSQL}} + } + ` ) - -// DO NOT MODIFY -type {{StructName}} struct { - migration.Migration -} - -// DO NOT MODIFY -func init() { - m := &{{StructName}}{} - m.Created = "{{CurrTime}}" - migration.Register("{{StructName}}", m) -} - -// Run the migrations -func (m *{{StructName}}) Up() { - // use m.SQL("CREATE TABLE ...") to make schema update - {{UpSQL}} -} - -// Reverse the migrations -func (m *{{StructName}}) Down() { - // use m.SQL("DROP TABLE ...") to reverse schema update - {{DownSQL}} -} -` From 042a4cf2449599030bcec276d1a3029efd946006 Mon Sep 17 00:00:00 2001 From: Gnanakeethan Balasubramaniam Date: Thu, 13 Jul 2017 21:24:53 +0530 Subject: [PATCH 10/14] Update: fixing switch statement Signed-off-by: Gnanakeethan Balasubramaniam --- generate/g_migration.go | 1 - 1 file changed, 1 deletion(-) diff --git a/generate/g_migration.go b/generate/g_migration.go index 3759f57..adb13d0 100644 --- a/generate/g_migration.go +++ b/generate/g_migration.go @@ -212,7 +212,6 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { switch strings.Title(DDL.String()) { case "Create": spec = strings.Replace(DDLSpecCreate, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) - break case "Alter": spec = strings.Replace(DDLSpecAlter, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) } From 3673659178f5925f103e62e25fd3f67ae9e13f09 Mon Sep 17 00:00:00 2001 From: Gnanakeethan Balasubramaniam Date: Sun, 16 Jul 2017 07:25:36 +0530 Subject: [PATCH 11/14] Update: complementary fix for removing calling ddlSpec on migration file. Signed-off-by: Gnanakeethan Balasubramaniam --- generate/g_migration.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/generate/g_migration.go b/generate/g_migration.go index adb13d0..4014a2e 100644 --- a/generate/g_migration.go +++ b/generate/g_migration.go @@ -203,12 +203,10 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { fpath := path.Join(migrationFilePath, fmt.Sprintf("%s_%s.go", today, mname)) if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer utils.CloseFile(f) - ddlSpec := "" spec := "" up := "" down := "" if DDL != "" { - ddlSpec = "m.ddlSpec()" switch strings.Title(DDL.String()) { case "Create": spec = strings.Replace(DDLSpecCreate, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) @@ -224,7 +222,6 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { } header := strings.Replace(MigrationHeader, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) - header = strings.Replace(header, "{{ddlSpec}}", ddlSpec, -1) header = strings.Replace(header, "{{CurrTime}}", today, -1) f.WriteString(header + spec + up + down) // Run 'gofmt' on the generated source code @@ -250,7 +247,6 @@ const ( func init() { m := &{{StructName}}{} m.Created = "{{CurrTime}}" - {{ddlSpec}} migration.Register("{{StructName}}", m) } ` From 552111517e86f085fe7655f72b696be34d37e3a0 Mon Sep 17 00:00:00 2001 From: Gnanakeethan Balasubramaniam Date: Sun, 16 Jul 2017 08:13:08 +0530 Subject: [PATCH 12/14] Revert "Update: complementary fix for removing calling ddlSpec on migration" The odds of getting this perfectly up is not good. This reverts commit 3673659178f5925f103e62e25fd3f67ae9e13f09. --- generate/g_migration.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/generate/g_migration.go b/generate/g_migration.go index 4014a2e..adb13d0 100644 --- a/generate/g_migration.go +++ b/generate/g_migration.go @@ -203,10 +203,12 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { fpath := path.Join(migrationFilePath, fmt.Sprintf("%s_%s.go", today, mname)) if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer utils.CloseFile(f) + ddlSpec := "" spec := "" up := "" down := "" if DDL != "" { + ddlSpec = "m.ddlSpec()" switch strings.Title(DDL.String()) { case "Create": spec = strings.Replace(DDLSpecCreate, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) @@ -222,6 +224,7 @@ func GenerateMigration(mname, upsql, downsql, curpath string) { } header := strings.Replace(MigrationHeader, "{{StructName}}", utils.CamelCase(mname)+"_"+today, -1) + header = strings.Replace(header, "{{ddlSpec}}", ddlSpec, -1) header = strings.Replace(header, "{{CurrTime}}", today, -1) f.WriteString(header + spec + up + down) // Run 'gofmt' on the generated source code @@ -247,6 +250,7 @@ const ( func init() { m := &{{StructName}}{} m.Created = "{{CurrTime}}" + {{ddlSpec}} migration.Register("{{StructName}}", m) } ` From 3523212500f52480379cd7c7240623ef445db906 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 19 Jul 2017 00:27:54 +0800 Subject: [PATCH 13/14] bee add subcommand server --- cmd/bee.go | 1 + cmd/commands/server/server.go | 76 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 cmd/commands/server/server.go diff --git a/cmd/bee.go b/cmd/bee.go index 8cf15aa..87ba122 100644 --- a/cmd/bee.go +++ b/cmd/bee.go @@ -29,6 +29,7 @@ import ( _ "github.com/beego/bee/cmd/commands/pack" _ "github.com/beego/bee/cmd/commands/rs" _ "github.com/beego/bee/cmd/commands/run" + _ "github.com/beego/bee/cmd/commands/server" _ "github.com/beego/bee/cmd/commands/version" "github.com/beego/bee/utils" ) diff --git a/cmd/commands/server/server.go b/cmd/commands/server/server.go new file mode 100644 index 0000000..8c06574 --- /dev/null +++ b/cmd/commands/server/server.go @@ -0,0 +1,76 @@ +// 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 apiapp + +import ( + "net/http" + + beeLogger "github.com/beego/bee/logger" + + "os" + + "github.com/beego/bee/cmd/commands" + "github.com/beego/bee/cmd/commands/version" + "github.com/beego/bee/utils" +) + +var CmdServer = &commands.Command{ + // CustomFlags: true, + UsageLine: "server [port]", + Short: "serving static content over HTTP on port", + Long: ` + The command 'server' creates a Beego API application. +`, + PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() }, + Run: createAPI, +} + +var ( + a utils.DocValue + p utils.DocValue + f utils.DocValue +) + +func init() { + CmdServer.Flag.Var(&a, "a", "Listen address") + CmdServer.Flag.Var(&p, "p", "Listen port") + CmdServer.Flag.Var(&f, "f", "Static files fold") + commands.AvailableCommands = append(commands.AvailableCommands, CmdServer) +} + +func createAPI(cmd *commands.Command, args []string) int { + if len(args) > 0 { + err := cmd.Flag.Parse(args[1:]) + if err != nil { + beeLogger.Log.Error(err.Error()) + } + } + if a == "" { + a = "127.0.0.1" + } + if p == "" { + p = "8080" + } + if f == "" { + cwd, _ := os.Getwd() + f = utils.DocValue(cwd) + } + beeLogger.Log.Infof("Start server on http://%s:%s, static file %s", a, p, f) + err := http.ListenAndServe(string(a)+":"+string(p), http.FileServer(http.Dir(f))) + if err != nil { + beeLogger.Log.Error(err.Error()) + } + return 0 +} From b65934a7647f649fa45225349f083bd15811937f Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 19 Jul 2017 00:28:53 +0800 Subject: [PATCH 14/14] v1.9.0 --- cmd/commands/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/commands/version/version.go b/cmd/commands/version/version.go index 563b6dd..a10cc0d 100644 --- a/cmd/commands/version/version.go +++ b/cmd/commands/version/version.go @@ -57,7 +57,7 @@ Prints the current Bee, Beego and Go version alongside the platform information. } var outputFormat string -const version = "1.8.4" +const version = "1.9.0" func init() { fs := flag.NewFlagSet("version", flag.ContinueOnError)