github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/cmd/budget-manager/main.go (about)

     1  package main
     2  
     3  import (
     4  	stdlog "log"
     5  	"os"
     6  	"os/signal"
     7  	"syscall"
     8  
     9  	"github.com/ShoshinNikita/budget-manager/internal/app"
    10  	"github.com/ShoshinNikita/budget-manager/internal/logger"
    11  )
    12  
    13  //nolint:gochecknoglobals
    14  var (
    15  	// version is a version of the app. It must be set during the build process with -ldflags flag
    16  	version = "unknown"
    17  	// gitHash is the last commit hash. It must be set during the build process with -ldflags flag
    18  	gitHash = "unknown"
    19  )
    20  
    21  // Swagger General Info
    22  //
    23  //nolint:lll
    24  //
    25  // @title Budget Manager API
    26  // @version v0.2
    27  // @description Easy-to-use, lightweight and self-hosted solution to track your finances - [GitHub](https://github.com/ShoshinNikita/budget-manager)
    28  //
    29  // @BasePath /api
    30  //
    31  // @securityDefinitions.basic BasicAuth
    32  //
    33  // @license.name MIT
    34  // @license.url https://github.com/ShoshinNikita/budget-manager/blob/master/LICENSE
    35  //
    36  
    37  func main() {
    38  	cfg, err := app.ParseConfig()
    39  	if err != nil {
    40  		stdlog.Fatalf("couldn't parse config: %s\n", err)
    41  	}
    42  	log := logger.New(cfg.Logger)
    43  
    44  	app := app.NewApp(cfg, log, version, gitHash)
    45  
    46  	if err := app.PrepareComponents(); err != nil {
    47  		stdlog.Fatalf("couldn't prepare components: %s\n", err)
    48  	}
    49  
    50  	appErrCh := make(chan error, 1)
    51  	go func() {
    52  		appErrCh <- app.Run()
    53  	}()
    54  
    55  	term := make(chan os.Signal, 1)
    56  	signal.Notify(term, syscall.SIGINT, syscall.SIGTERM)
    57  
    58  	// Wait for an interrupt signal or an app error
    59  	select {
    60  	case <-term:
    61  		log.Warn("got an interrupt signal")
    62  	case err := <-appErrCh:
    63  		log.WithError(err).Error("app finished with error")
    64  	}
    65  
    66  	app.Shutdown()
    67  }