github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/pkg/server/http/handler.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 "context" 21 "encoding/json" 22 "net/http" 23 "strings" 24 25 "github.com/lastbackend/toolkit/pkg/context/metadata" 26 "github.com/lastbackend/toolkit/pkg/server" 27 grpc_md "google.golang.org/grpc/metadata" 28 ) 29 30 type HandleOption func(*HandleOptions) 31 32 type HandleOptions struct { 33 Middlewares server.HttpServerMiddleware 34 } 35 36 type Handler func(w http.ResponseWriter, r *http.Request) error 37 38 func corsHandlerFunc(w http.ResponseWriter, r *http.Request) { 39 w.Header().Add("Access-Control-Allow-Origin", r.Header.Get("Origin")) 40 w.Header().Add("Access-Control-Allow-Credentials", "true") 41 w.Header().Add("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,DELETE") 42 w.Header().Add("Access-Control-Expose-Headers", "Content-Disposition") 43 w.Header().Add("Access-Control-Allow-Headers", "Authorization,Content-Type,Origin,X-Tools-Name,X-Requested-With,Content-Name,Accept,Accept-Range,Range") 44 w.Header().Add("Content-Type", "application/json") 45 } 46 47 func (s *httpServer) methodNotAllowedHandler() http.Handler { 48 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 49 if s.opts.EnableCORS { 50 corsHandlerFunc(w, r) 51 } 52 w.WriteHeader(http.StatusMethodNotAllowed) 53 w.Write(makeResponse(http.StatusMethodNotAllowed, "Method Not Allowed")) 54 }) 55 } 56 57 func (s *httpServer) methodNotFoundHandler() http.Handler { 58 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 59 if s.opts.EnableCORS { 60 corsHandlerFunc(w, r) 61 } 62 w.WriteHeader(http.StatusNotFound) 63 w.Write(makeResponse(http.StatusNotFound, "Not Found")) 64 }) 65 } 66 67 type response struct { 68 Code int `json:"code"` 69 Status string `json:"status"` 70 Message string `json:"message"` 71 } 72 73 func makeResponse(code int, message string) []byte { 74 r, _ := json.Marshal(response{ 75 Code: code, 76 Status: http.StatusText(code), 77 Message: message, 78 }) 79 return r 80 } 81 82 func NewIncomingContext(ctx context.Context, headers map[string]string) context.Context { 83 if headers == nil { 84 headers = make(map[string]string) 85 } 86 if md, ok := metadata.LoadFromContext(ctx); ok { 87 for k, v := range md { 88 headers[strings.ToLower(k)] = v 89 } 90 return grpc_md.NewIncomingContext(ctx, grpc_md.New(md)) 91 } 92 return grpc_md.NewIncomingContext(ctx, grpc_md.New(headers)) 93 }