Categorygithub.com/pmpsilva/go-starter
modulepackage
0.2.3
Repository: https://github.com/pmpsilva/go-starter.git
Documentation: pkg.go.dev

# README

go-starter

bootstrap for golang project with logg database and env

usage/configurations

Logger:

Zap

    logger, err := init.BuildLogger()
    if err != nil {
        log.Fatalf("can't initialize zap logger: %v", err)
    }
    
    //how to add an id to the logger (for example on a request)
    ctx := init.DeriveContextWithRequestId(context.Background())
    logger.Info("Log with an id", config.ZapFieldWithRequestIdFromCtx(ctx))

DB

For database this project uses postgres and the migrations will be on db/migrations folder

   //red env variables
   connectionString, err := starter.BuildDbString()
   if err != nil {
       logger.Error("Fail to get connection string", zap.Error(err))
       os.Exit(1)
   }
   //open DbConnecion
   dbConnection, err := starter.OpenDbConnection(connectionString, logger)
   if err != nil {
       os.Exit(1)
   }
   //to use migrations on //db/migartions
   _ = starter.RunMigrations(dataSource, logger)
   
   //transactionManager initialization 
   transactionManager := starter.NewTransactionManager(dataSource)
   
   //usage example at service or repository level
   var resultToReturn uuid.UUID
   if err := transactionManager.ExecWithTransaction(func(tx *sql.Tx) error {
   	//repository method to perform some query to db
   	dbResult := uuid.New()
   	if err != nil {
   		return err
   	}
   	//attach result to external variable
   	resultToReturn = dbResult

   	return nil
   }); err != nil {
   	//deal with error
   }

Env Variables

Variable NameDefault ValueRequiredPossible values
DB_HOSTniltrue*
DB_PORTniltrue*
DB_USERniltrue*
DB_PASSWORDniltrue*
DB_NAMEniltrue*
HTTP_HOST0.0.0.0false*
HTTP_PORT8080false*
CONTEXT_PATH/false*
NET_HTTP_PPROF_ENABLEDfalsefalsetrue, false
LOGGER_ENVdevelopmentfalsedevelopment, production

# Packages

No description provided by the author