Giter VIP home page Giter VIP logo

crab's Introduction

Crab 开发必备库

Logo

config

加载ini格式的配置文件, 支持以;或者#开头的注释

type testConf struct {
	DB struct {
		Domain string
		Port   int `default:"9088"`
		Enable bool
	}
    aaa int
}

var conf testConf
if err := LoadConfig(path, &conf); err != nil {
    t.Fatalf(errors.ErrorStack(err))
}
t.Logf("conf:%+v", conf)

配置文件

 [db]
domain    =jd.com
enable=true
# test comments
;port=3306

只要传入对应的ini文件全路径及struct指针就可以了,简单高效.
运行结果:

conf:{DB:{Domain:jd.com Port:9088 Enable:true} aaa:0}

注意:只会解析有访问权限的变量(大写)

handler

简单高效的HTTP路由,支持指定接口函数,支持自动注册接口
指定接口注册示例:

handler.Server.AddHandler(handler.GET, "/test/", false, onTestGet)
handler.Server.AddHandler(handler.POST, "/test/", false, onTestPost)
handler.Server.AddHandler(handler.DELETE, "/test/", false, onTestDelete)

自动注册接口示例:

//以包名为路径
handler.Server.AddInterface(&user{}, "")
//指定path
handler.Server.AddInterface(&user{}, "/api/user/")

type user struct {
}

//DoGet 默认get方法
func (u *user) DoGet(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Get user"))
}

//DoPost 默认post方法
func (u *user) DoPost(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Post user"))
}

orm

只支持mysql
查询示例

result := struct {
	ID       int64
	User     string
	Password string
}{}

if err = NewStmt(db, "userinfo").Where("id=2").Query(&result); err != nil {
	t.Fatal(err.Error())
}

修改示例

data := struct {
	User     string
	Password string
}{
	User:     fmt.Sprintf("new_user_%d", time.Now().Unix()),
	Password: fmt.Sprintf("new_password_%d", time.Now().Unix()),
}

id, err := NewStmt(db, "userinfo").Where("id=2").Update(&data)
if err != nil {
	t.Fatal(err.Error())
}

添加示例

data := struct {
	ID       int64 `db_defult:"auto"`
	User     string
	Password string
}{
	User:     fmt.Sprintf("user_%d", time.Now().Unix()),
	Password: fmt.Sprintf("password_%d", time.Now().Unix()),
}

id, err := NewStmt(db, "userinfo").Insert(&data)
if err != nil {
	t.Fatal(err.Error())
}

crab's People

Contributors

c-wind avatar cjsheng 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.