- 浏览: 2542901 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
2018 Golang Update(5)Configuration
Add the Dependency to Dep
>dep ensure -add github.com/spf13/viper
All the Viper method
https://godoc.org/github.com/spf13/viper#pkg-variables
Here is How I use that, when I run the command, I can do as follow to pass the ENV
>ENVIRONMENT=PROD bin/restful_go_api
Add dependency to use Viper
>dep ensure -add github.com/spf13/viper
The Configuration style will be YAML style config-prod.yaml as follow
restful_go_api/conf/config-prod.yaml
restful_go_api/conf/config-dev.yaml
restful_go_api/conf/config-stage.yaml
http:
port: 8081
gin:
debug: false
database:
type: mysql
host: 192.168.1.109
user: watermonitor
password: xxxxxx
port: 3306
name: watermonitor
here is the config module for init the Viper
restful_go_api/src/sillycat.com/restful_go_api/config/vipconfig.go
package config
import (
"fmt"
"github.com/spf13/viper"
"log"
"os"
)
const appName = "config"
func InitViperConfig() {
log.Println("Start to init the config--------")
viper.SetDefault("http.port", "8080")
if os.Getenv("ENVIRONMENT") == "PROD" {
log.Println("system is running under PROD mode")
viper.SetConfigName(appName + "-prod") // name of config file (without extension)
} else if os.Getenv("ENVIRONMENT") == "STAGE" {
log.Println("system is running under STAGE mode")
viper.SetConfigName(appName + "-stage") // name of config file (without extension)
} else {
log.Println("system is running under DEV mode")
viper.SetConfigName(appName + "-dev") // name of config file (without extension)
}
viper.AddConfigPath("/etc/" + appName + "/") // path to look for the config file in
viper.AddConfigPath("./") // optionally look for config in the working directory
viper.AddConfigPath("./conf/")
viper.AutomaticEnv()
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
In the database init class, here is how I use that
restful_go_api/src/sillycat.com/restful_go_api/common/db.go
package common
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"github.com/spf13/viper"
"sillycat.com/restful_go_api/config"
)
var engine = InitDatabase()
func InitDatabase() *xorm.Engine {
config.InitViperConfig()
var engine *xorm.Engine
var err error
dbType := viper.GetString("database.type")
dbUser := viper.GetString("database.user")
dbPwd := viper.GetString("database.password")
dbHost := viper.GetString("database.host")
dbPort := viper.GetString("database.port")
dbName := viper.GetString("database.name")
engine, err = xorm.NewEngine(dbType, dbUser+":"+dbPwd+"@tcp("+dbHost+":"+dbPort+")/"+dbName+"?charset=utf8")
if err != nil {
fmt.Println(err)
}
engine.Ping() //ping
engine.ShowSQL(true) //show SQL
engine.Logger().SetLevel(core.LOG_DEBUG)
return engine
}
func GetDatabase() *xorm.Engine {
return engine
}
In the main.go, here is how I use that configuration
restful_go_api/src/sillycat.com/restful_go_api/main.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"log"
"sillycat.com/restful_go_api/waterrecord"
)
var DB = make(map[string]string)
func SetupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
if viper.GetBool("gin.debug") {
log.Println("Gin is under debug mode")
} else {
log.Println("Gin is under prod mode")
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
// Ping test
router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
v1 := router.Group("api/v1")
{
v1.GET("/waterrecords", waterrecord.GetWaterRecords)
v1.GET("/waterrecords/:id", waterrecord.GetWaterRecord)
v1.POST("/waterrecords", waterrecord.PostWaterRecord)
v1.PUT("/waterrecords/:id", waterrecord.UpdateWaterRecord)
v1.DELETE("/waterrecords/:id", waterrecord.DeleteWaterRecord)
}
return router
}
func main() {
router := SetupRouter()
// Listen and Server in 0.0.0.0:8080
port := viper.GetString("http.port")
router.Run(":" + port)
}
References:
https://medium.com/@felipedutratine/manage-config-in-golang-to-get-variables-from-file-and-env-variables-33d876887152
https://github.com/spf13/viper
Add the Dependency to Dep
>dep ensure -add github.com/spf13/viper
All the Viper method
https://godoc.org/github.com/spf13/viper#pkg-variables
Here is How I use that, when I run the command, I can do as follow to pass the ENV
>ENVIRONMENT=PROD bin/restful_go_api
Add dependency to use Viper
>dep ensure -add github.com/spf13/viper
The Configuration style will be YAML style config-prod.yaml as follow
restful_go_api/conf/config-prod.yaml
restful_go_api/conf/config-dev.yaml
restful_go_api/conf/config-stage.yaml
http:
port: 8081
gin:
debug: false
database:
type: mysql
host: 192.168.1.109
user: watermonitor
password: xxxxxx
port: 3306
name: watermonitor
here is the config module for init the Viper
restful_go_api/src/sillycat.com/restful_go_api/config/vipconfig.go
package config
import (
"fmt"
"github.com/spf13/viper"
"log"
"os"
)
const appName = "config"
func InitViperConfig() {
log.Println("Start to init the config--------")
viper.SetDefault("http.port", "8080")
if os.Getenv("ENVIRONMENT") == "PROD" {
log.Println("system is running under PROD mode")
viper.SetConfigName(appName + "-prod") // name of config file (without extension)
} else if os.Getenv("ENVIRONMENT") == "STAGE" {
log.Println("system is running under STAGE mode")
viper.SetConfigName(appName + "-stage") // name of config file (without extension)
} else {
log.Println("system is running under DEV mode")
viper.SetConfigName(appName + "-dev") // name of config file (without extension)
}
viper.AddConfigPath("/etc/" + appName + "/") // path to look for the config file in
viper.AddConfigPath("./") // optionally look for config in the working directory
viper.AddConfigPath("./conf/")
viper.AutomaticEnv()
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
}
In the database init class, here is how I use that
restful_go_api/src/sillycat.com/restful_go_api/common/db.go
package common
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"github.com/spf13/viper"
"sillycat.com/restful_go_api/config"
)
var engine = InitDatabase()
func InitDatabase() *xorm.Engine {
config.InitViperConfig()
var engine *xorm.Engine
var err error
dbType := viper.GetString("database.type")
dbUser := viper.GetString("database.user")
dbPwd := viper.GetString("database.password")
dbHost := viper.GetString("database.host")
dbPort := viper.GetString("database.port")
dbName := viper.GetString("database.name")
engine, err = xorm.NewEngine(dbType, dbUser+":"+dbPwd+"@tcp("+dbHost+":"+dbPort+")/"+dbName+"?charset=utf8")
if err != nil {
fmt.Println(err)
}
engine.Ping() //ping
engine.ShowSQL(true) //show SQL
engine.Logger().SetLevel(core.LOG_DEBUG)
return engine
}
func GetDatabase() *xorm.Engine {
return engine
}
In the main.go, here is how I use that configuration
restful_go_api/src/sillycat.com/restful_go_api/main.go
package main
import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"log"
"sillycat.com/restful_go_api/waterrecord"
)
var DB = make(map[string]string)
func SetupRouter() *gin.Engine {
// Disable Console Color
// gin.DisableConsoleColor()
if viper.GetBool("gin.debug") {
log.Println("Gin is under debug mode")
} else {
log.Println("Gin is under prod mode")
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
// Ping test
router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
v1 := router.Group("api/v1")
{
v1.GET("/waterrecords", waterrecord.GetWaterRecords)
v1.GET("/waterrecords/:id", waterrecord.GetWaterRecord)
v1.POST("/waterrecords", waterrecord.PostWaterRecord)
v1.PUT("/waterrecords/:id", waterrecord.UpdateWaterRecord)
v1.DELETE("/waterrecords/:id", waterrecord.DeleteWaterRecord)
}
return router
}
func main() {
router := SetupRouter()
// Listen and Server in 0.0.0.0:8080
port := viper.GetString("http.port")
router.Run(":" + port)
}
References:
https://medium.com/@felipedutratine/manage-config-in-golang-to-get-variables-from-file-and-env-variables-33d876887152
https://github.com/spf13/viper
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 362Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 364Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 330Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 424Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 367Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 445VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 377Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 468NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 414Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 332Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 243GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 445GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 321GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 307Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 286Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 303Serverless with NodeJS and Tenc ...
相关推荐
`update-golang` 是一个专为Golang爱好者和开发者设计的实用脚本工具,它的主要目的是简化Go语言的更新过程,使得用户能够便捷地获取并安装最新的Golang版本,同时尽可能减少对系统的侵入性。这个工具对于那些频繁...
update-golang update-golang是一个脚本,可以轻松地获取和安装新的Golang版本,并减少系统入侵。 目录工作原理用法警告删除示例定制逐个更新update-golang update-golang是一个脚本,可轻松获取和安装新的Golang...
在`pycharm2018.3之golang插件`这个主题中,我们将探讨如何在PyCharm 2018.3版本上安装和配置Golang插件,以便在这款强大的Python IDE中编写和调试Go代码。 首先,我们需要了解为什么在PyCharm 2018.3这个特定版本...
然而,通过使用`update-golang`脚本,我们可以简化这一过程,实现快速、高效地获取并安装新的Golang版本。`update-golang`是一个专为Linux开发环境设计的实用工具,它使得Go语言版本的管理和更新变得轻而易举。 ...
标题中的"Pycharm Golang插件 jar"指的是在PyCharm这款流行的Python集成开发环境中安装Golang编程语言的扩展插件。PyCharm是由JetBrains公司开发的一款强大的IDE,它支持多种编程语言,包括Python、Go等。由于Golang...
weixin-golang-sdk 微信golang工具包
golang安装包下载
1、适合新手使用,详细的代码和举例 2、hmacMd5的方法网上资料少
golang-html5-sse-example, HTML5服务器发送的事件 Golang HTML5 SSE示例这是一个简单的例子,说明如何使用发送事件发送事件。 从服务器的角度来看,SSE几乎与长轮询相同。 客户端发出一个建立TCP连接的获取请求。 ...
5. **使用公钥解密**: 对于解密操作,你可以使用`crypto/rsa`包。 ```go pub, err := x509.ParseRSAPublicKeyFromPEM(publicKeyBytes) if err != nil { // 解析公钥失败 } decryptedData, err := rsa....
Golang 1.18.10 Windows安装包。Golang 1.18.10 Windows安装包。Golang 1.18.10 Windows安装包。Golang 1.18.10 Windows安装包。Golang 1.18.10 Windows安装包。Golang 1.18.10 Windows安装包。Golang 1.18.10 ...
开源项目-udhos-update-golang.zip,update-golang is a script to easily fetch and install new Golang releases
golang_http_client
Go语言,又称Golang,是由Google开发的一种静态类型、编译型、并发型且具有垃圾回收功能的编程语言。它设计的目标是提高开发者的生产效率,同时保持系统级编程的性能。Go语言的设计受到了C、 Pascal、 Miranda和...
在编程领域,Go语言(Golang)以其高效、简洁和并发特性受到了许多开发者的青睐。在Windows平台上,尽管Go语言最初的设计目标并非图形用户界面(GUI)开发,但随着技术的发展,已经出现了一些优秀的框架使得Go语言也...
从官方网站或者提供的"golang安装教程.zip"中的"goland-2018.1.6.tar.gz"文件下载Goland的最新版本。解压下载的文件,例如: ``` tar -zxvf goland-2018.1.6.tar.gz ``` 2. **移动到可执行目录**: 将解压后的...
5. **golang.org/x/image**:图像处理和图形库,支持多种图像格式的读写,以及图像操作,如缩放、旋转和滤波。 6. **golang.org/x/tools**:这个包是一组开发工具,包括代码分析、格式化、性能分析和Go语言服务器...
Golang Windows 1.19.1版本安装包
golang 实现海康相机抓拍,目前只支持球机。需要在代码中设置用户名和密码。 如何调试?用vscode打开,安装golang插件,即可直接调试运行。 编译与运行:运行go build命令,将HKnet.exe拷贝到build\Windows下,运行...