|
| 1 | +// Copyright 2022 Douyu |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package xfasthttp |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/douyu/jupiter/pkg/conf" |
| 21 | + "github.com/douyu/jupiter/pkg/constant" |
| 22 | + "github.com/douyu/jupiter/pkg/ecode" |
| 23 | + "github.com/douyu/jupiter/pkg/flag" |
| 24 | + "github.com/douyu/jupiter/pkg/xlog" |
| 25 | + "github.com/pkg/errors" |
| 26 | +) |
| 27 | + |
| 28 | +//ModName named a mod |
| 29 | +const ModName = "server.fasthttp" |
| 30 | + |
| 31 | +//Config HTTP config |
| 32 | +type Config struct { |
| 33 | + Host string |
| 34 | + Port int |
| 35 | + Deployment string |
| 36 | + Debug bool |
| 37 | + DisableMetric bool |
| 38 | + DisableTrace bool |
| 39 | + DisablePrintStack bool |
| 40 | + // ServiceAddress service address in registry info, default to 'Host:Port' |
| 41 | + ServiceAddress string |
| 42 | + CertFile string |
| 43 | + PrivateFile string |
| 44 | + EnableTLS bool |
| 45 | + |
| 46 | + SlowQueryThresholdInMilli int64 |
| 47 | + ReadBufferSize int |
| 48 | + WriteBufferSize int |
| 49 | + ReduceMemoryUsage bool |
| 50 | + |
| 51 | + logger *xlog.Logger |
| 52 | +} |
| 53 | + |
| 54 | +// DefaultConfig ... |
| 55 | +func DefaultConfig() *Config { |
| 56 | + return &Config{ |
| 57 | + Host: flag.String("host"), |
| 58 | + Port: 9091, |
| 59 | + Debug: false, |
| 60 | + Deployment: constant.DefaultDeployment, |
| 61 | + SlowQueryThresholdInMilli: 500, // 500ms |
| 62 | + ReadBufferSize: 1024, // 1KB |
| 63 | + WriteBufferSize: 1024, // 1KB |
| 64 | + ReduceMemoryUsage: true, |
| 65 | + logger: xlog.JupiterLogger.With(xlog.FieldMod(ModName)), |
| 66 | + DisablePrintStack: false, |
| 67 | + EnableTLS: false, |
| 68 | + CertFile: "cert.pem", |
| 69 | + PrivateFile: "private.pem", |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// StdConfig Jupiter Standard HTTP Server config |
| 74 | +func StdConfig(name string) *Config { |
| 75 | + return RawConfig("jupiter.server." + name) |
| 76 | +} |
| 77 | + |
| 78 | +// RawConfig ... |
| 79 | +func RawConfig(key string) *Config { |
| 80 | + var config = DefaultConfig() |
| 81 | + if err := conf.UnmarshalKey(key, &config); err != nil && |
| 82 | + errors.Cause(err) != conf.ErrInvalidKey { |
| 83 | + config.logger.Panic("http server parse config panic", xlog.FieldErrKind(ecode.ErrKindUnmarshalConfigErr), xlog.FieldErr(err), xlog.FieldKey(key), xlog.FieldValueAny(config)) |
| 84 | + } |
| 85 | + return config |
| 86 | +} |
| 87 | + |
| 88 | +// WithLogger ... |
| 89 | +func (config *Config) WithLogger(logger *xlog.Logger) *Config { |
| 90 | + config.logger = logger |
| 91 | + return config |
| 92 | +} |
| 93 | + |
| 94 | +// WithHost ... |
| 95 | +func (config *Config) WithHost(host string) *Config { |
| 96 | + config.Host = host |
| 97 | + return config |
| 98 | +} |
| 99 | + |
| 100 | +// WithPort ... |
| 101 | +func (config *Config) WithPort(port int) *Config { |
| 102 | + config.Port = port |
| 103 | + return config |
| 104 | +} |
| 105 | + |
| 106 | +func (config *Config) MustBuild() *Server { |
| 107 | + server, err := config.Build() |
| 108 | + if err != nil { |
| 109 | + xlog.Panicf("build echo server failed: %v", err) |
| 110 | + } |
| 111 | + return server |
| 112 | +} |
| 113 | + |
| 114 | +// Build create server instance, then initialize it with necessary interceptor |
| 115 | +func (config *Config) Build() (*Server, error) { |
| 116 | + server, err := newServer(config) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + return server, nil |
| 122 | +} |
| 123 | + |
| 124 | +// Address ... |
| 125 | +func (config *Config) Address() string { |
| 126 | + return fmt.Sprintf("%s:%d", config.Host, config.Port) |
| 127 | +} |
0 commit comments