Compare commits
No commits in common. "78a06fb900438aa70b0ac8b81e8bfa2afff6b9e3" and "6b0e2e473964e962050a9f8849dec61ce373e3d8" have entirely different histories.
78a06fb900
...
6b0e2e4739
3 changed files with 20 additions and 155 deletions
|
|
@ -54,11 +54,7 @@ func (c *Context) Next() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) prepare(ctx *Context) {
|
func (c *Context) prepare(ctx *Context) {
|
||||||
// Only parse multipart form if it hasn't been parsed already
|
ctx.Request.httpRequest.ParseMultipartForm(int64(app.Config.Request.MaxUploadFileSize))
|
||||||
// This prevents race conditions when multiple goroutines might access the same request
|
|
||||||
if ctx.Request.httpRequest.MultipartForm == nil {
|
|
||||||
ctx.Request.httpRequest.ParseMultipartForm(int64(app.Config.Request.MaxUploadFileSize))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) GetPathParam(key string) interface{} {
|
func (c *Context) GetPathParam(key string) interface{} {
|
||||||
|
|
|
||||||
83
core.go
83
core.go
|
|
@ -6,7 +6,6 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -52,18 +51,12 @@ type configContainer struct {
|
||||||
|
|
||||||
// App is a struct representing the main application container and its core components.
|
// App is a struct representing the main application container and its core components.
|
||||||
type App struct {
|
type App struct {
|
||||||
t int // for tracking hooks (deprecated - maintained for backward compatibility)
|
t int // for tracking hooks
|
||||||
chain *chain
|
chain *chain
|
||||||
hooks *Hooks
|
hooks *Hooks
|
||||||
Config *configContainer
|
Config *configContainer
|
||||||
}
|
}
|
||||||
|
|
||||||
// requestContext holds request-specific chain execution state
|
|
||||||
type requestContext struct {
|
|
||||||
chainIndex int
|
|
||||||
chainNodes []interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// app is a global instance of the App structure used to manage application configuration and lifecycle.
|
// app is a global instance of the App structure used to manage application configuration and lifecycle.
|
||||||
var app *App
|
var app *App
|
||||||
|
|
||||||
|
|
@ -238,25 +231,10 @@ func (app *App) RegisterRoutes(routes []Route, router *httprouter.Router) *httpr
|
||||||
// makeHTTPRouterHandlerFunc creates an httprouter.Handle that handles HTTP requests with the specified controller and hooks.
|
// makeHTTPRouterHandlerFunc creates an httprouter.Handle that handles HTTP requests with the specified controller and hooks.
|
||||||
func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Handle {
|
func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Handle {
|
||||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
// Create request-specific chain state
|
|
||||||
reqCtx := &requestContext{
|
|
||||||
chainIndex: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare the chain nodes for this request
|
|
||||||
mw := app.hooks.GetHooks()
|
|
||||||
for _, v := range mw {
|
|
||||||
reqCtx.chainNodes = append(reqCtx.chainNodes, v)
|
|
||||||
}
|
|
||||||
rhs := app.combHandlers(h, ms)
|
|
||||||
for _, v := range rhs {
|
|
||||||
reqCtx.chainNodes = append(reqCtx.chainNodes, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store chain state in request context
|
|
||||||
ctx := &Context{
|
ctx := &Context{
|
||||||
Request: &Request{
|
Request: &Request{
|
||||||
httpRequest: r.WithContext(context.WithValue(r.Context(), "goffeeChain", reqCtx)),
|
httpRequest: r,
|
||||||
httpPathParams: ps,
|
httpPathParams: ps,
|
||||||
},
|
},
|
||||||
Response: &Response{
|
Response: &Response{
|
||||||
|
|
@ -279,16 +257,10 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.prepare(ctx)
|
ctx.prepare(ctx)
|
||||||
|
rhs := app.combHandlers(h, ms)
|
||||||
// Execute the first handler in the chain
|
app.prepareChain(rhs)
|
||||||
if len(reqCtx.chainNodes) > 0 {
|
app.t = 0
|
||||||
n := reqCtx.chainNodes[0]
|
app.chain.execute(ctx)
|
||||||
if f, ok := n.(Hook); ok {
|
|
||||||
f(ctx)
|
|
||||||
} else if ff, ok := n.(Controller); ok {
|
|
||||||
ff(ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, header := range ctx.Response.headers {
|
for _, header := range ctx.Response.headers {
|
||||||
w.Header().Add(header.key, header.val)
|
w.Header().Add(header.key, header.val)
|
||||||
|
|
@ -391,30 +363,16 @@ func UseHook(mw Hook) {
|
||||||
|
|
||||||
// Next advances to the next middleware or controller in the chain and invokes it with the given context if available.
|
// Next advances to the next middleware or controller in the chain and invokes it with the given context if available.
|
||||||
func (app *App) Next(c *Context) {
|
func (app *App) Next(c *Context) {
|
||||||
// Get request-specific chain state from context
|
app.t = app.t + 1
|
||||||
if reqCtx, ok := c.Request.httpRequest.Context().Value("goffeeChain").(*requestContext); ok {
|
n := app.chain.getByIndex(app.t)
|
||||||
reqCtx.chainIndex++
|
if n != nil {
|
||||||
if reqCtx.chainIndex < len(reqCtx.chainNodes) {
|
f, ok := n.(Hook)
|
||||||
n := reqCtx.chainNodes[reqCtx.chainIndex]
|
if ok {
|
||||||
if f, ok := n.(Hook); ok {
|
f(c)
|
||||||
f(c)
|
} else {
|
||||||
} else if ff, ok := n.(Controller); ok {
|
ff, ok := n.(Controller)
|
||||||
ff(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Fallback to old behavior (not thread-safe)
|
|
||||||
app.t = app.t + 1
|
|
||||||
n := app.chain.getByIndex(app.t)
|
|
||||||
if n != nil {
|
|
||||||
f, ok := n.(Hook)
|
|
||||||
if ok {
|
if ok {
|
||||||
f(c)
|
ff(c)
|
||||||
} else {
|
|
||||||
ff, ok := n.(Controller)
|
|
||||||
if ok {
|
|
||||||
ff(c)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -452,17 +410,6 @@ func (app *App) prepareChain(hs []interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepareRequestChain prepares a request-specific chain to avoid race conditions
|
|
||||||
func (app *App) prepareRequestChain(requestChain *chain, hs []interface{}) {
|
|
||||||
mw := app.hooks.GetHooks()
|
|
||||||
for _, v := range mw {
|
|
||||||
requestChain.nodes = append(requestChain.nodes, v)
|
|
||||||
}
|
|
||||||
for _, v := range hs {
|
|
||||||
requestChain.nodes = append(requestChain.nodes, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// execute executes the first node in the chain, invoking it as either a Hook or Controller if applicable.
|
// execute executes the first node in the chain, invoking it as either a Hook or Controller if applicable.
|
||||||
func (cn *chain) execute(ctx *Context) {
|
func (cn *chain) execute(ctx *Context) {
|
||||||
i := cn.getByIndex(0)
|
i := cn.getByIndex(0)
|
||||||
|
|
|
||||||
84
graph.go
84
graph.go
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.smarteching.com/zeni/go-chart/v2"
|
"git.smarteching.com/zeni/go-chart/v2"
|
||||||
"git.smarteching.com/zeni/go-charts/v2"
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -45,7 +44,7 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
qtyl := len(labels)
|
qtyl := len(labels)
|
||||||
qtyv := len(values)
|
qtyv := len(values)
|
||||||
|
|
||||||
// check qty and equal values from url
|
// cjeck qty and equal values from url
|
||||||
if qtyl < 2 {
|
if qtyl < 2 {
|
||||||
fmt.Fprintf(w, "Missing captions in pie")
|
fmt.Fprintf(w, "Missing captions in pie")
|
||||||
return
|
return
|
||||||
|
|
@ -86,9 +85,9 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
qtyl := len(labels)
|
qtyl := len(labels)
|
||||||
qtyv := len(values)
|
qtyv := len(values)
|
||||||
|
|
||||||
// check qty and equal values from url
|
// cjeck qty and equal values from url
|
||||||
if qtyl < 2 {
|
if qtyl < 2 {
|
||||||
fmt.Fprintf(w, "Missing captions in bar")
|
fmt.Fprintf(w, "Missing captions in pie")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if qtyv != qtyl {
|
if qtyv != qtyl {
|
||||||
|
|
@ -117,83 +116,6 @@ func Graph(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
fmt.Printf("Error rendering pie chart: %v\n", err)
|
fmt.Printf("Error rendering pie chart: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// case multiple graph bar
|
|
||||||
case "mbar":
|
|
||||||
|
|
||||||
queryValues := r.URL.Query()
|
|
||||||
labels := strings.Split(queryValues.Get("l"), "|")
|
|
||||||
values := strings.Split(queryValues.Get("v"), "|")
|
|
||||||
options := strings.Split(queryValues.Get("o"), "|")
|
|
||||||
|
|
||||||
qtyl := len(labels)
|
|
||||||
qtyv := len(values)
|
|
||||||
qtyo := len(options)
|
|
||||||
|
|
||||||
// check qty and equal values from url
|
|
||||||
if qtyl < 2 {
|
|
||||||
fmt.Fprintf(w, "Missing captions in bar")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if qtyv < 2 {
|
|
||||||
fmt.Fprintf(w, "Missing values in bar")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if qtyo < 2 {
|
|
||||||
fmt.Fprintf(w, "Missing options in bar")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
valuest := [][]float64{
|
|
||||||
{
|
|
||||||
2.0,
|
|
||||||
4.9,
|
|
||||||
7.0,
|
|
||||||
23.2,
|
|
||||||
25.6,
|
|
||||||
76.7,
|
|
||||||
135.6,
|
|
||||||
162.2,
|
|
||||||
32.6,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
2.6,
|
|
||||||
5.9,
|
|
||||||
9.0,
|
|
||||||
26.4,
|
|
||||||
28.7,
|
|
||||||
70.7,
|
|
||||||
175.6,
|
|
||||||
182.2,
|
|
||||||
48.7,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
p, err := charts.BarRender(
|
|
||||||
valuest,
|
|
||||||
charts.XAxisDataOptionFunc([]string{
|
|
||||||
"Apr",
|
|
||||||
"May",
|
|
||||||
"Jun",
|
|
||||||
"Jul",
|
|
||||||
"Aug",
|
|
||||||
"Sep",
|
|
||||||
"Oct",
|
|
||||||
"Nov",
|
|
||||||
"Dec",
|
|
||||||
}),
|
|
||||||
charts.LegendLabelsOptionFunc(labels, charts.PositionRight),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := p.Bytes()
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "image/png")
|
|
||||||
w.Write(buf)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error rendering pie chart: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(w, "Unknown graph %s!\n", kindg)
|
fmt.Fprintf(w, "Unknown graph %s!\n", kindg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue