| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 | 
							- package mux
 
- import (
 
- 	"net/http"
 
- 	"path/filepath"
 
- 	"strings"
 
- 	"github.com/gorilla/mux"
 
- )
 
- type (
 
- 	MiddlewareFunc = mux.MiddlewareFunc
 
- 	Router         = mux.Router
 
- )
 
- var (
 
- 	router         = mux.NewRouter()
 
- 	defaultMethods = []string{http.MethodGet, http.MethodPost}
 
- )
 
- func New() *mux.Router {
 
- 	return mux.NewRouter()
 
- }
 
- func RegisterWith(router *mux.Router, path string, handler http.HandlerFunc, methods ...string) {
 
- 	r := router.HandleFunc(path, handler)
 
- 	if len(methods) == 0 {
 
- 		methods = defaultMethods
 
- 	}
 
- 	r.Methods(methods...)
 
- }
 
- func Default() *mux.Router {
 
- 	return router
 
- }
 
- func Register(path string, handler http.HandlerFunc, methods ...string) {
 
- 	RegisterWith(router, path, handler, methods...)
 
- }
 
- func RegisterHandle(path string, handler http.Handler, methods ...string) {
 
- 	Register(path, handler.ServeHTTP, methods...)
 
- }
 
- type GroupMux struct {
 
- 	router *mux.Router
 
- }
 
- func (g *GroupMux) Register(path string, handler http.HandlerFunc, methods ...string) {
 
- 	r := g.router.Handle(path, handler)
 
- 	if len(methods) == 0 {
 
- 		methods = defaultMethods
 
- 	}
 
- 	r.Methods(methods...)
 
- }
 
- func (g *GroupMux) RegisterHandle(path string, handler http.Handler, methods ...string) {
 
- 	g.Register(path, handler.ServeHTTP, methods...)
 
- }
 
- func (g *GroupMux) Use(mwf ...mux.MiddlewareFunc) {
 
- 	g.router.Use(mwf...)
 
- }
 
- func Group(prefix string) *GroupMux {
 
- 	return &GroupMux{router: router.PathPrefix(prefix).Subrouter()}
 
- }
 
- func Use(handle mux.MiddlewareFunc) {
 
- 	router.Use(handle)
 
- }
 
- func Params(r *http.Request) map[string]string {
 
- 	return mux.Vars(r)
 
- }
 
- func SetStaticPath(router *mux.Router, path, dir string) {
 
- 	if strings.LastIndex(path, "/") == -1 {
 
- 		path = path + "/"
 
- 	}
 
- 	router.PathPrefix(path).Handler(http.StripPrefix(path, http.FileServer(http.Dir(filepath.Join(dir)))))
 
- }
 
- // func SetStaticPath() {
 
- // 	router.PathPrefix("/web/").Handler(http.StripPrefix("/web/", http.FileServer(http.Dir("web"))))
 
- // 	router.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir("web/js"))))
 
- // 	router.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir("web/css"))))
 
- // 	router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir("web/img"))))
 
- // 	router.PathPrefix("/fonts/").Handler(http.StripPrefix("/fonts/", http.FileServer(http.Dir("web/fonts"))))
 
- // 	router.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", http.FileServer(http.Dir("web/dist/3d-orgin/assets"))))
 
- // 	router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("web/dist/static"))))
 
- // 	router.PathPrefix("/3d-orgin/").Handler(http.StripPrefix("/3d-orgin/", http.FileServer(http.Dir("web/dist/3d-orgin"))))
 
- // 	// favicon.ico 特殊处理
 
- // 	Register("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 
- // 		http.ServeFile(w, r, "web/dist/favicon.ico")
 
- // 	}, http.MethodGet)
 
- // }
 
- func CORS() mux.MiddlewareFunc {
 
- 	return func(next http.Handler) http.Handler {
 
- 		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
- 			w.Header().Add("Access-Control-Allow-Origin", "*")
 
- 			w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
 
- 			w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
 
- 			w.Header().Add("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
 
- 			w.Header().Add("Access-Control-Allow-Credentials", "true")
 
- 			next.ServeHTTP(w, req)
 
- 		})
 
- 	}
 
- }
 
 
  |