Coder Social home page Coder Social logo

xuhai008 / beego Goto Github PK

View Code? Open in Web Editor NEW

This project forked from beego/beego

0.0 1.0 0.0 9.41 MB

beego is an open-source, high-performance web framework for the Go programming language.

Home Page: beego.me

License: Other

Go 99.98% Makefile 0.01% Smarty 0.02%

beego's Introduction

Beego Build Status GoDoc Foundation Go Report Card

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.

More info at beego.me.

Quick Start

Please see Documentation for more.

Web Application

Create hello directory, cd hello directory

mkdir hello
cd hello

Init module

go mod init

Download and install

go get github.com/astaxie/beego

Create file hello.go

package main

import "github.com/astaxie/beego/server/web"

func main(){
    web.Run()
}

Build and run

go build hello.go
./hello

Congratulations! You've just built your first beego app.

Using ORM module

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

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

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

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

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

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)
}

Features

  • RESTful support
  • MVC architecture
  • Modularity
  • Auto API documents
  • Annotation router
  • Namespace
  • Powerful development tools
  • Full stack for Web & API

Documentation

Community

License

beego source code is licensed under the Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html).

beego's People

Contributors

astaxie avatar flycash avatar jessonchan avatar slene avatar miraclesu avatar fuxiaohei avatar ysqi avatar coldfire-x avatar kitelife avatar iamcathal avatar amrfaissal avatar jianzhiyao avatar unknwon avatar markismark avatar francoishill avatar gnurub avatar gadelkareem avatar elvizlai avatar smallfish avatar lxshadowxl avatar gnanakeethan avatar phiphi282 avatar gopherchai avatar dennismao avatar matrixik avatar tayoogunbiyi avatar harrywang29 avatar zhengyang avatar pjoe avatar saturn4er avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.