github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/http/services/serve-www.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  	"github.com/GeniusesGroup/libgo/www"
    12  )
    13  
    14  var ServeWWWService = serveWWWService{}
    15  
    16  func init() {
    17  	ServeWWWService.MT.Init("domain/http.protocol.service; name=serve-www")
    18  	ServeWWWService.DS.SetDetail(protocol.LanguageEnglish, domainEnglish,
    19  		"Serve WWW",
    20  		"",
    21  		"",
    22  		"",
    23  		nil)
    24  }
    25  
    26  type serveWWWService struct {
    27  	detail.DS
    28  	mediatype.MT
    29  	service.Service
    30  	WWW www.Assets
    31  }
    32  
    33  //libgo:impl protocol.MediaType
    34  func (s *serveWWWService) FileExtension() string               { return "" }
    35  func (s *serveWWWService) Status() protocol.SoftwareStatus     { return protocol.Software_PreAlpha }
    36  func (s *serveWWWService) ReferenceURI() string                { return "" }
    37  func (s *serveWWWService) IssueDate() protocol.Time            { return nil } // 1587282740
    38  func (s *serveWWWService) ExpiryDate() protocol.Time           { return nil }
    39  func (s *serveWWWService) ExpireInFavorOf() protocol.MediaType { return nil }
    40  func (s *serveWWWService) Fields() []protocol.Field            { return nil }
    41  
    42  //libgo:impl protocol.Service
    43  func (s *serveWWWService) URI() string                 { return "" }
    44  func (s *serveWWWService) Priority() protocol.Priority { return protocol.Priority_Unset }
    45  func (s *serveWWWService) Weight() protocol.Weight     { return protocol.Weight_Unset }
    46  func (s *serveWWWService) CRUDType() protocol.CRUD     { return protocol.CRUD_All }
    47  func (s *serveWWWService) UserType() protocol.UserType { return protocol.UserType_All }
    48  
    49  // ServeWWW will serve WWW assets to request
    50  func (s *serveWWWService) ServeHTTP(stream protocol.Stream, httpReq *http.Request, httpRes *http.Response) (err protocol.Error) {
    51  	var reqFile, _ = s.WWW.GUI.FileByPath(httpReq.URI().Path())
    52  	if reqFile == nil {
    53  		// TODO::: SSR to serve-to-robots
    54  		// TODO::: Have default error pages and can get customizes!
    55  		// Send beauty HTML response in http error situation like 500, 404, ...
    56  
    57  		const supportedLang = "en" // TODO::: get from header
    58  		reqFile, err = s.WWW.MainHTMLDir.File(supportedLang)
    59  		// if err != nil {
    60  		// TODO::: check other user language and at the end send better error
    61  		// }
    62  	}
    63  	httpRes.SetStatus(http.StatusOKCode, http.StatusOKPhrase)
    64  	httpRes.H.Set(http.HeaderKeyCacheControl, "max-age=31536000, immutable")
    65  	httpRes.SetBody(reqFile.Data())
    66  	return
    67  }