github.com/minio/console@v1.4.1/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  	"net/http"
    23  	"strconv"
    24  	"time"
    25  
    26  	"github.com/minio/console/pkg/logger"
    27  
    28  	"github.com/minio/cli"
    29  	"github.com/minio/console/api"
    30  )
    31  
    32  var appCmds = []cli.Command{
    33  	serverCmd,
    34  	updateCmd,
    35  }
    36  
    37  // StartServer starts the console service
    38  func StartServer(ctx *cli.Context) error {
    39  	if err := loadAllCerts(ctx); err != nil {
    40  		// Log this as a warning and continue running console without TLS certificates
    41  		api.LogError("Unable to load certs: %v", err)
    42  	}
    43  
    44  	xctx := context.Background()
    45  
    46  	transport := api.PrepareSTSClientTransport(api.LocalAddress).Transport.(*http.Transport)
    47  	if err := logger.InitializeLogger(xctx, transport); err != nil {
    48  		fmt.Println("error InitializeLogger", err)
    49  		logger.CriticalIf(xctx, err)
    50  	}
    51  	// custom error configuration
    52  	api.LogInfo = logger.Info
    53  	api.LogError = logger.Error
    54  	api.LogIf = logger.LogIf
    55  
    56  	var rctx api.Context
    57  	if err := rctx.Load(ctx); err != nil {
    58  		api.LogError("argument validation failed: %v", err)
    59  		return err
    60  	}
    61  
    62  	server, err := buildServer()
    63  	if err != nil {
    64  		api.LogError("Unable to initialize console server: %v", err)
    65  		return err
    66  	}
    67  
    68  	server.Host = rctx.Host
    69  	server.Port = rctx.HTTPPort
    70  	// set conservative timesout for uploads
    71  	server.ReadTimeout = 1 * time.Hour
    72  	// no timeouts for response for downloads
    73  	server.WriteTimeout = 0
    74  	api.Port = strconv.Itoa(server.Port)
    75  	api.Hostname = server.Host
    76  
    77  	if len(api.GlobalPublicCerts) > 0 {
    78  		// If TLS certificates are provided enforce the HTTPS schema, meaning console will redirect
    79  		// plain HTTP connections to HTTPS server
    80  		server.EnabledListeners = []string{"http", "https"}
    81  		server.TLSPort = rctx.HTTPSPort
    82  		// Need to store tls-port, tls-host un config variables so secure.middleware can read from there
    83  		api.TLSPort = strconv.Itoa(server.TLSPort)
    84  		api.Hostname = rctx.Host
    85  		api.TLSRedirect = rctx.TLSRedirect
    86  	}
    87  
    88  	defer server.Shutdown()
    89  
    90  	if err = server.Serve(); err != nil {
    91  		server.Logf("error serving API: %v", err)
    92  		return err
    93  	}
    94  
    95  	return nil
    96  }