hook

package
v1.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2024 License: MIT Imports: 14 Imported by: 0

README

// 例子

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/mzky/utils/hook"
	"net/http"
)

func main() {
	engine := gin.New()
	engine.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World!")
	})
	engine.GET("/login.html", func(c *gin.Context) {
		c.String(http.StatusOK, "login.html ok!")
	})

	// The default value of `hook` is "HTTP/1.1 302 Found\r\n\r\n<script>location.protocol='https:'</script>\r\n"
	// Restore the default values of the standard library ↓
	// srv.SetDefaultBadRequest("HTTP/1.0 400 Bad Request\r\n\r\nClient sent an HTTP request to an HTTPS server.\n")

	srv := hook.NewServer(":1234", engine)

	// custom response
	//srv.SetRedirectPath("/login.html")
	//body := "---- message ----"
	//srv.SetResponse(body, func(r *http.Response) {
	//	r.StatusCode = 400
	//	r.Status = http.StatusText(400)
	//	r.Header.Set("Content-Type", "text/html")
	//})

	// custom response 2
	//srv.SetRedirectPath("/login.html")
	//srv.SetResponse("", func(r *http.Response) {
	//	r.StatusCode = 307
	//	r.Status = http.StatusText(307)
	//	r.Header.Set("Timing-Allow-Origin", "*")
	//	r.Header.Set("Non-Authoritative-Reason", "HSTS") // 这种方式重定向比script快
	//	r.Header.Set("x-xss-protection", "0")
	//	r.Header.Set("Cross-Origin-Resource-Policy", "Cross-Origin")
	//	r.Header.Set("Content-Type", "text/html; charset=utf-8")
	//	r.Header.Set("Location", "https://192.168.0.188:7569") // 可选配重定向地址
	//})

	fmt.Println(srv.ListenAndServeTLS("server.pem", "server.key"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	http.Server
	// contains filtered or unexported fields
}

func NewServer added in v1.5.2

func NewServer(addr string, handler http.Handler) *Server

func (*Server) ListenAndServeTLS added in v1.5.1

func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error

func (*Server) SetDefaultBadRequest added in v1.5.3

func (srv *Server) SetDefaultBadRequest(dbr string)

func (*Server) SetRedirectPath added in v1.5.3

func (srv *Server) SetRedirectPath(rp string)

func (*Server) SetResponse added in v1.5.3

func (srv *Server) SetResponse(body string, fn func(r *http.Response))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL