...

Source file src/github.com/sassoftware/relic/cmdline/servecmd/servecmd.go

Documentation: github.com/sassoftware/relic/cmdline/servecmd

     1  //
     2  // Copyright (c) SAS Institute 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 servecmd
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"log"
    23  	"net"
    24  	"net/http"
    25  
    26  	"github.com/sassoftware/relic/cmdline/shared"
    27  	"github.com/sassoftware/relic/server/daemon"
    28  	"github.com/spf13/cobra"
    29  
    30  	_ "net/http/pprof"
    31  )
    32  
    33  var ServeCmd = &cobra.Command{
    34  	Use:   "serve",
    35  	Short: "Offer signing services over a HTTPS API",
    36  	RunE:  serveCmd,
    37  }
    38  
    39  var argTest bool
    40  
    41  func init() {
    42  	shared.RootCmd.AddCommand(ServeCmd)
    43  	ServeCmd.Flags().BoolP("force", "f", false, "(ignored)")
    44  	ServeCmd.Flags().BoolVarP(&argTest, "test", "t", false, "Test configuration and exit")
    45  }
    46  
    47  func MakeServer() (*daemon.Daemon, error) {
    48  	if err := shared.InitConfig(); err != nil {
    49  		return nil, err
    50  	}
    51  	if shared.CurrentConfig.Server == nil {
    52  		return nil, errors.New("Missing server section in configuration file")
    53  	}
    54  	if shared.CurrentConfig.Server.KeyFile == "" {
    55  		return nil, errors.New("Missing keyfile option in server configuration file")
    56  	}
    57  	if shared.CurrentConfig.Server.CertFile == "" {
    58  		return nil, errors.New("Missing certfile option in server configuration file")
    59  	}
    60  	if shared.CurrentConfig.Clients == nil {
    61  		return nil, errors.New("Missing clients section in configuration file")
    62  	}
    63  	if shared.CurrentConfig.Server.Listen == "" {
    64  		shared.CurrentConfig.Server.Listen = ":6300"
    65  	}
    66  	return daemon.New(shared.CurrentConfig, argTest)
    67  }
    68  
    69  func listenDebug() error {
    70  	if !shared.CurrentConfig.Server.ListenDebug {
    71  		return nil
    72  	}
    73  	lis, err := net.Listen("tcp", ":0")
    74  	if err != nil {
    75  		return err
    76  	}
    77  	fmt.Printf("Serving debug info on http://%s/debug/pprof/\n", lis.Addr())
    78  	go func() {
    79  		// pprof installs itself into the default handler on import
    80  		err := http.Serve(lis, nil)
    81  		if err != nil {
    82  			fmt.Println(err)
    83  		}
    84  	}()
    85  	return nil
    86  }
    87  
    88  func serveCmd(cmd *cobra.Command, args []string) error {
    89  	// let journald add timestamps
    90  	log.SetFlags(0)
    91  	srv, err := MakeServer()
    92  	if err != nil {
    93  		return shared.Fail(err)
    94  	} else if argTest {
    95  		fmt.Println("OK")
    96  		return nil
    97  	}
    98  	go watchSignals(srv)
    99  	if err := listenDebug(); err != nil {
   100  		return err
   101  	}
   102  	if err := srv.Serve(); err != nil && err != http.ErrServerClosed {
   103  		return shared.Fail(err)
   104  	}
   105  	return nil
   106  }
   107  

View as plain text