| 1 |
// Copyright 2012-2025 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 |
"github.com/nats-io/nats-server/v2/server" |
| 24 |
) |
| 25 |
|
| 26 |
var usageStr = ` |
| 27 |
Usage: nats-server [options] |
| 28 |
|
| 29 |
Server Options: |
| 30 |
-a, --addr, --net <host> Bind to host address (default: 0.0.0.0) |
| 31 |
-p, --port <port> Use port for clients (default: 4222) |
| 32 |
-n, --name |
| 33 |
--server_name <server_name> Server name (default: auto) |
| 34 |
-P, --pid <file> File to store PID |
| 35 |
-m, --http_port <port> Use port for http monitoring |
| 36 |
-ms,--https_port <port> Use port for https monitoring |
| 37 |
-c, --config <file> Configuration file |
| 38 |
-t Test configuration and exit |
| 39 |
-sl,--signal <signal>[=<pid>] Send signal to nats-server process (ldm, stop, quit, term, reopen, reload) |
| 40 |
<pid> can be either a PID (e.g. 1) or the path to a PID file (e.g. /var/run/nats-server.pid) |
| 41 |
--client_advertise <string> Client URL to advertise to other servers |
| 42 |
--ports_file_dir <dir> Creates a ports file in the specified directory (<executable_name>_<pid>.ports). |
| 43 |
|
| 44 |
Logging Options: |
| 45 |
-l, --log <file> File to redirect log output |
| 46 |
-T, --logtime Timestamp log entries (default: true) |
| 47 |
-s, --syslog Log to syslog or windows event log |
| 48 |
-r, --remote_syslog <addr> Syslog server addr (udp://localhost:514) |
| 49 |
-D, --debug Enable debugging output |
| 50 |
-V, --trace Trace the raw protocol |
| 51 |
-VV Verbose trace (traces system account as well) |
| 52 |
-DV Debug and trace |
| 53 |
-DVV Debug and verbose trace (traces system account as well) |
| 54 |
--log_size_limit <limit> Logfile size limit (default: auto) |
| 55 |
--max_traced_msg_len <len> Maximum printable length for traced messages (default: unlimited) |
| 56 |
|
| 57 |
JetStream Options: |
| 58 |
-js, --jetstream Enable JetStream functionality |
| 59 |
-sd, --store_dir <dir> Set the storage directory |
| 60 |
|
| 61 |
Authorization Options: |
| 62 |
--user <user> User required for connections |
| 63 |
--pass <password> Password required for connections |
| 64 |
--auth <token> Authorization token required for connections |
| 65 |
|
| 66 |
TLS Options: |
| 67 |
--tls Enable TLS, do not verify clients (default: false) |
| 68 |
--tlscert <file> Server certificate file |
| 69 |
--tlskey <file> Private key for server certificate |
| 70 |
--tlsverify Enable TLS, verify client certificates |
| 71 |
--tlscacert <file> Client certificate CA for verification |
| 72 |
|
| 73 |
Cluster Options: |
| 74 |
--routes <rurl-1, rurl-2> Routes to solicit and connect |
| 75 |
--cluster <cluster-url> Cluster URL for solicited routes |
| 76 |
--cluster_name <string> Cluster Name, if not set one will be dynamically generated |
| 77 |
--no_advertise <bool> Do not advertise known cluster information to clients |
| 78 |
--cluster_advertise <string> Cluster URL to advertise to other servers |
| 79 |
--connect_retries <number> For implicit routes, number of connect retries |
| 80 |
--cluster_listen <url> Cluster url from which members can solicit routes |
| 81 |
|
| 82 |
Profiling Options: |
| 83 |
--profile <port> Profiling HTTP port |
| 84 |
|
| 85 |
Common Options: |
| 86 |
-h, --help Show this message |
| 87 |
-v, --version Show version |
| 88 |
--help_tls TLS help |
| 89 |
` |
| 90 |
|
| 91 |
// usage will print out the flag options for the server. |
| 92 |
func usage() { |
| 93 |
fmt.Printf("%s\n", usageStr) |
| 94 |
os.Exit(0) |
| 95 |
} |
| 96 |
|
| 97 |
func main() { |
| 98 |
exe := "nats-server" |
| 99 |
|
| 100 |
// Create a FlagSet and sets the usage |
| 101 |
fs := flag.NewFlagSet(exe, flag.ExitOnError) |
| 102 |
fs.Usage = usage |
| 103 |
|
| 104 |
// Configure the options from the flags/config file |
| 105 |
opts, err := server.ConfigureOptions(fs, os.Args[1:], |
| 106 |
server.PrintServerAndExit, |
| 107 |
fs.Usage, |
| 108 |
server.PrintTLSHelpAndDie) |
| 109 |
if err != nil { |
| 110 |
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err)) |
| 111 |
} else if opts.CheckConfig { |
| 112 |
fmt.Fprintf(os.Stderr, "%s: configuration file %s is valid (%s)\n", exe, opts.ConfigFile, opts.ConfigDigest()) |
| 113 |
os.Exit(0) |
| 114 |
} |
| 115 |
|
| 116 |
// Redact secret arguments before expvar reads them. |
| 117 |
server.RedactArgs(os.Args) |
| 118 |
|
| 119 |
// Create the server with appropriate options. |
| 120 |
s, err := server.NewServer(opts) |
| 121 |
if err != nil { |
| 122 |
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err)) |
| 123 |
} |
| 124 |
|
| 125 |
// Configure the logger based on the flags. |
| 126 |
s.ConfigureLogger() |
| 127 |
|
| 128 |
// Start things up. Block here until done. |
| 129 |
if err := server.Run(s); err != nil { |
| 130 |
server.PrintAndDie(err.Error()) |
| 131 |
} |
| 132 |
|
| 133 |
s.WaitForShutdown() |
| 134 |
} |