github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/opts.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  import (
    14  	"errors"
    15  
    16  	"github.com/apex/log"
    17  	"github.com/valyala/fasthttp"
    18  )
    19  
    20  // OptAppName sets app.name and app.serverBase.Name
    21  func OptAppName(n string) func(*App) {
    22  	return func(app *App) {
    23  		assertAppNotNill(app)
    24  		app.name = n
    25  	}
    26  }
    27  
    28  // OptUseCustomLogger allows use custom preconfigured Apex logger.
    29  // For exmaple with custom Handler.
    30  func OptUseCustomLogger(logger *log.Logger) func(*App) {
    31  	return func(app *App) {
    32  		assertAppNotNill(app)
    33  		app.Logger = logger
    34  	}
    35  }
    36  
    37  // OptUseServer sets fasthttp.Server instance to use
    38  func OptUseServer(s *fasthttp.Server) func(*App) {
    39  	return func(app *App) {
    40  		assertAppNotNill(app)
    41  		if s == nil {
    42  			panic(errors.New("cannot set nil as app server instance"))
    43  		}
    44  		app.serverBase = s
    45  		app.serverBase.Handler = app.handler()
    46  	}
    47  }
    48  
    49  // OptMaxRequestBodySize sets new MaxRequestBodySize in the server used at the execution time.
    50  // All OptUseServer will overwrite this setting 'case OptUseServer replaces the whole server instance
    51  // with a new one.
    52  func OptMaxRequestBodySize(size int) func(*App) {
    53  	return func(app *App) {
    54  		assertAppNotNill(app)
    55  		if app.serverBase == nil {
    56  			app.serverBase = newDefaultServerBaseFor(app)
    57  		}
    58  		app.serverBase.MaxRequestBodySize = size
    59  	}
    60  }
    61  
    62  // OptKeepHijackedConns sets new KeepHijackedConns in the server used at the execution time.
    63  // All OptUseServer will overwrite this setting 'case OptUseServer replaces the whole server instance
    64  // with a new one.
    65  func OptKeepHijackedConns(keep bool) func(*App) {
    66  	return func(app *App) {
    67  		assertAppNotNill(app)
    68  		if app.serverBase == nil {
    69  			app.serverBase = newDefaultServerBaseFor(app)
    70  		}
    71  		app.serverBase.KeepHijackedConns = keep
    72  	}
    73  }
    74  
    75  func assertAppNotNill(app *App) {
    76  	if app == nil {
    77  		panic(errors.New("option can be implemented only to already creaded app object not to nil"))
    78  	}
    79  }