Skip to content
/ gofr Public

A tool kit, which is include HTTP request/Distributed lock/Frequency controller/Page Execute/Guard panic/slice operator/redpackage and so on.

Notifications You must be signed in to change notification settings

neo532/gofr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gofr Web Framework

Go Report Card Sourcegraph

Gofr is a web framework written in Go (Golang).It aims to be a more easy framework.

Contents

Installation

To install Gofr package, you need to install Go and set your Go workspace first.

  1. The first need Go installed (version 1.12+ is required), then you can use the below Go command to install Gofr.
    $ go get -u github.com/neo532/gofr
  1. Import it in your code:
    import "github.com/neo532/gofr"

Efficient Cache

It makes getting remote data like getting variable.

example

    func main() {
        c := context.Background()
        d := user.Get(c)
        fmt.Println(fmt.Sprintf("%s\t:get,%v", b.Name(), d))

        d := money.Get()
        fmt.Println(fmt.Sprintf("%s\t:get,%v", b.Name(), d))
    }

HTTP request

It is a powerful-tool of request.It contains log/retry.

example

    package main

    import (
        "github.com/neo532/gofr/ghttp"
    )

    type Logger struct {
    }

    func (l *Logger) Log(c context.Context, statusCode int, curl string, limit time.Duration, cost time.Duration, resp []byte, err error) {
        logMsg := fmt.Sprintf("[%s] [code:%+v] [limit:%+v] [cost:%+v] [err:%+v] [%+v]",
            curl,
            statusCode,
            limit,
            cost,
            err,
            string(resp),
        )
        fmt.Println(logMsg)
    }

    type ReqParam struct {
        Directory string `form:"directory,omitempty"`
    }

    type Body struct {
        Directory string `json:"directory"`
    }

    func main() {

        // register logger if it's necessary.
        ghttp.RegLogger(&Logger{})

        // build args
        p := (&ghttp.HTTP{
            Method: "GET",
            URL:    "https://github.com/neo532/gofr",
            Limit:  time.Duration(3) * time.Second, // optional
            Retry:  1,                              // optional, default:1
        }).
        QueryArgs(&ReqParam{Directory: "request"}).                                // optional
        JsonBody(&Body{Directory: "request"}).                                     // optional
        Header(http.Header{"a": []string{"a1", "a2"}, "b": []string{"b1", "b2"}}). // optional
        CheckArgs()

        // check arguments
        if p.Err() != nil {
            fmt.Println(p.Err())
            return
        }

        // request
        p.Request(context.Background())
    }

Distributed lock

It is a distributed lock with signle instance by redis.

example

    package main

    import (
        "github.com/go-redis/redis"
        "github.com/neo532/gofr/tool"
    )

    type RedisOne struct {
        cache *redis.Client
    }

    func (l *RedisOne) Eval(c context.Context, cmd string, keys []string, args []interface{}) (rst interface{}, err error) {
        return l.cache.Eval(cmd, keys, args...).Result()
    }

    var Lock *tool.Lock

    func init(){

        rdb := &RedisOne{
            redis.NewClient(&redis.Options{
                Addr:     "127.0.0.1:6379",
                Password: "password",
            })
        }

        Lock = tool.NewLock(rdb)
    }

    func main() {

        c := context.Background()
        key := "IamAKey"
        expire := time.Duration(10) * time.Second
        wait := time.Duration(2) * time.Second

        code, err := Lock.Lock(c, key, expire, wait)
        Lock.UnLock(c, key, code)
    }

Frequency controller

It is a frequency with signle instance by redis.

example

    package main

    import (
        "github.com/go-redis/redis"
        "github.com/neo532/gofr/tool"
    )

    type RedisOne struct {
        cache *redis.Client
    }

    func (l *RedisOne) Eval(c context.Context, cmd string, keys []string, args []interface{}) (rst interface{}, err error) {
        return l.cache.Eval(cmd, keys, args...).Result()
    }

    var Freq *tool.Freq

    func init(){

        rdb := &RedisOne{
            redis.NewClient(&redis.Options{
                Addr:     "127.0.0.1:6379",
                Password: "password",
            })
        }

        Freq = tool.NewFreq(rdb)
        Freq.Timezone("Local")
    }

    func main() {

        c := context.Background()
        preKey := "user.test"
        rule := []tool.FreqRule{
            tool.FreqRule{Duri: "10000", Times: 80},
            tool.FreqRule{Duri: "today", Times: 5},
        }

        fmt.Println(Freq.IncrCheck(c, preKey, rule...))
        fmt.Println(Freq.Get(c, preKey, rule...))
    }

Page Execute

It is a tool to page slice.

example

    package main

    import (
        "github.com/neo532/gofr/tool"
    )

    func main() {

        arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

        tool.PageExec(len(arr), 3, func(b, e int) {
            fmt.Println(arr[b:e])
        })
        // [1 2 3] [4 5 6] [7 8 9] [10]
    }

Guard panic

About

A tool kit, which is include HTTP request/Distributed lock/Frequency controller/Page Execute/Guard panic/slice operator/redpackage and so on.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages