github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/ihttpctl/impl.go (about)

     1  /*
     2  * Copyright (c) 2022-present unTill Pro, Ltd.
     3  * @author Maxim Geraskin
     4   */
     5  
     6  package ihttpctl
     7  
     8  import (
     9  	"context"
    10  	"io/fs"
    11  
    12  	"github.com/voedger/voedger/pkg/goutils/logger"
    13  
    14  	"github.com/voedger/voedger/pkg/ihttp"
    15  )
    16  
    17  type httpProcessorController struct {
    18  	processor          ihttp.IHTTPProcessor
    19  	staticResources    map[string]fs.FS
    20  	redirections       RedirectRoutes
    21  	defaultRedirection DefaultRedirectRoute
    22  	apps               AppRequestHandlers
    23  }
    24  
    25  func (hc *httpProcessorController) Prepare() (err error) {
    26  	return nil
    27  }
    28  
    29  func (hc *httpProcessorController) Run(ctx context.Context) {
    30  	for path, fs := range hc.staticResources {
    31  		hc.processor.DeployStaticContent(path, fs)
    32  		logger.Info(path, "deployed")
    33  	}
    34  	for src, dst := range hc.redirections {
    35  		hc.processor.AddReverseProxyRoute(src, dst)
    36  		logger.Info("redirection", src, arrow, dst, "added")
    37  	}
    38  	for src, dst := range hc.defaultRedirection {
    39  		hc.processor.SetReverseProxyRouteDefault(src, dst)
    40  		logger.Info("default redirection", src, arrow, dst, "added")
    41  	}
    42  
    43  	for _, appRequestHandler := range hc.apps {
    44  		if err := hc.processor.DeployApp(appRequestHandler.AppQName, appRequestHandler.NumPartitions, appRequestHandler.NumAppWS); err != nil {
    45  			panic(err)
    46  		}
    47  		for partNo, handler := range appRequestHandler.Handlers {
    48  			if err := hc.processor.DeployAppPartition(appRequestHandler.AppQName, partNo, handler); err != nil {
    49  				panic(err)
    50  			}
    51  		}
    52  	}
    53  
    54  	<-ctx.Done()
    55  
    56  	for _, appRequestHandler := range hc.apps {
    57  		for partNo := range appRequestHandler.Handlers {
    58  			if err := hc.processor.UndeployAppPartition(appRequestHandler.AppQName, partNo); err != nil {
    59  				panic(err)
    60  			}
    61  		}
    62  		if err := hc.processor.UndeployApp(appRequestHandler.AppQName); err != nil {
    63  			panic(err)
    64  		}
    65  	}
    66  }