github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/server/server.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 server 18 19 import ( 20 "context" 21 "crypto/tls" 22 "github.com/lastbackend/toolkit/pkg/server/http/marshaler" 23 "github.com/lastbackend/toolkit/pkg/server/http/websockets" 24 "google.golang.org/grpc" 25 "net/http" 26 ) 27 28 type HTTPServer interface { 29 Start(ctx context.Context) error 30 Stop(ctx context.Context) error 31 32 UseMiddleware(...KindMiddleware) 33 UseMarshaler(contentType string, marshaler marshaler.Marshaler) error 34 35 GetMiddlewares() []any 36 SetMiddleware(middleware any) 37 38 SetService(service interface{}) 39 GetService() interface{} 40 41 AddHandler(method, path string, h http.HandlerFunc, opts ...HTTPServerOption) 42 43 GetConstructor() interface{} 44 45 Subscribe(event string, h websockets.EventHandler) 46 Info() ServerInfo 47 48 ServerWS(w http.ResponseWriter, r *http.Request) 49 SetCorsHandlerFunc(hf http.HandlerFunc) 50 SetErrorHandlerFunc(hf func(http.ResponseWriter, error)) 51 } 52 53 type HTTPServerOptions struct { 54 Host string 55 Port int 56 57 TLSConfig *tls.Config 58 } 59 60 type ServerInfo struct { 61 Kind ServerKind 62 Host string 63 Port int 64 65 TLSConfig *tls.Config 66 } 67 68 type HTTPServerHandler struct { 69 Method string 70 Path string 71 Handler http.HandlerFunc 72 Options []HTTPServerOption 73 } 74 75 type HttpOptionKind string 76 77 type HTTPServerOption interface { 78 Kind() HttpOptionKind 79 } 80 81 type DefaultHttpServerMiddleware struct{} 82 83 func (DefaultHttpServerMiddleware) Order() int { 84 return 0 85 } 86 87 type HttpServerMiddleware interface { 88 Apply(h http.HandlerFunc) http.HandlerFunc 89 Kind() KindMiddleware 90 Order() int 91 } 92 93 type KindMiddleware string 94 type KindInterceptor string 95 96 type GRPCServer interface { 97 Start(ctx context.Context) error 98 Stop() error 99 100 SetDescriptor(descriptor grpc.ServiceDesc) 101 102 SetService(constructor interface{}) 103 SetConstructor(fn interface{}) 104 105 GetInterceptors() []interface{} 106 SetInterceptor(interceptor any) 107 108 GetService() interface{} 109 GetConstructor() interface{} 110 GetInterceptorsConstructor() interface{} 111 112 RegisterService(service interface{}) 113 Info() ServerInfo 114 } 115 116 type GRPCServerOptions struct { 117 Host string 118 Port int 119 120 TLSConfig *tls.Config 121 } 122 123 type ServerKind string 124 125 type GRPCInterceptor interface { 126 Kind() KindInterceptor 127 Order() int 128 Interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) 129 } 130 131 const ( 132 ServerKindHTTPServer = "http" 133 ServerKindGRPCServer = "grpc" 134 )