diff options
| author | Jeff Carr <[email protected]> | 2025-03-18 15:14:23 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-18 15:14:23 -0500 |
| commit | ef371cda38829dc4b836e218b25ad20204d54ec7 (patch) | |
| tree | 337e51a574d0ae78d8f39f2c959a99fae3badb56 /gin.go | |
| parent | 5a9ae561ee0dbd9b82285594b6fb8b9fe3eea0bd (diff) | |
maybe this is a good idea, maybe not
Diffstat (limited to 'gin.go')
| -rw-r--r-- | gin.go | 127 |
1 files changed, 118 insertions, 9 deletions
@@ -1,7 +1,7 @@ // Copyright 2017-2025 WIT.COM Inc. All rights reserved. // Use of this source code is governed by the GPL 3.0 -package pinpb +package ginpb // this is similar to 'gin' but specifically only for // sending and working with protocol buffers @@ -10,8 +10,10 @@ package pinpb // package (there is no go.sum file) import ( + "log" "net/http" "net/url" + "regexp" "sync" "text/template" ) @@ -32,7 +34,7 @@ func (engine *Engine) allocateContext(maxParams uint16) *Context { return &Context{engine: engine, params: &v} } -func New() *Engine { +func New(opts ...OptionFunc) *Engine { engine := &Engine{ RouterGroup: RouterGroup{ Handlers: nil, @@ -46,10 +48,8 @@ func New() *Engine { engine.RouterGroup.engine = engine engine.pool.New = func() any { return engine.allocateContext(engine.maxParams) - // return nil } - // return engine.With(opts...) - return engine + return engine.With(opts...) } // With returns a Engine with the configuration set in the OptionFunc. @@ -70,10 +70,11 @@ func Default(opts ...OptionFunc) *Engine { // Context is the most important part of gin. It allows us to pass variables between middleware, // manage the flow, validate the JSON of a request and render a JSON response for example. type Context struct { - engine *Engine - params *Params - Request *http.Request - handlers HandlersChain + writermem responseWriter + engine *Engine + params *Params + Request *http.Request + handlers HandlersChain // queryCache caches the query result from c.Request.URL.Query(). queryCache url.Values @@ -205,3 +206,111 @@ type Engine struct { trustedCIDRs []*net.IPNet */ } + +// GET is a shortcut for router.Handle("GET", path, handlers). +func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes { + return group.handle(http.MethodGet, relativePath, handlers) +} + +// IRouter defines all router handle interface includes single and group router. +type IRouter interface { + IRoutes + Group(string, ...HandlerFunc) *RouterGroup + Any(string, ...HandlerFunc) IRoutes +} + +// IRoutes defines all router handle interface. +type IRoutes interface { + // Use(...HandlerFunc) IRoutes + + // Handle(string, string, ...HandlerFunc) IRoutes + Any(string, ...HandlerFunc) IRoutes + GET(string, ...HandlerFunc) IRoutes + // POST(string, ...HandlerFunc) IRoutes + // DELETE(string, ...HandlerFunc) IRoutes +} + +func (group *RouterGroup) handle(httpMethod, relativePath string, handlers HandlersChain) IRoutes { + // absolutePath := group.calculateAbsolutePath(relativePath) + handlers = group.combineHandlers(handlers) + group.engine.addRoute(httpMethod, relativePath, handlers) + return group.returnObj() +} + +func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain { + finalSize := len(group.Handlers) + len(handlers) + // assert1(finalSize < int(abortIndex), "too many handlers") + mergedHandlers := make(HandlersChain, finalSize) + copy(mergedHandlers, group.Handlers) + copy(mergedHandlers[len(group.Handlers):], handlers) + return mergedHandlers +} + +func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { + // assert1(path[0] == '/', "path must begin with '/'") +} + +func (group *RouterGroup) returnObj() IRoutes { + if group.root { + return group.engine + } + return group +} + +// Any registers a route that matches all the HTTP methods. +// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE. +func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes { + for _, method := range anyMethods { + group.handle(method, relativePath, handlers) + } + + return group.returnObj() +} + +var ( + // regEnLetter matches english letters for http method name + regEnLetter = regexp.MustCompile("^[A-Z]+$") + + // anyMethods for RouterGroup Any method + anyMethods = []string{ + http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, + http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect, + http.MethodTrace, + } +) + +// Run attaches the router to a http.Server and starts listening and serving HTTP requests. +// It is a shortcut for http.ListenAndServe(addr, router) +// Note: this method will block the calling goroutine indefinitely unless an error happens. +func (engine *Engine) Run(addr string) (err error) { + // defer func() { debugPrintError(err) }() + + /* + if engine.isUnsafeTrustedProxies() { + debugPrint("[WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.\n" + + "Please check https://github.com/gin-gonic/gin/blob/master/docs/doc.md#dont-trust-all-proxies for details.") + } + */ + // engine.updateRouteTrees() + // address := resolveAddress(addr) + address := addr + log.Printf("Listening and serving HTTP on %s\n", address) + err = http.ListenAndServe(address, engine.Handler()) + return +} + +func (engine *Engine) Handler() http.Handler { + return engine +} + +// ServeHTTP conforms to the http.Handler interface. +func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { + c := engine.pool.Get().(*Context) + c.writermem.reset(w) + c.Request = req + // c.reset() + + // engine.handleHTTPRequest(c) + + engine.pool.Put(c) +} |
