github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/http/services/service-mux.go (about) 1 /* For license and copyright information please see the LEGAL file in the code repository */ 2 3 package hs 4 5 import ( 6 "github.com/GeniusesGroup/libgo/detail" 7 "github.com/GeniusesGroup/libgo/http" 8 "github.com/GeniusesGroup/libgo/mediatype" 9 "github.com/GeniusesGroup/libgo/protocol" 10 "github.com/GeniusesGroup/libgo/service" 11 uuid "github.com/GeniusesGroup/libgo/uuid/32byte" 12 ) 13 14 const MuxService_Path = "/m" 15 16 var MuxService = muxService{} 17 18 func init() { 19 MuxService.MT.Init("domain/http.protocol.service; name=service-multiplexer") 20 MuxService.DS.SetDetail(protocol.LanguageEnglish, domainEnglish, 21 "Service Multiplexer", 22 "Multiplex services by its ID with impressive performance", 23 "", 24 "", 25 nil) 26 } 27 28 type muxService struct { 29 detail.DS 30 mediatype.MT 31 service.Service 32 } 33 34 //libgo:impl protocol.MediaType 35 func (s *muxService) FileExtension() string { return "" } 36 func (s *muxService) Status() protocol.SoftwareStatus { return protocol.Software_PreAlpha } 37 func (s *muxService) ReferenceURI() string { return "" } 38 func (s *muxService) IssueDate() protocol.Time { return nil } // 1587282740 39 func (s *muxService) ExpiryDate() protocol.Time { return nil } 40 func (s *muxService) ExpireInFavorOf() protocol.MediaType { return nil } 41 func (s *muxService) Fields() []protocol.Field { return nil } 42 43 //libgo:impl protocol.Service 44 func (s *muxService) URI() string { return MuxService_Path } 45 func (s *muxService) Priority() protocol.Priority { return protocol.Priority_Unset } 46 func (s *muxService) Weight() protocol.Weight { return protocol.Weight_Unset } 47 func (s *muxService) CRUDType() protocol.CRUD { return protocol.CRUD_All } 48 func (s *muxService) UserType() protocol.UserType { return protocol.UserType_All } 49 50 func (ser *muxService) ServeHTTP(st protocol.Stream, httpReq *http.Request, httpRes *http.Response) (err protocol.Error) { 51 if httpReq.Method() != http.MethodPOST { 52 // err = 53 httpRes.SetStatus(http.StatusMethodNotAllowedCode, http.StatusMethodNotAllowedPhrase) 54 return 55 } 56 57 var serviceID uint64 58 var service protocol.Service 59 serviceID, err = uuid.IDfromString(httpReq.URI().Query()) 60 if err == nil { 61 service, err = protocol.App.GetServiceByID(protocol.ID(serviceID)) 62 if err != nil { 63 httpRes.SetStatus(http.StatusNotFoundCode, http.StatusNotFoundPhrase) 64 httpRes.SetError(&ErrNotFound) 65 // err = &ErrNotFound 66 return 67 } 68 } else { 69 httpRes.SetStatus(http.StatusBadRequestCode, http.StatusBadRequestPhrase) 70 httpRes.SetError(err) 71 return 72 } 73 74 // Add some header for dynamically services like not index by SE(google, ...), ... 75 httpRes.H.Set("X-Robots-Tag", "noindex") 76 // httpRes.H.Set(HeaderKeyCacheControl, "no-store") 77 78 st.SetService(service) 79 // TODO::: can't easily call service and must schedule it by its weight. 80 err = service.ServeHTTP(st, httpReq, httpRes) 81 if err != nil { 82 httpRes.SetError(err) 83 } 84 return 85 } 86 87 func (ser *muxService) doHTTP(httpReq *http.Request, httpRes *http.Response) (err protocol.Error) { 88 return 89 }