github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/app_listenAndServeAll.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  // Copyright 2019-present Highload LTD (UK CN: 11893420)
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  
    11  package gramework
    12  
    13  import "fmt"
    14  
    15  // ListenAndServeAll serves HTTP and HTTPS automatically.
    16  // HTTPS is served on :443.
    17  // If it can't serve http or https, it logs an error and
    18  // exit the server with app.Logger.Fatalf().
    19  func (app *App) ListenAndServeAll(httpAddr ...string) {
    20  	go func() {
    21  		bindAddr := ":443"
    22  		if app.TLSPort != 0 {
    23  			bindAddr = fmt.Sprintf(":%d", app.TLSPort)
    24  		}
    25  		err := app.ListenAndServeAutoTLS(bindAddr)
    26  		app.internalLog.Fatalf("can't serve tls: %s", err)
    27  	}()
    28  
    29  	if err := app.ListenAndServe(httpAddr...); err != nil {
    30  		app.internalLog.Fatalf("can't serve http: %s", err)
    31  	}
    32  }
    33  
    34  // ListenAndServeAllDev serves HTTP and HTTPS automatically
    35  // with localhost HTTPS support via self-signed certs.
    36  // HTTPS is served on :443.
    37  // If it can't serve http or https, it logs an error and
    38  // exit the server with app.Logger.Fatalf().
    39  // Deprecated: Use ListenAndServeAll() instead
    40  func (app *App) ListenAndServeAllDev(httpAddr ...string) {
    41  	app.ListenAndServeAll(httpAddr...)
    42  }