golang 去我的博客文章中间件样本。 http://justinas.org/writing-http-middleware-in-go/

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 去我的博客文章中间件样本。 http://justinas.org/writing-http-middleware-in-go/相关的知识,希望对你有一定的参考价值。

package main

import (
	"net/http"
	"net/http/httptest"
)

type ModifierMiddleware struct {
	handler http.Handler
}

func (m *ModifierMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	rec := httptest.NewRecorder()
	// passing a ResponseRecorder instead of the original RW
	m.handler.ServeHTTP(rec, r)
	// after this finishes, we have the response recorded
	// and can modify it before copying it to the original RW

	// we copy the original headers first
	for k, v := range rec.Header() {
		w.Header()[k] = v
	}
	// and set an additional one
	w.Header().Set("X-We-Modified-This", "Yup")
	// only then the status code, as this call writes the headers as well 
	w.WriteHeader(418)
        // The body hasn't been written (to the real RW) yet,
        // so we can prepend some data.
        data := []byte("Middleware says hello again. ")

        // But the Content-Length might have been set already,
        // we should modify it by adding the length
        // of our own data.
        // Ignoring the error is fine here:
        // if Content-Length is empty or otherwise invalid,
        // Atoi() will return zero,
        // which is just what we'd want in that case.
        clen, _ := strconv.Atoi(r.Header.Get("Content-Length"))
        clen += len(data)
        w.Header.Set("Content-Length", strconv.Itoa(clen))

        // finally, write out our data
        w.Write(data)
	// then write out the original body
	w.Write(rec.Body.Bytes())
}

func myHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Success!"))
}

func main() {
	mid := &ModifierMiddleware{http.HandlerFunc(myHandler)}

	println("Listening on port 8080")
	http.ListenAndServe(":8080", mid)
}
package main

import (
	"net/http"
)

type AppendMiddleware struct {
	handler http.Handler
}

func (a *AppendMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	a.handler.ServeHTTP(w, r)
	w.Write([]byte("<!-- Middleware says hello! -->"))
}

func myHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Success!"))
}

func main() {
	mid := &AppendMiddleware{http.HandlerFunc(myHandler)}

	println("Listening on port 8080")
	http.ListenAndServe(":8080", mid)
}
package main

import (
	"net/http"
)

func SingleHost(handler http.Handler, allowedHost string) http.Handler {
	ourFunc := func(w http.ResponseWriter, r *http.Request) {
		host := r.Host
		if host == allowedHost {
			handler.ServeHTTP(w, r)
		} else {
			w.WriteHeader(403)
		}
	}
	return http.HandlerFunc(ourFunc)
}


func myHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Success!"))
}

func main() {
	single := SingleHost(http.HandlerFunc(myHandler), "example.com")

	println("Listening on port 8080")
	http.ListenAndServe(":8080", single)
}
package main

import (
	"net/http"
)

type SingleHost struct {
	handler     http.Handler
	allowedHost string
}

func NewSingleHost(handler http.Handler, allowedHost string) *SingleHost {
	return &SingleHost{handler: handler, allowedHost: allowedHost}
}

func (s *SingleHost) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	host := r.Host
	if host == s.allowedHost {
		s.handler.ServeHTTP(w, r)
	} else {
		w.WriteHeader(403)
	}
}

func myHandler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Success!"))
}

func main() {
	single := NewSingleHost(http.HandlerFunc(myHandler), "example.com")

	println("Listening on port 8080")
	http.ListenAndServe(":8080", single)
}

自己喜欢的几个前端框架

文章中的列表中都是一些资源链接,微信不让插入外链,大家可以直接复制链接去我的个人博客查看原文:  http://120.79.75.27/blog/51

Semantic-ui

不说啥,就是喜欢,我觉得最漂亮的前端框架。自己的博客就是基于这个做的

  • 官网  

  • Github  

  • 网易云课堂教程  

  • 我的博客

Foundation

也很漂亮,不过我还没用过,马上学习

  • 官网

  • Github

  • 慕课网教程1

  • 慕课网教程2

Materialize

一个基于 Material Design 的 CSS 框架。

  • 官网

  • Github

Material-ui

集成 Google Material 设计的 React 组件。

  • 官网

  • Github

Phantomjs

可以用于 页面自动化 , 网络监测 , 网页截屏 ,以及 无界面测试 等。

  • 官网

  • Github

Pure

  • 官网

  • GitHub

  • 中文教程

Flat-ui

Flat UI是基于Bootstrap做的Metro化改造,由Designmodo提供。Flat UI包含了很多Bootstrap提供的组件,但是外观更加漂亮。Flat UI Flat UI是一套精美的扁平风格 UI 工具包,基于 Twitter Bootstrap 实现。

  • 官网

  • Github

React-bootstrap

  • 官网

  • Github

Uikit

  • 官网

  • GitHub

  • 中文教程

Metro-ui-css

Win8风格,巨硬粉必备

  • 官网

  • Github

Mui

MUI 是一个轻量级的CSS框架,遵循Google的Material Design设计方针。

  • 官网

  • Github

Frozenui

腾讯的移动端UI框架

  • 官网

  • Github

AlloyUI

  • 官网

  • Github

以上是关于golang 去我的博客文章中间件样本。 http://justinas.org/writing-http-middleware-in-go/的主要内容,如果未能解决你的问题,请参考以下文章

自己喜欢的几个前端框架

通过 HTTP 中间件验证 WebSocket 连接 - Golang

golang中可变长参数的使用

golang中可变长参数的使用

Golang Web入门:如何设计API

Golang Web入门:如何设计API