From d41abdb5e4ef7bc789c74dad58cebe315811ca09 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Thu, 8 Oct 2020 23:17:38 +0800 Subject: [PATCH] Remove scripts directory; update readme --- CONTRIBUTING.md | 13 +- README.md | 209 ++++++++++++++++++++++++++++++- build_info.go | 2 +- core/config/config.go | 3 - scripts/adapter.sh | 6 - scripts/prepare_etcd.sh | 8 -- scripts/test.sh | 14 --- scripts/test_docker_compose.yaml | 55 -------- 8 files changed, 214 insertions(+), 96 deletions(-) delete mode 100644 scripts/adapter.sh delete mode 100644 scripts/prepare_etcd.sh delete mode 100644 scripts/test.sh delete mode 100644 scripts/test_docker_compose.yaml diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5035ae94..cb279cbb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -17,11 +17,14 @@ go get -u golang.org/x/tools/cmd/goimports go get -u github.com/gordonklaus/ineffassign ``` -And the go into project directory, run : +Put those lines into your pre-commit githook script: ```shell script -cp ./githook/pre-commit ./.git/hooks/pre-commit +goimports -w -format-only ./ + +ineffassign . + +staticcheck -show-ignored -checks "-ST1017,-U1000,-ST1005,-S1034,-S1012,-SA4006,-SA6005,-SA1019,-SA1024" ./ ``` -This will add git hooks into .git/hooks. Or you can add it manually. ## Prepare middleware @@ -33,7 +36,7 @@ You can run: ```shell script docker-compose -f scripts/test_docker_compose.yml up -d ``` -Unit tests read addressed from environment, here is an example: +Unit tests read addresses from environment, here is an example: ```shell script export ORM_DRIVER=mysql export ORM_SOURCE="beego:test@tcp(192.168.0.105:13306)/orm_test?charset=utf8" @@ -86,5 +89,5 @@ documenting your bug report or improvement proposal. If it does, it never hurts to add a quick "+1" or "I have this problem too". This will help prioritize the most common problems and requests. -Also if you don't know how to use it. please make sure you have read though +Also, if you don't know how to use it. please make sure you have read through the docs in http://beego.me/docs \ No newline at end of file diff --git a/README.md b/README.md index de8f0063..934fc429 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,12 @@ It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific feature ## Quick Start +###### Please see [Documentation](http://beego.me/docs) for more. + +###### [beego-example](https://github.com/beego-dev/beego-example) + +### Web Application + #### Create `hello` directory, cd `hello` directory mkdir hello @@ -25,10 +31,10 @@ It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific feature ```go package main -import "github.com/astaxie/beego" +import "github.com/astaxie/beego/server/web" func main(){ - beego.Run() + web.Run() } ``` #### Build and run @@ -40,9 +46,204 @@ func main(){ Congratulations! You've just built your first **beego** app. -###### Please see [Documentation](http://beego.me/docs) for more. +### Using ORM module + +```go + +package main + +import ( + "github.com/astaxie/beego/client/orm" + "github.com/astaxie/beego/core/logs" + _ "github.com/go-sql-driver/mysql" +) + +// User - +type User struct { + ID int `orm:"column(id)"` + Name string `orm:"column(name)"` +} + +func init() { + // need to register models in init + orm.RegisterModel(new(User)) + + // need to register db driver + orm.RegisterDriver("mysql", orm.DRMySQL) + + // need to register default database + orm.RegisterDataBase("default", "mysql", "beego:test@tcp(192.168.0.105:13306)/orm_test?charset=utf8") +} + +func main() { + // automatically build table + orm.RunSyncdb("default", false, true) + + // create orm object, and it will use `default` database + o := orm.NewOrm() + + // data + user := new(User) + user.Name = "mike" + + // insert data + id, err := o.Insert(user) + if err != nil { + logs.Info(err) + } + + // ... +} +``` + +### Using httplib as http client +```go +package main + +import ( + "github.com/astaxie/beego/client/httplib" + "github.com/astaxie/beego/core/logs" +) + +func main() { + // Get, more methods please read docs + req := httplib.Get("http://beego.me/") + str, err := req.String() + if err != nil { + logs.Error(err) + } + logs.Info(str) +} + +``` + +### Using config module + +```go +package main + +import ( + "context" + + "github.com/astaxie/beego/core/config" + "github.com/astaxie/beego/core/logs" +) + +var ( + ConfigFile = "./app.conf" +) + +func main() { + cfg, err := config.NewConfig("ini", ConfigFile) + if err != nil { + logs.Critical("An error occurred:", err) + panic(err) + } + res, _ := cfg.String(context.Background(), "name") + logs.Info("load config name is", res) +} +``` +### Using logs module +```go +package main + +import ( + "github.com/astaxie/beego/core/logs" +) + +func main() { + err := logs.SetLogger(logs.AdapterFile, `{"filename":"project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10,"color":true}`) + if err != nil { + panic(err) + } + logs.Info("hello beego") +} +``` +### Using timed task + +```go +package main + +import ( + "context" + "time" + + "github.com/astaxie/beego/core/logs" + "github.com/astaxie/beego/task" +) + +func main() { + // create a task + tk1 := task.NewTask("tk1", "0/3 * * * * *", func(ctx context.Context) error { logs.Info("tk1"); return nil }) + + // check task + err := tk1.Run(context.Background()) + if err != nil { + logs.Error(err) + } + + // add task to global todolist + task.AddTask("tk1", tk1) + + // start tasks + task.StartTask() + + // wait 12 second + time.Sleep(12 * time.Second) + defer task.StopTask() +} +``` + +### Using cache module + +```go +package main + +import ( + "context" + "time" + + "github.com/astaxie/beego/client/cache" + + // don't forget this + _ "github.com/astaxie/beego/client/cache/redis" + + "github.com/astaxie/beego/core/logs" +) + +func main() { + // create cache + bm, err := cache.NewCache("redis", `{"key":"default", "conn":":6379", "password":"123456", "dbNum":"0"}`) + if err != nil { + logs.Error(err) + } + + // put + isPut := bm.Put(context.Background(), "astaxie", 1, time.Second*10) + logs.Info(isPut) + + isPut = bm.Put(context.Background(), "hello", "world", time.Second*10) + logs.Info(isPut) + + // get + result, _ := bm.Get(context.Background(),"astaxie") + logs.Info(string(result.([]byte))) + + multiResult, _ := bm.GetMulti(context.Background(), []string{"astaxie", "hello"}) + for i := range multiResult { + logs.Info(string(multiResult[i].([]byte))) + } + + // isExist + isExist, _ := bm.IsExist(context.Background(), "astaxie") + logs.Info(isExist) + + // delete + isDelete := bm.Delete(context.Background(), "astaxie") + logs.Info(isDelete) +} +``` -###### [beego-example](https://github.com/beego-dev/beego-example) ## Features diff --git a/build_info.go b/build_info.go index 33287090..42f42c28 100644 --- a/build_info.go +++ b/build_info.go @@ -28,5 +28,5 @@ var ( const ( // VERSION represent beego web framework version. - VERSION = "1.12.2" + VERSION = "2.0.0-alpha" ) diff --git a/core/config/config.go b/core/config/config.go index 0891e571..deac8e3e 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -184,17 +184,14 @@ func (c *BaseConfiger) Strings(ctx context.Context, key string) ([]string, error return strings.Split(res, ";"), nil } -// TODO remove this before release v2.0.0 func (c *BaseConfiger) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...DecodeOption) error { return errors.New("unsupported operation") } -// TODO remove this before release v2.0.0 func (c *BaseConfiger) Sub(ctx context.Context, key string) (Configer, error) { return nil, errors.New("unsupported operation") } -// TODO remove this before release v2.0.0 func (c *BaseConfiger) OnChange(ctx context.Context, key string, fn func(value string)) { // do nothing } diff --git a/scripts/adapter.sh b/scripts/adapter.sh deleted file mode 100644 index ce2d319a..00000000 --- a/scripts/adapter.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -# using pkg/adapter. Usually you want to migrate to V2 smoothly, you could running this script - -find ./ -name '*.go' -type f -exec sed -i '' -e 's/github.com\/astaxie\/beego/github.com\/astaxie\/beego\/pkg\/adapter/g' {} \; -find ./ -name '*.go' -type f -exec sed -i '' -e 's/"github.com\/astaxie\/beego\/pkg\/adapter"/beego "github.com\/astaxie\/beego\/pkg\/adapter"/g' {} \; diff --git a/scripts/prepare_etcd.sh b/scripts/prepare_etcd.sh deleted file mode 100644 index d34c05a3..00000000 --- a/scripts/prepare_etcd.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -etcdctl put current.float 1.23 -etcdctl put current.bool true -etcdctl put current.int 11 -etcdctl put current.string hello -etcdctl put current.serialize.name test -etcdctl put sub.sub.key1 sub.sub.key \ No newline at end of file diff --git a/scripts/test.sh b/scripts/test.sh deleted file mode 100644 index 473a7066..00000000 --- a/scripts/test.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -docker-compose -f "$(pwd)/scripts/test_docker_compose.yaml" up -d - -export ORM_DRIVER=mysql -export TZ=UTC -export ORM_SOURCE="beego:test@tcp(localhost:13306)/orm_test?charset=utf8" - -go test "$(pwd)/..." - -# clear all container -docker-compose -f "$(pwd)/scripts/test_docker_compose.yaml" down - - diff --git a/scripts/test_docker_compose.yaml b/scripts/test_docker_compose.yaml deleted file mode 100644 index f22b6deb..00000000 --- a/scripts/test_docker_compose.yaml +++ /dev/null @@ -1,55 +0,0 @@ -version: "3.8" -services: - redis: - container_name: "beego-redis" - image: redis - environment: - - ALLOW_EMPTY_PASSWORD=yes - ports: - - "6379:6379" - - mysql: - container_name: "beego-mysql" - image: mysql:5.7.30 - ports: - - "13306:3306" - environment: - - MYSQL_ROOT_PASSWORD=1q2w3e - - MYSQL_DATABASE=orm_test - - MYSQL_USER=beego - - MYSQL_PASSWORD=test - - postgresql: - container_name: "beego-postgresql" - image: bitnami/postgresql:latest - ports: - - "5432:5432" - environment: - - ALLOW_EMPTY_PASSWORD=yes - ssdb: - container_name: "beego-ssdb" - image: wendal/ssdb - ports: - - "8888:8888" - memcache: - container_name: "beego-memcache" - image: memcached - ports: - - "11211:11211" - etcd: - command: > - sh -c " - etcdctl put current.float 1.23 - && etcdctl put current.bool true - && etcdctl put current.int 11 - && etcdctl put current.string hello - && etcdctl put current.serialize.name test - " - container_name: "beego-etcd" - environment: - - ALLOW_NONE_AUTHENTICATION=yes -# - ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379 - image: bitnami/etcd - ports: - - "2379:2379" - - "2380:2380" \ No newline at end of file