get.pme.sh/pnats@v0.0.0-20240304004023-26bb5a137ed0/main.go (about)

     1  // Copyright 2012-2022 The NATS Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  //go:generate go run server/errors_gen.go
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"os"
    22  
    23  	"get.pme.sh/pnats/server"
    24  	"go.uber.org/automaxprocs/maxprocs"
    25  )
    26  
    27  var usageStr = `
    28  Usage: nats-server [options]
    29  
    30  Server Options:
    31      -a, --addr, --net <host>         Bind to host address (default: 0.0.0.0)
    32      -p, --port <port>                Use port for clients (default: 4222)
    33      -n, --name
    34          --server_name <server_name>  Server name (default: auto)
    35      -P, --pid <file>                 File to store PID
    36      -m, --http_port <port>           Use port for http monitoring
    37      -ms,--https_port <port>          Use port for https monitoring
    38      -c, --config <file>              Configuration file
    39      -t                               Test configuration and exit
    40      -sl,--signal <signal>[=<pid>]    Send signal to nats-server process (ldm, stop, quit, term, reopen, reload)
    41                                       pid> can be either a PID (e.g. 1) or the path to a PID file (e.g. /var/run/nats-server.pid)
    42          --client_advertise <string>  Client URL to advertise to other servers
    43          --ports_file_dir <dir>       Creates a ports file in the specified directory (<executable_name>_<pid>.ports).
    44  
    45  Logging Options:
    46      -l, --log <file>                 File to redirect log output
    47      -T, --logtime                    Timestamp log entries (default: true)
    48      -s, --syslog                     Log to syslog or windows event log
    49      -r, --remote_syslog <addr>       Syslog server addr (udp://localhost:514)
    50      -D, --debug                      Enable debugging output
    51      -V, --trace                      Trace the raw protocol
    52      -VV                              Verbose trace (traces system account as well)
    53      -DV                              Debug and trace
    54      -DVV                             Debug and verbose trace (traces system account as well)
    55          --log_size_limit <limit>     Logfile size limit (default: auto)
    56          --max_traced_msg_len <len>   Maximum printable length for traced messages (default: unlimited)
    57  
    58  JetStream Options:
    59      -js, --jetstream                 Enable JetStream functionality
    60      -sd, --store_dir <dir>           Set the storage directory
    61  
    62  Authorization Options:
    63          --user <user>                User required for connections
    64          --pass <password>            Password required for connections
    65          --auth <token>               Authorization token required for connections
    66  
    67  TLS Options:
    68          --tls                        Enable TLS, do not verify clients (default: false)
    69          --tlscert <file>             Server certificate file
    70          --tlskey <file>              Private key for server certificate
    71          --tlsverify                  Enable TLS, verify client certificates
    72          --tlscacert <file>           Client certificate CA for verification
    73  
    74  Cluster Options:
    75          --routes <rurl-1, rurl-2>    Routes to solicit and connect
    76          --cluster <cluster-url>      Cluster URL for solicited routes
    77          --cluster_name <string>      Cluster Name, if not set one will be dynamically generated
    78          --no_advertise <bool>        Do not advertise known cluster information to clients
    79          --cluster_advertise <string> Cluster URL to advertise to other servers
    80          --connect_retries <number>   For implicit routes, number of connect retries
    81          --cluster_listen <url>       Cluster url from which members can solicit routes
    82  
    83  Profiling Options:
    84          --profile <port>             Profiling HTTP port
    85  
    86  Common Options:
    87      -h, --help                       Show this message
    88      -v, --version                    Show version
    89          --help_tls                   TLS help
    90  `
    91  
    92  // usage will print out the flag options for the server.
    93  func usage() {
    94  	fmt.Printf("%s\n", usageStr)
    95  	os.Exit(0)
    96  }
    97  
    98  func main() {
    99  	exe := "nats-server"
   100  
   101  	// Create a FlagSet and sets the usage
   102  	fs := flag.NewFlagSet(exe, flag.ExitOnError)
   103  	fs.Usage = usage
   104  
   105  	// Configure the options from the flags/config file
   106  	opts, err := server.ConfigureOptions(fs, os.Args[1:],
   107  		server.PrintServerAndExit,
   108  		fs.Usage,
   109  		server.PrintTLSHelpAndDie)
   110  	if err != nil {
   111  		server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
   112  	} else if opts.CheckConfig {
   113  		fmt.Fprintf(os.Stderr, "%s: configuration file %s is valid\n", exe, opts.ConfigFile)
   114  		os.Exit(0)
   115  	}
   116  
   117  	// Create the server with appropriate options.
   118  	s, err := server.NewServer(opts)
   119  	if err != nil {
   120  		server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
   121  	}
   122  
   123  	// Configure the logger based on the flags
   124  	s.ConfigureLogger()
   125  
   126  	// Start things up. Block here until done.
   127  	if err := server.Run(s); err != nil {
   128  		server.PrintAndDie(err.Error())
   129  	}
   130  
   131  	// Adjust MAXPROCS if running under linux/cgroups quotas.
   132  	undo, err := maxprocs.Set(maxprocs.Logger(s.Debugf))
   133  	if err != nil {
   134  		s.Warnf("Failed to set GOMAXPROCS: %v", err)
   135  	} else {
   136  		defer undo()
   137  	}
   138  
   139  	s.WaitForShutdown()
   140  }