github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/server/http/options.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     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  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package http
    18  
    19  import (
    20  	"crypto/tls"
    21  	"github.com/lastbackend/toolkit/pkg/server"
    22  	"net/http"
    23  )
    24  
    25  const (
    26  	defaultAddress                                          = ":8080"
    27  	optionKindMiddleware              server.HttpOptionKind = "middleware"
    28  	optionKindExcludeGlobalMiddleware server.HttpOptionKind = "excludeGlobalMiddleware"
    29  )
    30  
    31  const (
    32  	MethodGet     = "GET"
    33  	MethodHead    = "HEAD"
    34  	MethodPost    = "POST"
    35  	MethodPut     = "PUT"
    36  	MethodPatch   = "PATCH" // RFC 5789
    37  	MethodDelete  = "DELETE"
    38  	MethodConnect = "CONNECT"
    39  	MethodOptions = "OPTIONS"
    40  	MethodTrace   = "TRACE"
    41  )
    42  
    43  type optionMiddleware struct {
    44  	kind       server.HttpOptionKind
    45  	middleware server.KindMiddleware
    46  }
    47  
    48  func (optionMiddleware) Kind() server.HttpOptionKind {
    49  	return optionKindMiddleware
    50  }
    51  
    52  func WithMiddleware(middleware server.KindMiddleware) server.HTTPServerOption {
    53  	return &optionMiddleware{kind: optionKindMiddleware, middleware: middleware}
    54  }
    55  
    56  type optionExcludeGlobalMiddleware struct {
    57  	kind   server.HttpOptionKind
    58  	regexp string
    59  }
    60  
    61  func (optionExcludeGlobalMiddleware) Kind() server.HttpOptionKind {
    62  	return optionKindExcludeGlobalMiddleware
    63  }
    64  
    65  func WithExcludeGlobalMiddleware(regexp string) server.HTTPServerOption {
    66  	return &optionExcludeGlobalMiddleware{kind: optionKindMiddleware, regexp: regexp}
    67  }
    68  
    69  type Config struct {
    70  	Id string
    71  
    72  	Host string `env:"SERVER_LISTEN" envDefault:"0.0.0.0" comment:"Set HTTP server listen host"`
    73  	Port int    `env:"SERVER_PORT" envDefault:"8080" comment:"Set HTTP server listen port"`
    74  
    75  	Prefix string
    76  
    77  	EnableCORS bool `env:"SERVER_CORS_ENABLED" envDefault:"false" comment:"Enable Cross-Origin Resource Sharing header"`
    78  	IsDisable  bool
    79  
    80  	TLSConfig *tls.Config
    81  }
    82  
    83  type Wrapper func(h http.Handler) http.Handler