Skip to content

Commit

Permalink
config: add external-address
Browse files Browse the repository at this point in the history
It's the IP and port that are to be shown in the API documentation.

Signed-off-by: Tatiana Nesterenko <tatiana@nspcc.io>
  • Loading branch information
tatiana-nspcc committed Feb 9, 2024
1 parent 2f1b9d7 commit 5dac12e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 23 deletions.
3 changes: 3 additions & 0 deletions cmd/neofs-rest-gw/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ var serverFlags = []string{
restapi.FlagTLSKeepAlive,
restapi.FlagTLSReadTimeout,
restapi.FlagTLSWriteTimeout,
restapi.FlagExternalAddress,
}

var bindings = map[string]string{
Expand Down Expand Up @@ -418,6 +419,8 @@ func serverConfig(v *viper.Viper) *restapi.ServerConfig {
TLSKeepAlive: v.GetDuration(cfgServerSection + restapi.FlagTLSKeepAlive),
TLSReadTimeout: v.GetDuration(cfgServerSection + restapi.FlagTLSReadTimeout),
TLSWriteTimeout: v.GetDuration(cfgServerSection + restapi.FlagTLSWriteTimeout),

ExternalAddress: v.GetString(cfgServerSection + restapi.FlagExternalAddress),
}
}

Expand Down
22 changes: 19 additions & 3 deletions cmd/neofs-rest-gw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"os/signal"
"syscall"

Expand All @@ -23,14 +24,29 @@ func main() {
logger.Fatal("init neofs", zap.Error(err))
}

serverCfg := serverConfig(v)
serverCfg.SuccessfulStartCallback = neofsAPI.StartCallback

// Unmarshal the JSON into a map
var swaggerMap map[string]interface{}
err = json.Unmarshal(restapi.SwaggerJSON, &swaggerMap)
if err != nil {
logger.Fatal("unmarshaling SwaggerJSON", zap.Error(err))
}

Check warning on line 35 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L27-L35

Added lines #L27 - L35 were not covered by tests

swaggerMap["host"] = serverCfg.ExternalAddress

// Marshal the map back into json.RawMessage
restapi.SwaggerJSON, err = json.MarshalIndent(swaggerMap, "", " ")
if err != nil {
logger.Fatal("marshaling updated SwaggerJSON", zap.Error(err))
}

Check warning on line 43 in cmd/neofs-rest-gw/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/neofs-rest-gw/main.go#L37-L43

Added lines #L37 - L43 were not covered by tests

swaggerSpec, err := loads.Analyzed(restapi.SwaggerJSON, "")
if err != nil {
logger.Fatal("init spec", zap.Error(err))
}

serverCfg := serverConfig(v)
serverCfg.SuccessfulStartCallback = neofsAPI.StartCallback

api := operations.NewNeofsRestGwAPI(swaggerSpec)
server := restapi.NewServer(api, serverCfg)
defer func() {
Expand Down
3 changes: 3 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ server:
tls-read-timeout: 30s
# Maximum duration before timing out write of the response.
tls-write-timeout: 30s

# The IP and port to be shown in the API documentation.
external-address: localhost:8090
41 changes: 22 additions & 19 deletions docs/gate-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,30 @@ tls-listen-limit: 0
tls-keep-alive: 3m
tls-read-timeout: 30s
tls-write-timeout: 30s

external-address: localhost:8090
```
| Parameter | Type | Default value | Description |
|------------------------|------------|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `scheme` | `[]string` | `[http]` | The listeners to enable, this can be repeated and defaults to the schemes in the swagger spec. |
| `cleanup-timeout` | `duration` | `10s` | Grace period for which to wait before killing idle connections. |
| `graceful-timeout` | `duration` | `15s` | Grace period for which to wait before shutting down the server. |
| `max-header-size` | `int` | `1000000` | Controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line. It does not limit the size of the request body. |
| `listen-address` | `string` | `localhost:8080` | The IP and port to listen on. |
| `listen-limit` | `int` | `0` | Limit the number of outstanding requests. `0` means no limit | |
| `keep-alive` | `duration` | `3m` | Sets the TCP keep-alive timeouts on accepted connections. |
| `read-timeout` | `duration` | `30s` | Maximum duration before timing out read of the request. It prunes dead TCP connections (e.g. closing laptop mid-download). |
| `write-timeout` | `duration` | `30s` | Maximum duration before timing out write of the response. |
| `tls-listen-address` | `string` | `localhost:8081` | The IP and port to listen on for TLS. |
| `tls-certificate` | `string` | | The certificate file to use for secure connections. |
| `tls-key` | `string` | | The private key file to use for secure connections (without passphrase). |
| `tls-ca` | `string` | | The certificate authority certificate file to be used with mutual tls auth. |
| `tls-listen-limit` | `int` | `0` | Limit the number of outstanding requests for TLS. `0` means no limit | |
| `tls-keep-alive` | `duration` | `3m` | Sets the TCP keep-alive timeouts on accepted connections for TLS. |
| `tls-read-timeout` | `duration` | `30s` | Maximum duration before timing out read of the request for TLS. It prunes dead TCP connections (e.g. closing laptop mid-download). |
| `tls-write-timeout` | `duration` | `30s` | Maximum duration before timing out write of the response for TLS. |
| Parameter | Type | Default value | Description |
|----------------------|------------|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `scheme` | `[]string` | `[http]` | The listeners to enable, this can be repeated and defaults to the schemes in the swagger spec. |
| `cleanup-timeout` | `duration` | `10s` | Grace period for which to wait before killing idle connections. |
| `graceful-timeout` | `duration` | `15s` | Grace period for which to wait before shutting down the server. |
| `max-header-size` | `int` | `1000000` | Controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line. It does not limit the size of the request body. |
| `listen-address` | `string` | `localhost:8080` | The IP and port to listen on. |
| `listen-limit` | `int` | `0` | Limit the number of outstanding requests. `0` means no limit | |
| `keep-alive` | `duration` | `3m` | Sets the TCP keep-alive timeouts on accepted connections. |
| `read-timeout` | `duration` | `30s` | Maximum duration before timing out read of the request. It prunes dead TCP connections (e.g. closing laptop mid-download). |
| `write-timeout` | `duration` | `30s` | Maximum duration before timing out write of the response. |
| `tls-listen-address` | `string` | `localhost:8081` | The IP and port to listen on for TLS. |
| `tls-certificate` | `string` | | The certificate file to use for secure connections. |
| `tls-key` | `string` | | The private key file to use for secure connections (without passphrase). |
| `tls-ca` | `string` | | The certificate authority certificate file to be used with mutual tls auth. |
| `tls-listen-limit` | `int` | `0` | Limit the number of outstanding requests for TLS. `0` means no limit | |
| `tls-keep-alive` | `duration` | `3m` | Sets the TCP keep-alive timeouts on accepted connections for TLS. |
| `tls-read-timeout` | `duration` | `30s` | Maximum duration before timing out read of the request for TLS. It prunes dead TCP connections (e.g. closing laptop mid-download). |
| `tls-write-timeout` | `duration` | `30s` | Maximum duration before timing out write of the response for TLS. |
| `external-address` | `string` | `localhost:8090` | The IP and port to be shown in the API documentation. |

# `wallet` section

Expand Down
8 changes: 7 additions & 1 deletion gen/restapi/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gen/restapi/server_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/rest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ info:
description: REST API for native integration with NeoFS.
version: v1

# host value is replaced by `external-address` in config.yaml
host: localhost:8090
basePath: /v1
schemes:
Expand Down
3 changes: 3 additions & 0 deletions templates/server/config.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
FlagTLSKeepAlive = "tls-keep-alive"
FlagTLSReadTimeout = "tls-read-timeout"
FlagTLSWriteTimeout = "tls-write-timeout"
FlagExternalAddress = "external-address"
)

// BindDefaultFlag init default flag.
Expand All @@ -54,4 +55,6 @@ func BindDefaultFlags(flagSet *pflag.FlagSet) {
flagSet.Duration(FlagTLSKeepAlive, 3*time.Minute, "sets the TCP keep-alive timeouts on accepted connections. It prunes dead TCP connections ( e.g. closing laptop mid-download)")
flagSet.Duration(FlagTLSReadTimeout, 30*time.Second, "maximum duration before timing out read of the request")
flagSet.Duration(FlagTLSWriteTimeout, 30*time.Second, "maximum duration before timing out write of the response")

flagSet.String(FlagExternalAddress, "localhost:8090", "the IP and port to be shown in the API documentation")
}
6 changes: 6 additions & 0 deletions templates/server/server.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type ServerConfig struct {
TLSCertificateKey string
TLSCACertificate string

ExternalAddress string

SuccessfulStartCallback func()
}

Expand All @@ -90,6 +92,7 @@ func NewServer(api *{{ .APIPackageAlias }}.{{ pascalize .Name }}API, cfg *Server
s.TLSKeepAlive = cfg.TLSKeepAlive
s.TLSReadTimeout = cfg.TLSReadTimeout
s.TLSWriteTimeout = cfg.TLSWriteTimeout
s.ExternalAddress = cfg.ExternalAddress
s.shutdown = make(chan struct{})
s.api = api
s.startCallback = cfg.SuccessfulStartCallback
Expand Down Expand Up @@ -126,6 +129,9 @@ type Server struct {
TLSKeepAlive time.Duration
TLSReadTimeout time.Duration
TLSWriteTimeout time.Duration

ExternalAddress string

httpsServerL net.Listener

startCallback func()
Expand Down

0 comments on commit 5dac12e

Please sign in to comment.