github.com/minio/console@v1.3.0/cmd/console/app_commands.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strconv"
    23  	"time"
    24  
    25  	"github.com/minio/console/pkg/logger"
    26  
    27  	"github.com/minio/cli"
    28  	"github.com/minio/console/api"
    29  )
    30  
    31  var appCmds = []cli.Command{
    32  	serverCmd,
    33  	updateCmd,
    34  }
    35  
    36  // StartServer starts the console service
    37  func StartServer(ctx *cli.Context) error {
    38  	if err := loadAllCerts(ctx); err != nil {
    39  		// Log this as a warning and continue running console without TLS certificates
    40  		api.LogError("Unable to load certs: %v", err)
    41  	}
    42  
    43  	xctx := context.Background()
    44  
    45  	transport := api.PrepareSTSClientTransport(false, api.LocalAddress)
    46  	if err := logger.InitializeLogger(xctx, transport.Transport); err != nil {
    47  		fmt.Println("error InitializeLogger", err)
    48  		logger.CriticalIf(xctx, err)
    49  	}
    50  	// custom error configuration
    51  	api.LogInfo = logger.Info
    52  	api.LogError = logger.Error
    53  	api.LogIf = logger.LogIf
    54  
    55  	var rctx api.Context
    56  	if err := rctx.Load(ctx); err != nil {
    57  		api.LogError("argument validation failed: %v", err)
    58  		return err
    59  	}
    60  
    61  	server, err := buildServer()
    62  	if err != nil {
    63  		api.LogError("Unable to initialize console server: %v", err)
    64  		return err
    65  	}
    66  
    67  	server.Host = rctx.Host
    68  	server.Port = rctx.HTTPPort
    69  	// set conservative timesout for uploads
    70  	server.ReadTimeout = 1 * time.Hour
    71  	// no timeouts for response for downloads
    72  	server.WriteTimeout = 0
    73  	api.Port = strconv.Itoa(server.Port)
    74  	api.Hostname = server.Host
    75  
    76  	if len(api.GlobalPublicCerts) > 0 {
    77  		// If TLS certificates are provided enforce the HTTPS schema, meaning console will redirect
    78  		// plain HTTP connections to HTTPS server
    79  		server.EnabledListeners = []string{"http", "https"}
    80  		server.TLSPort = rctx.HTTPSPort
    81  		// Need to store tls-port, tls-host un config variables so secure.middleware can read from there
    82  		api.TLSPort = strconv.Itoa(server.TLSPort)
    83  		api.Hostname = rctx.Host
    84  		api.TLSRedirect = rctx.TLSRedirect
    85  	}
    86  
    87  	defer server.Shutdown()
    88  
    89  	if err = server.Serve(); err != nil {
    90  		server.Logf("error serving API: %v", err)
    91  		return err
    92  	}
    93  
    94  	return nil
    95  }