github.com/kafkaliu/etcd@v0.1.2-0.20131007164923-44c16dd30d69/etcd_server.go (about)

     1  /*
     2  Copyright 2013 CoreOS Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"net/http"
    21  )
    22  
    23  type etcdServer struct {
    24  	http.Server
    25  	name    string
    26  	url     string
    27  	tlsConf *TLSConfig
    28  	tlsInfo *TLSInfo
    29  }
    30  
    31  var e *etcdServer
    32  
    33  func newEtcdServer(name string, urlStr string, listenHost string, tlsConf *TLSConfig, tlsInfo *TLSInfo) *etcdServer {
    34  	return &etcdServer{
    35  		Server: http.Server{
    36  			Handler:   NewEtcdMuxer(),
    37  			TLSConfig: &tlsConf.Server,
    38  			Addr:      listenHost,
    39  		},
    40  		name:    name,
    41  		url:     urlStr,
    42  		tlsConf: tlsConf,
    43  		tlsInfo: tlsInfo,
    44  	}
    45  }
    46  
    47  // Start to listen and response etcd client command
    48  func (e *etcdServer) ListenAndServe() {
    49  
    50  	infof("etcd server [name %s, listen on %s, advertised url %s]", e.name, e.Server.Addr, e.url)
    51  
    52  	if e.tlsConf.Scheme == "http" {
    53  		fatal(e.Server.ListenAndServe())
    54  	} else {
    55  		fatal(e.Server.ListenAndServeTLS(e.tlsInfo.CertFile, e.tlsInfo.KeyFile))
    56  	}
    57  }