...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/chaos-dashboard/main.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/chaos-dashboard

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package main
    17  
    18  import (
    19  	"context"
    20  	"flag"
    21  	stdlog "log"
    22  	"os"
    23  
    24  	fxlogr "github.com/chaos-mesh/fx-logr"
    25  	_ "github.com/jinzhu/gorm/dialects/mssql"
    26  	_ "github.com/jinzhu/gorm/dialects/mysql"
    27  	_ "github.com/jinzhu/gorm/dialects/postgres"
    28  	_ "github.com/jinzhu/gorm/dialects/sqlite"
    29  	"go.uber.org/fx"
    30  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    31  	ctrl "sigs.k8s.io/controller-runtime"
    32  
    33  	"github.com/chaos-mesh/chaos-mesh/pkg/config"
    34  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apiserver"
    35  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/collector"
    36  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store"
    37  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/ttlcontroller"
    38  	"github.com/chaos-mesh/chaos-mesh/pkg/log"
    39  	"github.com/chaos-mesh/chaos-mesh/pkg/version"
    40  )
    41  
    42  // @title Chaos Mesh Dashboard API
    43  // @version 2.5
    44  // @description Swagger for Chaos Mesh Dashboard. If you encounter any problems with API, please click on the issues link below to report.
    45  
    46  // @contact.name GitHub Issues
    47  // @contact.url https://github.com/chaos-mesh/chaos-mesh/issues
    48  
    49  // @license.name Apache 2.0
    50  // @license.url http://www.apache.org/licenses/LICENSE-2.0.html
    51  
    52  // @BasePath /api
    53  func main() {
    54  	var printVersion bool
    55  
    56  	flag.BoolVar(&printVersion, "version", false, "print version information and exit")
    57  	flag.Parse()
    58  
    59  	version.PrintVersionInfo("Chaos Dashboard")
    60  	if printVersion {
    61  		os.Exit(0)
    62  	}
    63  
    64  	rootLogger, err := log.NewDefaultZapLogger()
    65  	if err != nil {
    66  		stdlog.Fatal("failed to create root logger", err)
    67  	}
    68  
    69  	ctrl.SetLogger(rootLogger)
    70  	mainLog := rootLogger.WithName("main")
    71  	fxLogger := rootLogger.WithName("fx")
    72  
    73  	dashboardConfig, err := config.GetChaosDashboardEnv()
    74  	if err != nil {
    75  		mainLog.Error(err, "invalid ChaosDashboardConfig")
    76  		os.Exit(1)
    77  	}
    78  	dashboardConfig.Version = version.Get().GitVersion
    79  
    80  	persistTTLConfigParsed, err := dashboardConfig.PersistTTL.Parse()
    81  	if err != nil {
    82  		mainLog.Error(err, "invalid PersistTTLConfig")
    83  		os.Exit(1)
    84  	}
    85  
    86  	controllerRuntimeSignalHandlerContext := ctrl.SetupSignalHandler()
    87  	app := fx.New(
    88  		fx.WithLogger(fxlogr.WithLogr(&fxLogger)),
    89  		fx.Supply(rootLogger),
    90  		fx.Provide(
    91  			func() (context.Context, *config.ChaosDashboardConfig, *config.TTLConfig) {
    92  				return controllerRuntimeSignalHandlerContext, dashboardConfig, persistTTLConfigParsed
    93  			},
    94  			store.Bootstrap,
    95  			collector.Bootstrap,
    96  			ttlcontroller.Bootstrap,
    97  		),
    98  		store.Module,
    99  		apiserver.Module,
   100  		fx.Invoke(collector.Register),
   101  		fx.Invoke(ttlcontroller.Register),
   102  	)
   103  
   104  	app.Run()
   105  }
   106