• HandleDecorator装饰器模式实践


    HandleDecorator装饰器模式实践

    实现记录时间的中间件,记录地址中间件

    装饰模式原理

    连接地址(点我)

    代码实现

    // 中间件的实现
    // 装饰模式
    package main
    
    import (
    	"encoding/json"
    	"errors"
    	"fmt"
    	"log"
    	"net/http"
    	"time"
    )
    
    // 路由的第一种形式
    type Router1 struct {}
    
    func (r1 *Router1) ServeHTTP(w http.ResponseWriter,r *http.Request) {
    	type Res1 struct {
    		Msg string
    	}
    	fmt.Println("欢迎来到路由1")
    	res := &Res1{Msg:"hello router 1"}
    	marshal, _ := json.Marshal(res)
    	w.Write(marshal)
    }
    
    // 路由的第二种形式
    func Router2() http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		fmt.Println("欢迎来到路由2")
    		type Res2 struct {
    			Msg string
    		}
    		res2 := &Res2{Msg:"touter2"}
    		marshal, _ := json.Marshal(res2)
    		w.Write(marshal)
    	})
    }
    
    func Router5(w http.ResponseWriter, r *http.Request) {
    	type Res5 struct {
    		Msg string
    	}
    	fmt.Println("欢迎来到路由5")
    	res := &Res5{Msg:"hello router 5"}
    	marshal, _ := json.Marshal(res)
    	w.Write(marshal)
    }
    
    // 时间记录中间件
    func traceTime(next http.Handler) http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		log.Println("时间记录中间件")
    		t := time.Now()
    		next.ServeHTTP(w, r)
    		since := time.Since(t)
    		log.Println("路由运行时间为", since)
    	})
    }
    
    // 地址记录中间件
    func tranceUrl(next http.Handler) http.Handler {
    	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    		log.Println("访问地址记录中间件")
    		next.ServeHTTP(w, r)
    		log.Println("访问地址为", r.URL.String())
    	})
    }
    
    func main() {
    	http.Handle("/router1", new(Router1))
    	http.Handle("/router2", Router2())
    	// 第三种路由
    	handler3 := func(http.ResponseWriter, *http.Request) {
    		type Res3 struct {
    			Msg string
    		}
    		fmt.Println("欢迎来到路由3")
    		//res := &Res3{Msg:"hello router 3"}
    		//marshal, _ := json.Marshal(res)
    		//w.Write(marshal)
    	}
    	http.HandleFunc("/router3", handler3)
    	http.HandleFunc("/router4", func(w http.ResponseWriter, r *http.Request) {
    		type Res4 struct {
    			Msg string
    		}
    		fmt.Println("欢迎来到路由4")
    		res := &Res4{Msg:"hello router 4"}
    		marshal, _ := json.Marshal(res)
    		w.Write(marshal)
    	})
    	http.HandleFunc("/router5", Router5)
    	http.Handle("/router6", traceTime(tranceUrl(new(Router1))))
    	http.Handle("/router7", traceTime(tranceUrl(Router2())))
    	err := http.ListenAndServe(":9000", nil)
    	if err != nil {
    		log.Println(err, errors.New("服务器启动失败"))
    	}
    }
    
  • 相关阅读:
    JDK5.0新特性系列目录
    JDK5.0新特性系列11.5.2线程 同步装置之CountDownLatch
    JDK5.0新特性系列11.5.4线程 同步装置之Exchanger
    JDK5.0新特性系列11.5.1线程 同步装置之Semaphore
    Axure RP Pro 6.0 原型设计工具(产品经理必备)
    JDK5.0新特性系列11.4线程 Condition
    OLTP 和 OLAP 的区别
    JDK5.0新特性系列11.5.3线程 同步装置之CyclicBarrier
    电脑通过手机上网的设置
    (转)刚开始Outlook Addin的布署问题
  • 原文地址:https://www.cnblogs.com/maomaomaoge/p/14129393.html
Copyright © 2020-2023  润新知