aresgo

Aresgo

aresgo是一个简单快速开发go应用的高性能框架,你可以用她来开发一些Api、Web及其他的一些服务应用,她是一个RESTful的框架。她包含快速的Http实现、Url路由与转发、Redis的实现、Mysql的CURD实现、JSON和INI配置文件的读写,以及其他一些方法的使用。后续会继续将一些常用应用添加到框架中。

产品特点(Features)

安装(Installation)

使用“go get”命令:

$ go get github.com/misgo/aresgo

用法(Usage)

使用aresgo框架,你只需要在源文件都加上:

import “github.com/misgo/aresgo”

或者如果使用框架中的某个包的方法,可以这样使用:

import “github.com/misgo/aresgo/text”

更多实例参见:aresgo-demo

http实现


import "github.com/misgo/aresgo"

func main(){

  //初始化路由
  router := aresgo.Routing()

  //定义404错误页
  router.Get("/404.html", NotFound)
  router.NotFound = NotFound  //只要访问不存在的方法或地址就会跳转这个页面

  //输出方法
  router.Get("/hello/:name", Hello)   //Get方法请求,Post请求时会报错
  router.Post("/hello/:name", Hello)  //Post方法请求,Get请求时会报错

  //注册对象,注册后对象(struct)的所有公共方法可以被调用
  router.Register("/passport/", &action.UserAction{}, aresgo.ActionGet) 
  
  //POST or GET or ...请求被拒绝时执行的方法,取决于路由方法的设置
  router.MethodNotAllowed = DisAllowedMethod  
  
  //默认http拦截器方法,当拦截器方法返回false,且未有指定跳转时执行此方法
  router.HttpModuleDenied = httpModuleDenied
  
  //静态文件目录,提供一种方式用来访问静态资源,如包含页面的管理系统中涉及的CSS、Javascritp、图片等静态资源
 // 路径设计必须用这种形式:/XXX/*filepath(必须以/*filepath为结尾标识),还必须有文件目录的绝对地址路径
 //如静态文件路径存放在目录/var/www/static中,访问是想通过这种形式访问:http://www.XXX.com/static/XXX.js
 //可以这样设置:r.ServerFiles("/static/*filepath","/var/www/static")
 //通过这种方式可以创建一个纯静态的文件服务器,或者搭建一个包含模板静态资源的应用
  router.ServeFiles("/static/*filepath", "/var/www/static")
  //监听IP与端口,阻塞式服务
  router.Listen(127.0.0.1:8010)

}

//404错误页
func NotFound(ctx *aresgo.Context) {
	fmt.Fprint(ctx, "页面不存在!\n")
}
// 欢迎页
func Hello(ctx *aresgo.Context) {
	fmt.Fprintf(ctx, "hello, 欢迎【%s】光临!\n", ctx.UserValue("name"))
}

更多http实例参见:aresgo-demo/Server.go

mysql实现

导入aresgo框架

import “github.com/misgo/aresgo”

需要指定数据库配置的路径:

aresgo.DbConfigPath = [你的数据库配置文件路径]

更多数据库实例可以参见:aresgo-demo/Database.go

redis实现

导入aresgo框架

import “github.com/misgo/aresgo”

需要指定redis配置的路径:

aresgo.RedisConfigPath = [你的redis配置文件路径]

配置文件的Json格式

{
    "dev": {
        "master": {
            "ip": "10.168.31.33",
            "port": "6379",
            "password":"123456",
            "maxidle":10,
            "idletimeout":10,
            "maxactive":1024,
             "db":5
        },
        "slave": {
            "ip": "10.168.31.33",
            "port": "6379",
            "password":"",
            "maxidle":10,
            "idletimeout":10,
            "maxactive":1024,
            "db":5
        }
    },
    "other": {
       ...
      }
    }
}

配置文件操作

导入配置文件包

import(
    "github.com/misgo/aresgo"
    "github.com/misgo/aresgo/config"
)

实例化配置文件

jsonConfiger, err1:= aresgo.LoadConfig("json", [json配置文件路径])
iniConfiger, err 2:= aresgo.LoadConfig("ini", [ini配置文件路径])

Json文件内容

{
    "dev": {
        "master": {
            "ip": "10.168.31.33",
            "port": "6379",
            "password":"123456",
            "maxidle":10,
            "idletimeout":10,
            "maxactive":1024,
             "db":5
        },
        "slave": {
           ...
        }
    },
    "other": {
       ...
      }
    }
}

ini文件内容

[dev.master]
ip=127.0.0.1
port=6379
db=gomaster

获取数据库实例dev中主库配置master的ip地址:

json

ip := jsonConfiger.String("dev.master.ip")

ini

ip := iniConfiger.String("dev.master->ip")

获取数据库实例dev中主库配置master的ip2地址,如果不存在返回一个默认值: json

ip := jsonConfiger.DefaultString("dev.master.ip2", "192.168.0.1")

ini

ip := iniConfiger.DefaultString("dev.master->ip2", "not choose")

获取数据库实例dev中主库配置的所有项 json

obj, err := jsonConfiger.GetVal("dev.master") 

ini

obj, err:= iniConfiger.GetSection("dev.master") 

更多redis实例参见:aresgo-demo/Config.go