github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/new.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  	"sync"
    15  	"time"
    16  
    17  	"github.com/apex/log"
    18  	"github.com/microcosm-cc/bluemonday"
    19  	"github.com/valyala/fasthttp"
    20  )
    21  
    22  var defaultMaxHackAttempts int32 = 5
    23  
    24  // New App
    25  func New(opts ...func(*App)) *App {
    26  	logger := Logger
    27  	internalLog = func() *log.Entry {
    28  		glogger := Logger.WithField("package", "gramework")
    29  		glogger.Level = log.DebugLevel
    30  		if !enableDebug {
    31  			glogger.Level = log.InfoLevel
    32  		}
    33  
    34  		return glogger
    35  	}()
    36  	flags := &Flags{
    37  		values: make(map[string]Flag),
    38  	}
    39  	defFWLimit := int64(-1)
    40  	defBlockTimeout := int64(-1)
    41  	maxHackAttempts := defaultMaxHackAttempts
    42  	app := &App{
    43  		Flags:          flags,
    44  		flagsQueue:     flagsToRegister,
    45  		Logger:         logger,
    46  		name:           DefaultAppName,
    47  		domainListLock: new(sync.RWMutex),
    48  		firewall: &firewall{
    49  			blockList:      make(map[string]int64),
    50  			MaxReqPerMin:   &defFWLimit,
    51  			BlockTimeout:   &defBlockTimeout,
    52  			requestCounter: make(map[string]int64),
    53  		},
    54  		firewallInit:              new(sync.Once),
    55  		domains:                   make(map[string]*Router),
    56  		middlewaresMu:             new(sync.RWMutex),
    57  		middlewaresAfterRequestMu: new(sync.RWMutex),
    58  		preMiddlewaresMu:          new(sync.RWMutex),
    59  		middlewares:               make([]func(*Context), 0),
    60  		middlewaresAfterRequest:   make([]func(*Context), 0),
    61  		preMiddlewares:            make([]func(*Context), 0),
    62  		seed:                      uintptr(time.Now().Nanosecond()),
    63  		maxHackAttempts:           &maxHackAttempts,
    64  		runningServersMu:          new(sync.Mutex),
    65  		internalLog:               internalLog,
    66  		cookieExpire:              6 * time.Hour,
    67  		cookiePath:                defaultCookiePath,
    68  
    69  		sanitizerPolicy: bluemonday.StrictPolicy(),
    70  	}
    71  
    72  	for _, opt := range opts {
    73  		opt(app)
    74  	}
    75  
    76  	if app.serverBase == nil {
    77  		app.serverBase = newDefaultServerBaseFor(app)
    78  	}
    79  	// avoid race condition then OptUseServer becomes before OptAppName
    80  	// or OptUseCustomLogger becomes before OptUseServer
    81  	app.serverBase.Name = app.name
    82  	app.serverBase.Logger = NewFastHTTPLoggerAdapter(&app.Logger)
    83  
    84  	app.defaultRouter = &Router{
    85  		router: newRouter(),
    86  		app:    app,
    87  	}
    88  
    89  	return app
    90  }
    91  
    92  // Common code used with `gramework.New()` itself and `Opt*` functions.
    93  func newDefaultServerBaseFor(app *App) *fasthttp.Server {
    94  	return &fasthttp.Server{
    95  		Handler: app.handler(),
    96  		Logger:  NewFastHTTPLoggerAdapter(&app.Logger),
    97  	}
    98  }