<commit_before>package chttp

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"strings"
)

type action map[string]*curl

type method struct {
	method string
}

type curl struct {
	permissions []string
	mfunc       func(Context)
	method      method
}

type Context struct {
	Response http.ResponseWriter
	Request  *http.Request
	Params   url.Values
}

//路由
type Route struct {
	action action
}

//路由
var route = &Route{
	action: make(map[string]*curl),
}

//实现ServeHTTP
func (this *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	//如果有错误就恢复 并跳转到错误页面
	defer func() {
		if err := recover(); err != nil {
			log.Fatal(err)
			this.Error500(w, r)
		}
	}()
	url := r.URL.String()
	ix := strings.Index(url, "?")
	if ix > 0 {
		url = url[0:ix]
	}
	if this.isStatic(url) { //js、css、image
		if staticRoute == nil {
			staticRoute = &StaticRoute{
				handle: http.StripPrefix(Conf.Static.Prefix, http.FileServer(http.Dir(Conf.Static.Dir))),
			}
		}
		staticRoute.ServeHTTP(w, r)
		return
	} else if this.isHtml(url) { //html
		if htmlRoute == nil {
			htmlRoute = &StaticRoute{
				handle: http.StripPrefix(Conf.Html.Prefix, http.FileServer(http.Dir(Conf.Html.Dir))),
			}
		}
		htmlRoute.ServeHTTP(w, r)
		return
	} else if murl, ok := this.action[url]; ok { //请求url判断
		if murl.method.method == ALL || murl.method.method == r.Method { //请求方式判断
			//判断Action Url是否设置了url权限
			if murl.permissions != nil && len(murl.permissions) > 0 {

			} else {
				c, err := this.Context(w, r)
				if err != nil {
					fmt.Errorf(err.Error())
				}
				murl.mfunc(c) //调用函数
			}
		} else {
			this.Error404(w, r)
		}
	} else {
		this.Error404(w, r)
	}
}

//404 error
func (*Route) Error500(w http.ResponseWriter, r *http.Request) {<commit_msg>修改recover<commit_after>package chttp

import (
	"fmt"
	"log"
	"net/http"
	"net/url"
	"strings"
)

type action map[string]*curl

type method struct {
	method string
}

type curl struct {
	permissions []string
	mfunc       func(Context)
	method      method
}

type Context struct {
	Response http.ResponseWriter
	Request  *http.Request
	Params   url.Values
}

//路由
type Route struct {
	action action
}

//路由
var route = &Route{
	action: make(map[string]*curl),
}

//实现ServeHTTP
func (this *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	//如果有错误就恢复 并跳转到错误页面
	defer func() {
		if err := recover(); err != nil {
			log.Println(err)
			this.Error500(w, r)
		}
	}()
	url := r.URL.String()
	ix := strings.Index(url, "?")
	if ix > 0 {
		url = url[0:ix]
	}
	if this.isStatic(url) { //js、css、image
		if staticRoute == nil {
			staticRoute = &StaticRoute{
				handle: http.StripPrefix(Conf.Static.Prefix, http.FileServer(http.Dir(Conf.Static.Dir))),
			}
		}
		staticRoute.ServeHTTP(w, r)
		return
	} else if this.isHtml(url) { //html
		if htmlRoute == nil {
			htmlRoute = &StaticRoute{
				handle: http.StripPrefix(Conf.Html.Prefix, http.FileServer(http.Dir(Conf.Html.Dir))),
			}
		}
		htmlRoute.ServeHTTP(w, r)
		return
	} else if murl, ok := this.action[url]; ok { //请求url判断
		if murl.method.method == ALL || murl.method.method == r.Method { //请求方式判断
			//判断Action Url是否设置了url权限
			if murl.permissions != nil && len(murl.permissions) > 0 {

			} else {
				c, err := this.Context(w, r)
				if err != nil {
					log.Println(err)
				}
				murl.mfunc(c) //调用函数
			}
		} else {
			this.Error404(w, r)
		}
	} else {
		this.Error404(w, r)
	}
}

//500 error
func (*Route) Error500(w http.ResponseWriter, r *http.Request) {
{'fraction_non_alphanumeric': 0.15493773530263538, 'fraction_numerical': 0.010425716768027803, 'mean_word_length': 3.08274231678487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5411693', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1426, 'n_words': 298}