Build Viper's Config file and Firebase Credential together with Golang Statik

Viper Library for Go Config. It seems to be made to be placed outside the project and monitored, but I wanted to include it in the binary, so I investigated and implemented it. What about Firebase in terms of security ... https://github.com/spf13/viper

overall structure

Project root/
 ├ asset/
 │  ├ environment/Environment name.yaml
 │  └ firebase/Credentials.json
 │
 ├ statik/statik.go
 ├ config/config.go
 ├ firebase.firebase.go
 └ main.go

Introduced statik
$ go get github.com/rakyll/[email protected]

##### Place environment/environment name.yaml and firebase/credential.json under the project root/asset.
##### Execute the command. statik/statik.go is generated. ``` $ statik -src asset ```
The following processing for reading files with Static

main.go


package main

import (
  "flag"

  "Project root/config"
  "Project root/firebase"
  "github.com/facebookgo/grace/gracehttp"
  "github.com/labstack/echo/v4"
  //statik Binary asset file PATH
  _ "Project root/statik"
)

func main() {
  //Environment setting acquisition Parameter name,Default,Explanation
  env := flag.String("e", "develop", "Environment name")
  //flag.Enter value with Parse
  flag.Parse()
  //Initialize config by passing parameters
  config.Init(*env, "environment")
  //Firebase initialization
  firebase.Init()

  //Server start(echo)
  e := echo.New()
  e.HideBanner = true
  e.HidePort = true

  e.POST("/", handler.function)

  e.Server.Addr = ":8000"
  e.Logger.Fatal(gracehttp.Serve(e.Server))
}

config.go


package config

import (
  "bytes"
  "io/ioutil"

  "github.com/rakyll/statik/fs"
  "github.com/spf13/viper"
)

var c *viper.Viper

//Init Config initialization
func Init(env string, path string) {
  //Use a configuration file library called Viper
  c = viper.New()
  c.SetConfigType("YAML")
  // statik
  fileSystem, _ := fs.New()
  //Specify the directory specified by the statik command
  f, err := fileSystem.Open("/" + path + "/" + env + ".yaml")
  if err != nil {
  }
  //Read file to byte
  r, _ := ioutil.ReadAll(f)
  if err := c.ReadConfig(bytes.NewBuffer(r)); err != nil {
    panic(err)
  }
}

// GetConfig returns config
func GetConfig() *viper.Viper {
  return c
}

firebase.go


package firebase

import (
  "context"
  "io/ioutil"

  firebase "firebase.google.com/go"
  "firebase.google.com/go/auth"
  "firebase.google.com/go/messaging"
  "github.com/labstack/echo/v4"
  "github.com/rakyll/statik/fs"
  "google.golang.org/api/option"
)

var fb *firebase.App
var message *messaging.Client
var authenticate *auth.Client

//Init firebase initialization
func Init() {
  c := config.GetConfig()
  ctx := context.Background()
  config := &firebase.Config{ProjectID:Project ID}
  //
  fileSystem, _ := fs.New()
  f, openErr := fileSystem.Open(firebase/Credential name.json)
  if openErr != nil {
    panic(openErr)
  }
  //Also to byte
  r, readErr := ioutil.ReadAll(f)
  if readErr != nil {
    panic(readErr)
  }
  //Generate ClientOption with WithCredentials JSON
  opt := option.WithCredentialsJSON(r)
  var err error
  fb, err = firebase.NewApp(ctx, config, opt)
  if err != nil {
    panic(err)
  }

  message, err = fb.Messaging(ctx)
  if err != nil {
    panic(err)
  }
  authenticate, err = fb.Auth(ctx)
  if err != nil {
    panic(err)
  }
}

Recommended Posts

Build Viper's Config file and Firebase Credential together with Golang Statik
Authentication process with gRPC and Firebase Authentication
Build a deb file with Docker
Replace the directory name and the file name in the directory together with a Linux command.