github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/http/service-service-mux.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package http
     4  
     5  import (
     6  	"../mediatype"
     7  	"../protocol"
     8  	"../service"
     9  )
    10  
    11  const serviceMuxPath = "/m"
    12  
    13  var MuxService = muxService{
    14  	Service: service.New("", mediatype.New("domain/http.protocol.service; name=service-multiplexer").SetDetail(protocol.LanguageEnglish, domainEnglish,
    15  		"Service Multiplexer",
    16  		"Multiplex services by its ID with impressive performance",
    17  		"",
    18  		"",
    19  		nil).SetInfo(protocol.Software_PreAlpha, 1587282740, "")).
    20  		SetAuthorization(protocol.CRUDAll, protocol.UserType_All),
    21  }
    22  
    23  type muxService struct {
    24  	*service.Service
    25  }
    26  
    27  func (ser *muxService) ServeHTTP(st protocol.Stream, httpReq *Request, httpRes *Response) (err protocol.Error) {
    28  	if httpReq.method != MethodPOST {
    29  		// err =
    30  		httpRes.SetStatus(StatusMethodNotAllowedCode, StatusMethodNotAllowedPhrase)
    31  		return
    32  	}
    33  
    34  	var serviceID uint64
    35  	var service protocol.Service
    36  	serviceID, err = mediatype.IDfromString(httpReq.uri.query)
    37  	if err == nil {
    38  		service, err = protocol.App.GetServiceByID(serviceID)
    39  		if err != nil {
    40  			httpRes.SetStatus(StatusNotFoundCode, StatusNotFoundPhrase)
    41  			httpRes.SetError(ErrNotFound)
    42  			// err = ErrNotFound
    43  			return
    44  		}
    45  	} else {
    46  		httpRes.SetStatus(StatusBadRequestCode, StatusBadRequestPhrase)
    47  		httpRes.SetError(err)
    48  		return
    49  	}
    50  
    51  	// Add some header for dynamically services like not index by SE(google, ...), ...
    52  	httpRes.H.Set("X-Robots-Tag", "noindex")
    53  	// httpRes.H.Set(HeaderKeyCacheControl, "no-store")
    54  
    55  	st.SetService(service)
    56  	// TODO::: can't easily call service and must schedule it by its weight.
    57  	err = service.ServeHTTP(st, httpReq, httpRes)
    58  	if err != nil {
    59  		httpRes.SetError(err)
    60  	}
    61  	return
    62  }
    63  
    64  func (ser *muxService) DoHTTP(httpReq *Request, httpRes *Response) (err protocol.Error) {
    65  	return
    66  }