github.com/google/cloudprober@v0.11.3/servers/servers.go (about)

     1  // Copyright 2017 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  /*
    16  Package servers provides an interface to initialize cloudprober servers using servers config.
    17  */
    18  package servers
    19  
    20  import (
    21  	"context"
    22  	"html/template"
    23  
    24  	"github.com/google/cloudprober/logger"
    25  	"github.com/google/cloudprober/metrics"
    26  	"github.com/google/cloudprober/servers/external"
    27  	"github.com/google/cloudprober/servers/grpc"
    28  	"github.com/google/cloudprober/servers/http"
    29  	configpb "github.com/google/cloudprober/servers/proto"
    30  	"github.com/google/cloudprober/servers/udp"
    31  	"github.com/google/cloudprober/web/formatutils"
    32  )
    33  
    34  // StatusTmpl variable stores the HTML template suitable to generate the
    35  // servers' status for cloudprober's /status page. It expects an array of
    36  // ServerInfo objects as input.
    37  var StatusTmpl = template.Must(template.New("statusTmpl").Parse(`
    38  <table class="status-list">
    39    <tr>
    40      <th>Type</th>
    41      <th>Conf</th>
    42    </tr>
    43    {{ range . }}
    44    <tr>
    45      <td>{{.Type}}</td>
    46      <td>
    47      {{if .Conf}}
    48        <pre>{{.Conf}}</pre>
    49      {{else}}
    50        default
    51      {{end}}
    52      </td>
    53    </tr>
    54    {{ end }}
    55  </table>
    56  `))
    57  
    58  // Server interface has only one method: Start.
    59  type Server interface {
    60  	Start(ctx context.Context, dataChan chan<- *metrics.EventMetrics) error
    61  }
    62  
    63  // ServerInfo encapsulates a Server and related info.
    64  type ServerInfo struct {
    65  	Server
    66  	Type string
    67  	Conf string
    68  }
    69  
    70  // Init initializes cloudprober servers, based on the provided config.
    71  func Init(initCtx context.Context, serverDefs []*configpb.ServerDef) (servers []*ServerInfo, err error) {
    72  	for _, serverDef := range serverDefs {
    73  		var l *logger.Logger
    74  		l, err = logger.NewCloudproberLog(serverDef.GetType().String())
    75  		if err != nil {
    76  			return
    77  		}
    78  
    79  		var conf interface{}
    80  		var server Server
    81  
    82  		switch serverDef.GetType() {
    83  		case configpb.ServerDef_HTTP:
    84  			server, err = http.New(initCtx, serverDef.GetHttpServer(), l)
    85  			conf = serverDef.GetHttpServer()
    86  		case configpb.ServerDef_UDP:
    87  			server, err = udp.New(initCtx, serverDef.GetUdpServer(), l)
    88  			conf = serverDef.GetUdpServer()
    89  		case configpb.ServerDef_GRPC:
    90  			server, err = grpc.New(initCtx, serverDef.GetGrpcServer(), l)
    91  			conf = serverDef.GetGrpcServer()
    92  		case configpb.ServerDef_EXTERNAL:
    93  			server, err = external.New(initCtx, serverDef.GetExternalServer(), l)
    94  			conf = serverDef.GetExternalServer()
    95  		}
    96  		if err != nil {
    97  			return
    98  		}
    99  
   100  		servers = append(servers, &ServerInfo{
   101  			Server: server,
   102  			Type:   serverDef.GetType().String(),
   103  			Conf:   formatutils.ConfToString(conf),
   104  		})
   105  	}
   106  	return
   107  }