2017-11-25 17:23:46 +00:00
# Beego [![Build Status](https://travis-ci.org/astaxie/beego.svg?branch=master)](https://travis-ci.org/astaxie/beego) [![GoDoc](http://godoc.org/github.com/astaxie/beego?status.svg)](http://godoc.org/github.com/astaxie/beego) [![Foundation](https://img.shields.io/badge/Golang-Foundation-green.svg)](http://golangfoundation.org) [![Go Report Card](https://goreportcard.com/badge/github.com/astaxie/beego)](https://goreportcard.com/report/github.com/astaxie/beego)
2013-06-19 14:27:21 +00:00
2015-07-18 10:09:22 +00:00
beego is used for rapid development of RESTful APIs, web apps and backend services in Go.
It is inspired by Tornado, Sinatra and Flask. beego has some Go-specific features such as interfaces and struct embedding.
2012-12-16 15:43:42 +00:00
2020-12-12 14:04:26 +00:00
[Officail website ](http://beego.me )
[Example ](https://github.com/beego-dev/beego-example )
2020-11-12 13:36:11 +00:00
2020-12-12 14:04:26 +00:00
> If you could not open official website, go to [beedoc](https://github.com/beego/beedoc)
2020-11-12 13:36:11 +00:00
## beego 1.x and 2.x
1. If you are working on beego v1.x please try `go get github.com/astaxie/beego@v1.12.3`
2020-12-12 14:04:26 +00:00
2. If you want to try beego 2.0.0, run `go get github.com/astaxie/beego@v2.0.0`
2017-04-02 09:04:08 +00:00
## Quick Start
2013-06-22 11:17:50 +00:00
2020-10-08 15:17:38 +00:00
### Web Application
2020-07-09 10:14:31 +00:00
#### Create `hello` directory, cd `hello` directory
2020-07-09 09:18:01 +00:00
2020-07-10 01:58:06 +00:00
mkdir hello
cd hello
2020-07-09 10:14:31 +00:00
2020-07-09 09:18:01 +00:00
#### Init module
2020-07-10 01:58:06 +00:00
go mod init
#### Download and install
2020-12-12 14:04:26 +00:00
go get github.com/astaxie/beego@v2.0.0
2020-07-09 09:18:01 +00:00
2017-04-02 09:04:08 +00:00
#### Create file `hello.go`
2015-07-16 04:32:07 +00:00
```go
package main
2020-10-08 15:17:38 +00:00
import "github.com/astaxie/beego/server/web"
2015-07-16 04:32:07 +00:00
func main(){
2020-10-08 15:17:38 +00:00
web.Run()
2015-07-16 04:32:07 +00:00
}
```
2017-04-02 09:04:08 +00:00
#### Build and run
2015-07-16 04:32:07 +00:00
go build hello.go
./hello
2017-04-02 09:04:08 +00:00
#### Go to [http://localhost:8080](http://localhost:8080)
Congratulations! You've just built your first **beego** app.
2020-10-08 15:17:38 +00:00
### 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)
}
```
2015-07-16 04:32:07 +00:00
2020-06-29 13:53:52 +00:00
2013-06-22 11:17:50 +00:00
## Features
2012-12-16 15:43:42 +00:00
2013-12-22 07:56:15 +00:00
* RESTful support
2013-09-25 13:40:24 +00:00
* MVC architecture
2014-08-22 05:32:54 +00:00
* Modularity
* Auto API documents
* Annotation router
* Namespace
2015-02-02 14:33:59 +00:00
* Powerful development tools
2014-08-22 05:32:54 +00:00
* Full stack for Web & API
2013-06-22 11:17:50 +00:00
## Documentation
2014-08-22 05:32:54 +00:00
* [English ](http://beego.me/docs/intro/ )
* [中文文档 ](http://beego.me/docs/intro/ )
2015-12-26 13:43:49 +00:00
* [Русский ](http://beego.me/docs/intro/ )
2012-12-16 15:43:42 +00:00
2014-08-22 05:32:54 +00:00
## Community
2012-12-17 05:33:32 +00:00
2014-08-22 05:32:54 +00:00
* [http://beego.me/community ](http://beego.me/community )
2016-02-02 15:34:32 +00:00
* Welcome to join us in Slack: [https://beego.slack.com ](https://beego.slack.com ), you can get invited from [here ](https://github.com/beego/beedoc/issues/232 )
2019-10-10 03:23:19 +00:00
* QQ Group Group ID:523992905
2013-06-21 08:40:58 +00:00
2017-04-02 09:04:08 +00:00
## License
2013-06-22 11:17:50 +00:00
2016-01-13 14:20:34 +00:00
beego source code is licensed under the Apache Licence, Version 2.0
2014-08-22 05:32:54 +00:00
(http://www.apache.org/licenses/LICENSE-2.0.html).