示例#1
0
func Informaton() {
	count := 10
	for i := 0; i < count; i++ {
		stats := stat.States{}
		stats.MethodName = common.RandText(10)
		stats.NameSpace = "Lasitha"
		stats.ObjectSize = 10
		stats.ElapsedTime = 1
		stats.Status = stat.Sucess
		stat.Add(stats)
	}
}
示例#2
0
//ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
func (_ manager) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	stats := stat.States{}
	stats.ClientIP = r.RemoteAddr
	t := time.Now()
	ptype := ""
	//stats.NameSpace = r.URL.RequestURI()
	//stats.MethodName = ""

	//.
	url_, err := url.QueryUnescape(r.URL.RequestURI())
	w.Header().Add("Access-Control-Allow-Origin", "*") //serv.rb.AddHeader()
	defer func() {
		if rec := recover(); rec != nil {
			log.Println("Internal Server Error: Could not serve page: ", r.Method, url_)
			log.Println(rec)
			log.Printf("%s", debug.Stack())
			w.WriteHeader(http.StatusInternalServerError)
		}
	}()

	if err != nil {
		log.Println("Could not serve page: ", r.Method, r.URL.RequestURI(), "Error:", err)
		w.WriteHeader(400)
		w.Write([]byte("Client sent bad request."))
		return
	}
	stats.NameSpace = url_
	if ep, args, queryArgs, xsrft, found := getEndPointByUrl(r.Method, url_); found {
		stats.NameSpace = url_
		stats.MethodName = ep.name
		ptype = ep.parentTypeName
		ctx := new(Context)
		ctx.writer = w
		ctx.request = r
		ctx.args = args
		ctx.queryArgs = queryArgs
		ctx.xsrftoken = xsrft

		data, state := prepareServe(ctx, ep)

		if state.httpCode == http.StatusOK {
			switch ep.requestMethod {
			case POST, PUT, DELETE, HEAD, OPTIONS:
				{
					if ctx.responseCode == 0 {
						w.WriteHeader(getDefaultResponseCode(ep.requestMethod))
						stats.Status = stat.Sucess
					} else {
						if !ctx.dataHasBeenWritten {
							w.WriteHeader(ctx.responseCode)
							stats.Status = stat.Sucess
						}
					}
				}
			case GET:
				{
					if ctx.responseCode == 0 {
						if !ctx.responseMimeSet {
							w.Header().Set("Content-Type", _manager().getType(ep.parentTypeName).producesMime)
						}
						w.WriteHeader(getDefaultResponseCode(ep.requestMethod))
						stats.Status = stat.Sucess
					} else {
						if !ctx.dataHasBeenWritten {
							if !ctx.responseMimeSet {
								w.Header().Set("Content-Type", _manager().getType(ep.parentTypeName).producesMime)
							}
							w.WriteHeader(ctx.responseCode)
						}
						stats.Status = stat.Sucess
					}

					if !ctx.overide {
						stats.ObjectSize += len(data)
						w.Write(data)
						stats.Status = stat.Sucess
					}

				}
			}

		} else {
			stats.Status = stat.Error
			log.Println("Problem with request. Error:", r.Method, state.httpCode, state.reason, "; Request: ", r.URL.RequestURI())
			w.WriteHeader(state.httpCode)
			w.Write([]byte(state.reason))
		}

	} else {
		stats.Status = stat.Error
		log.Println("Could not serve page, path not found: ", r.Method, url_)
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("The resource in the requested path could not be found."))
	}
	stats.ElapsedTime = int64(time.Now().Sub(t).Nanoseconds() / 1000000) //.Sub(time.Now()).Nanoseconds()
	if ptype != "duov6.com/statservice/StatSvc" {
		stat.Add(stats)
	}

}