crab

command module
v1.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

README

Crab 开发必备库

Logo

codecov

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

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
http
util
aes
str

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL