helper

package module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2024 License: BSD-3-Clause Imports: 26 Imported by: 0

README

go-helper

Under development

Package croonix/go-helper help in the development of Go applications by providing some useful functions.

Usage

To use, only import as helper and use it:

package main

import (
	"fmt"

	helper "github.com/croonix/go-helper"
)

...

Tasks

  • Add Test files

Documentation

Overview

This module contains helper functions primary focused on Google Cloud Platform

Index

Constants

View Source
const WrongMessage = "What are you doing here?"

Variables

This section is empty.

Functions

func AddServiceAccountUserRole added in v1.2.0

func AddServiceAccountUserRole(impersonateAccount string, targetAccount string, principalAccount string) error

func CatMode added in v1.3.3

func CatMode(r *http.Request)

CatMode is a function that will print the request information It will print the URL, Method, Headers and Body of the request It is used to debug the request information

func CheckBody added in v1.3.0

func CheckBody(w http.ResponseWriter, r *http.Request)

CheckBody is a function that will print the request information It will print the URL, Method, Headers and Body of the request It is used to debug the request information Needs to be used with io.TeeReader to be able to read the body multiple times Example:

  • http.HandleFunc("/", CheckBody)

Output:

  • Request information:
  • Request URL: /?test=1
  • Request Method: GET
  • Request Headers: map[Accept:[*/*] Accept-Encoding:[gzip] User-Agent:Go-http-client/1.1]
  • Request Body: test=1

func ConnectCloudSQL added in v1.0.0

func ConnectCloudSQL(database string) *sql.DB

func ConvertToBoolPointer added in v1.4.0

func ConvertToBoolPointer(value interface{}) *bool

ConvertToBoolPointer is a simple function that converts a interface to a *bool

func GetFavicon added in v1.1.0

func GetFavicon(w http.ResponseWriter, r *http.Request)

func GetProjectFromServiceAccount added in v1.2.1

func GetProjectFromServiceAccount(serviceAccount string) string

func GormDatabase added in v1.4.0

func GormDatabase(databaseName string) *gorm.DB

GormDatabase is a function that returns a GORM database connection - Use simple Structure to connect to the database - Use the following environment variables:

  • DATABASE_USER
  • DATABASE_PASS
  • DATABASE_IP

Works only with MySQL databases (for now)

func HealthCheck

func HealthCheck(w http.ResponseWriter, r *http.Request)

func ImpersonateSA added in v1.0.0

func ImpersonateSA(context context.Context, serviceAccount string, scope string) (token oauth2.TokenSource, err error)

func Logger

func Logger(message string, level string)

func MessageHelper added in v1.0.0

func MessageHelper(w http.ResponseWriter, message string)

func MustGetenv

func MustGetenv(k string) string

MustGetenv validates if the environment variable is set and returns it otherwise it will log a fatal error and exit the program It is used to validate the environment variables Example:

  • os.Setenv("TEST", "test")
  • log.Println(MustGetenv("TEST"))
  • log.Println(MustGetenv("TEST2"))

Output:

  • test
  • 2021/08/31 11:30:00 Missing environment variable: TEST2
  • exit status 1

func RemoveServiceAccountUserRole added in v1.2.0

func RemoveServiceAccountUserRole(impersonateAccount string, targetAccount string, principalAccount string) error

func UndefinedAnswer

func UndefinedAnswer(w http.ResponseWriter, r *http.Request)

func WrongParameter added in v1.0.0

func WrongParameter(w http.ResponseWriter, r *http.Request)

func WrongPath

func WrongPath() http.Handler

Types

type Message added in v1.0.0

type Message struct {
	Message string `json:"message"`
}

type TimestampSerializer added in v1.4.0

type TimestampSerializer struct {
}

TimestampSerializer is a custom serializer for GORM to handle timestamps values

func (TimestampSerializer) Scan added in v1.4.0

func (TimestampSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.Value, dbValue interface{}) (err error)

func (TimestampSerializer) Value added in v1.4.0

func (TimestampSerializer) Value(ctx context.Context, field *schema.Field, dst reflect.Value, fieldValue interface{}) (interface{}, error)

Value implements serializer interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL