Giter Site home page Giter Site logo

pipeflow's Introduction

Pipeflow

Pipeflow is a middleware container which is used in my own blog system.

Quick Look

package main

import (
    "fmt"
    "github.com/go-redis/redis"
    "net/http"
    "pipeflow"
    "reflect"
)

func main() {
	fb := pipeflow.NewBuilder()

	fb.Run(func(ctx pipeflow.HTTPContext) {
		fmt.Println("request URL: " + ctx.Request.RequestURI)
	})

	fb.UseCors([]string{"http://localhost:18080"}, nil, nil, nil)

	fb.Use(func(ctx pipeflow.HTTPContext, next func()) {
		fmt.Println("first")
		next()
		fmt.Println("first post action")
	})

	fb.Use(func(ctx pipeflow.HTTPContext, next func()) {
		fmt.Println("second")
		next()
		fmt.Println("second post action")
	})

	fb.Use(func(ctx pipeflow.HTTPContext, next func()) {
		if token := ctx.Request.Header.Get("token"); token != "" {
			next()
		} else {
			ctx.ResponseWriter.WriteHeader(http.StatusNonAuthoritativeInfo)
			_, _ = ctx.ResponseWriter.Write([]byte("NonAuthoritativeInfo"))
		}
	})

	redisClient := redis.NewClient(&redis.Options{Addr: "127.0.0.1:6379", Password: "password", DB: 0})
	if _, err := redisClient.Ping().Result(); nil != err {
		panic(err)
	}

	// fb.SetResource("redis", redisClient)
	// fb.SetResourceWithType(reflect.TypeOf(redisClient), redisClient)
	fb.SetResourceAlsoWithType("redis", redisClient)

	fb.Map("/hey", func(ctx pipeflow.HTTPContext) {
		var client, _ = ctx.GetResource("redis").(*redis.Client)
		var count, _ = client.Get("count").Int()
		client.Set("count", count+1, -1)
		_, _ = ctx.ResponseWriter.Write([]byte("hello"))
	}, pipeflow.HTTPPost, pipeflow.HTTPGet)

	fb.GET("/hello", func(ctx pipeflow.HTTPContext) {
		var client1, _ = ctx.GetResource("redis").(*redis.Client)
		var client2 = ctx.GetResourceByType(reflect.TypeOf((*redis.Client)(nil))).(*redis.Client)
		var count, _ = client1.Get("count").Int()
		client2.Set("count", count+1, -1)
		_, _ = ctx.ResponseWriter.Write([]byte("hello"))
	})

	fb.GET("/{foo}/hello?id&name", func(ctx pipeflow.HTTPContext) {
		_, _ = fmt.Fprintln(ctx.ResponseWriter, "foo = "+ctx.Vars["foo"]+", id = "+ctx.Request.Form.Get("id")+", name = "+ctx.Request.Form.Get("name"))
	})

	_ = http.ListenAndServe(":8888", fb.Build())
}

Request: http://localhost:8888/bar/hello?id=1&name=nomyfan

Response: foo = bar, id = 1, name = nomyfan

Console output:

request URL: /bar/hello?id=1&name=nomyfan
first
second
second post action
first post action

pipeflow's People

Contributors

nomyfan avatar

Watchers

 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.