Giter Site home page Giter Site logo

droplet's Introduction

droplet

Decouple the service access layer so that the business only cares about input and output.

Background

When you write a http web server, you will see code like below everywhere. Such as gin:

func consumerCreate(c *gin.Context) {
	// read header
    requestId, _ := c.Get("X-Request-Id")
    param, exist := c.Get("requestBody")
    
    u4 := uuid.NewV4()
    
    if !exist || len(param.([]byte)) < 1 {
        err := errno.New(errno.InvalidParam)
        logger.WithField(conf.RequestId, requestId).Error(err.ErrorDetail())
        
        // write response
        c.AbortWithStatusJSON(http.StatusBadRequest, err.Response())
        return
    }
    
    if err := service.ConsumerCreate(param, u4.String()); err != nil {
    	// handler error
        handleServiceError(c, requestId, err)
        return
    }
    
    // write response
    c.JSON(http.StatusOK, errno.Succeed())
}

Wow, All of those codes is not related with business logic, right? So is here anyway could let us just care what application need and what application should return? That is why droplet was born.

Let We have a look at the code after used droplet:

type JsonInput struct {
    ID    string   `auto_read:"id,path" json:"id"`
    User  string   `auto_read:"user,header" json:"user"`
    IPs   []string `json:"ips"`
    Count int      `json:"count"`
    Body  []byte   `auto_read:"@body"`
}

func JsonInputDo(ctx droplet.Context) (interface{}, error) {
    input := ctx.Input().(*JsonInput)
    return input, nil
}

Here are things droplet help you to do:

  • Automatically read parameter from request by you input's tag
  • Check returns result and error, check error and convert it to a pre-define data structure
  • Write result to response

Droplet is not only that, please keep reading to find more.

Concept

Droplet is designed to work between the service access layer and the application layer, and is an intermediate layer framework. It provides a pipeline mechanism based on the reified Struct, and provides a lot of convenient middleware based on this.

Intall

go get github.com/shiningrush/droplet

droplet's People

Contributors

shiningrush avatar nic-chen 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.