github.com/lastbackend/toolkit@v0.0.0-20241020043710-cafa37b95aad/examples/service/main.go (about)

     1  /*
     2  Copyright [2014] - [2023] The Last.Backend authors.
     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  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"github.com/lastbackend/toolkit/examples/service/config"
    23  	servicepb "github.com/lastbackend/toolkit/examples/service/gen"
    24  	"github.com/lastbackend/toolkit/examples/service/internal/controller"
    25  	"github.com/lastbackend/toolkit/examples/service/internal/repository"
    26  	"github.com/lastbackend/toolkit/examples/service/internal/server"
    27  	"github.com/lastbackend/toolkit/pkg/runtime"
    28  	"github.com/lastbackend/toolkit/pkg/server/http"
    29  	"os"
    30  	"time"
    31  )
    32  
    33  func main() {
    34  	// define service with name and options
    35  	app, err := servicepb.NewExampleService("example",
    36  		runtime.WithVersion("0.1.0"),
    37  		runtime.WithDescription("Example microservice"),
    38  		runtime.WithEnvPrefix("LB"),
    39  	)
    40  	if err != nil {
    41  		fmt.Println(err)
    42  	}
    43  
    44  	// Config management
    45  	cfg := config.New()
    46  
    47  	if err := app.RegisterConfig(cfg); err != nil {
    48  		app.Log().Error(err)
    49  		return
    50  	}
    51  
    52  	// Add packages
    53  	app.RegisterPackage(repository.NewRepository, controller.NewController)
    54  
    55  	// Add server
    56  	app.Server().GRPC().SetService(server.NewServer)
    57  	app.Server().GRPC().SetInterceptor(server.NewExampleGRPCServerInterceptor)
    58  
    59  	app.Server().HTTPNew("", nil)
    60  	app.Server().HTTP().SetMiddleware(server.RegisterExampleHTTPServerMiddleware)
    61  	app.Server().HTTP().AddHandler(http.MethodGet, "/", server.ExampleHTTPServerHandler, http.WithMiddleware(server.MWAuthenticate))
    62  
    63  	app.RegisterOnStartHook(func(ctx context.Context) error {
    64  		time.Sleep(3 * time.Second)
    65  		app.Log().Info("call gracefully stop")
    66  		app.Stop(ctx, fmt.Errorf("test error"))
    67  		return nil
    68  	})
    69  
    70  	//go func() {
    71  	//	time.Sleep(5 * time.Second)
    72  	//	app.Log().Info("call gracefully stop")
    73  	//	app.Stop(context.Background())
    74  	//}()
    75  
    76  	// Service run
    77  	if err := app.Start(context.Background()); err != nil {
    78  		app.Log().Errorf("could not run the service %v", err)
    79  		os.Exit(1)
    80  		return
    81  	}
    82  
    83  	// time.Sleep(10 * time.Second)
    84  	// app.Stop(context.Background())
    85  
    86  	app.Log().Info("graceful stop")
    87  }