Created
This commit is contained in:
51
config/env.go
Normal file
51
config/env.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var DotenvLoaded bool = false
|
||||
|
||||
// Getenv tries to get an environment variable
|
||||
// and fails if it isn't set
|
||||
func Getenv(key string) string {
|
||||
if !DotenvLoaded {
|
||||
err := godotenv.Load()
|
||||
if err != nil && os.Getenv(key) == "" {
|
||||
panic(fmt.Errorf("environment variable does not exist or is not set: %s", key))
|
||||
}
|
||||
DotenvLoaded = true
|
||||
}
|
||||
if val := os.Getenv(key); val != "" {
|
||||
return val
|
||||
}
|
||||
|
||||
panic(fmt.Errorf("environment variable does not exist or is not set: %s", key))
|
||||
}
|
||||
|
||||
func GetenvDefault(key string, fallback string) string {
|
||||
if !DotenvLoaded {
|
||||
err := godotenv.Load()
|
||||
if err != nil && os.Getenv(key) == "" {
|
||||
return fallback
|
||||
}
|
||||
DotenvLoaded = true
|
||||
}
|
||||
if val := os.Getenv(key); val != "" {
|
||||
return val
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
func GetenvDefaultLowercase(key string, fallback string) string {
|
||||
return strings.ToLower(GetenvDefault(key, fallback))
|
||||
}
|
||||
|
||||
func DebugActive() bool {
|
||||
return strings.ToLower(GetenvDefault("DEBUG", "false")) == "true"
|
||||
}
|
||||
68
config/mongodb.go
Normal file
68
config/mongodb.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gopkg.cloudyne.io/go-fiber-template/helpers"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
var Client *mongo.Client = ConnectDB()
|
||||
var Database *mongo.Database = Client.Database(Getenv("MONGODB"))
|
||||
|
||||
func ConnectDB() *mongo.Client {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
mongoOptions := options.Client()
|
||||
mongoOptions.ApplyURI(Getenv("MONGOURI"))
|
||||
|
||||
if mongotls := GetenvDefault("MONGOCACERT", ""); mongotls != "" {
|
||||
rootCert := x509.NewCertPool()
|
||||
rootCert.AppendCertsFromPEM([]byte(mongotls))
|
||||
mongoOptions.SetTLSConfig(&tls.Config{
|
||||
RootCAs: rootCert,
|
||||
})
|
||||
}
|
||||
|
||||
client, err := mongo.Connect(ctx, mongoOptions)
|
||||
helpers.PanicIfError(err)
|
||||
|
||||
err = client.Ping(ctx, nil)
|
||||
helpers.PanicIfError(err)
|
||||
helpers.Logger.Print("Connected to MongoDB")
|
||||
|
||||
database := Getenv("MONGODB")
|
||||
|
||||
helpers.Logger.Printf("Checking for database %s", database)
|
||||
|
||||
databases, err := client.ListDatabaseNames(ctx, bson.M{})
|
||||
helpers.PanicIfError(err)
|
||||
|
||||
exists := false
|
||||
for _, dbname := range databases {
|
||||
if dbname == database {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !exists {
|
||||
panic(fmt.Errorf("error: not database with given name %s exists", database))
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func CheckDB() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
Client.Ping(ctx, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user