github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/router_default.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  // Copyright 2019-present Highload LTD (UK CN: 11893420)
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  
    11  package gramework
    12  
    13  // JSON register internal handler that sets json content type
    14  // and serves given handler with GET method
    15  func (app *App) JSON(route string, handler interface{}) *App {
    16  	h := app.defaultRouter.determineHandler(handler)
    17  	app.defaultRouter.Handle(MethodGET, route, jsonHandler(h))
    18  
    19  	return app
    20  }
    21  
    22  func jsonHandler(h func(*Context)) func(*Context) {
    23  	return func(ctx *Context) {
    24  		ctx.SetContentType(jsonCT)
    25  		h(ctx)
    26  	}
    27  }
    28  
    29  // GET registers a handler for a GET request to the given route
    30  func (app *App) GET(route string, handler interface{}) *App {
    31  	app.defaultRouter.Handle(MethodGET, route, handler)
    32  	return app
    33  }
    34  
    35  // DELETE registers a handler for a DELETE request to the given route
    36  func (app *App) DELETE(route string, handler interface{}) *App {
    37  	app.defaultRouter.Handle(MethodDELETE, route, handler)
    38  	return app
    39  }
    40  
    41  // HEAD registers a handler for a HEAD request to the given route
    42  func (app *App) HEAD(route string, handler interface{}) *App {
    43  	app.defaultRouter.Handle(MethodHEAD, route, handler)
    44  	return app
    45  }
    46  
    47  // OPTIONS registers a handler for a OPTIONS request to the given route
    48  func (app *App) OPTIONS(route string, handler interface{}) *App {
    49  	app.defaultRouter.Handle(MethodOPTIONS, route, handler)
    50  	return app
    51  }
    52  
    53  // PUT registers a handler for a PUT request to the given route
    54  func (app *App) PUT(route string, handler interface{}) *App {
    55  	app.defaultRouter.Handle(MethodPUT, route, handler)
    56  	return app
    57  }
    58  
    59  // POST registers a handler for a POST request to the given route
    60  func (app *App) POST(route string, handler interface{}) *App {
    61  	app.defaultRouter.Handle(MethodPOST, route, handler)
    62  	return app
    63  }
    64  
    65  // PATCH registers a handler for a PATCH request to the given route
    66  func (app *App) PATCH(route string, handler interface{}) *App {
    67  	app.defaultRouter.Handle(MethodPATCH, route, handler)
    68  	return app
    69  }
    70  
    71  // Handle registers a new request handle with the given path and method.
    72  // For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
    73  // This function is intended for bulk loading and to allow the usage of less frequently used,
    74  // non-standardized or custom methods (e.g. for internal communication with a proxy).
    75  func (app *App) Handle(method, route string, handler interface{}) *App {
    76  	app.defaultRouter.Handle(method, route, handler)
    77  	return app
    78  }
    79  
    80  // PanicHandler set a handler for unhandled panics
    81  func (app *App) PanicHandler(panicHandler func(*Context, interface{})) *App {
    82  	app.defaultRouter.PanicHandler(panicHandler)
    83  	return app
    84  }
    85  
    86  // NotFound set a handler which is called when no matching route is found
    87  func (app *App) NotFound(notFoundHandler func(*Context)) *App {
    88  	app.defaultRouter.NotFound(notFoundHandler)
    89  	return app
    90  }
    91  
    92  // ServeFile serves a file on a given route
    93  func (app *App) ServeFile(route, file string) *Router {
    94  	return app.defaultRouter.ServeFile(route, file)
    95  }
    96  
    97  // SPAIndex serves an index file or handler on any unregistered route
    98  func (app *App) SPAIndex(pathOrHandler interface{}) *Router {
    99  	return app.defaultRouter.SPAIndex(pathOrHandler)
   100  }
   101  
   102  // HandleMethodNotAllowed changes HandleMethodNotAllowed mode in the router
   103  func (app *App) HandleMethodNotAllowed(newValue bool) (oldValue bool) {
   104  	return app.defaultRouter.HandleMethodNotAllowed(newValue)
   105  }
   106  
   107  // HandleOPTIONS changes HandleOPTIONS mode in the router
   108  func (app *App) HandleOPTIONS(newValue bool) (oldValue bool) {
   109  	return app.defaultRouter.HandleOPTIONS(newValue)
   110  }
   111  
   112  // Sub let you quickly register subroutes with given prefix
   113  // like app.Sub("/v1").Sub("/users").GET("/view/:id", "hi").DELETE("/delete/:id", "hi"),
   114  // that give you /v1/users/view/:id and /v1/users/delete/:id registered
   115  func (app *App) Sub(path string) *SubRouter {
   116  	return app.defaultRouter.Sub(path)
   117  }