-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
81 changed files
with
13,496 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package cfg | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const ( | ||
DefaultWorkDir = "/usr/local/pprofparser" | ||
DefaultCfgName = "conf.yml" | ||
DefaultCfgPath = DefaultWorkDir + "/" + DefaultCfgName | ||
) | ||
|
||
const ( | ||
EnvLocal = "local" | ||
EnvDev = "dev" | ||
EnvTest = "test" | ||
EnvPre = "pre" | ||
EnvProduction = "prod" | ||
) | ||
|
||
var ( | ||
Cfg *Config | ||
) | ||
|
||
func Load(file string) error { | ||
reader, err := os.Open(file) | ||
if err != nil { | ||
var wd string | ||
if wd, err = os.Getwd(); err == nil { | ||
cfgFile := filepath.Join(wd, "cfg", DefaultCfgName) | ||
reader, err = os.Open(cfgFile) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("read config file fail: %w", err) | ||
} | ||
} | ||
defer func() { | ||
_ = reader.Close() | ||
}() | ||
|
||
decoder := yaml.NewDecoder(reader) | ||
var cfg Config | ||
if err := decoder.Decode(&cfg); err != nil { | ||
return fmt.Errorf("decode yaml config file fail: %w", err) | ||
} | ||
Cfg = &cfg | ||
return nil | ||
} | ||
|
||
type Config struct { | ||
Serv Server `yaml:"server"` | ||
Log Log `yaml:"log"` | ||
Gin Gin `yaml:"gin"` | ||
Oss Oss `yaml:"oss"` | ||
Storage Storage `yaml:"storage"` | ||
} | ||
|
||
// Server configuration | ||
type Server struct { | ||
Addr string `yaml:"addr"` | ||
Port string `yaml:"port"` | ||
} | ||
|
||
// Log log configuration | ||
type Log struct { | ||
Path string `yaml:"path"` | ||
File string `yaml:"file"` | ||
Level string `yaml:"level"` | ||
} | ||
|
||
// Gin gin configuration | ||
type Gin struct { | ||
RunMode string `yaml:"run_mode"` | ||
Log string `yaml:"log"` | ||
ErrorLog string `yaml:"error_log"` | ||
} | ||
|
||
// Oss aliyun oss configuration | ||
type Oss struct { | ||
Host string `yaml:"host"` | ||
AccessKey string `yaml:"access_key"` | ||
SecretKey string `yaml:"secret_key"` | ||
ProfileBucket string `yaml:"profile_bucket"` | ||
ProfileDir string `yaml:"profile_dir"` | ||
} | ||
|
||
type Disk struct { | ||
ProfileDir string `yaml:"profile_dir"` | ||
} | ||
|
||
type Storage struct { | ||
Disk Disk `yaml:"disk"` | ||
Oss Oss `yaml:"oss"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cfg | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestInitConfig(t *testing.T) { | ||
err := Load("testdata/conf.yml") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Printf("%+#v\n", Cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# ============= PPROF PARSER CONFIG ============= | ||
server: | ||
addr: 0.0.0.0 | ||
port: 9634 | ||
|
||
log: | ||
path: /usr/local/pprofparser | ||
file: main.log | ||
level: info | ||
|
||
gin: | ||
log: /usr/local/pprofparser/gin.log | ||
error_log: /usr/local/pprofparser/gin_error.log | ||
|
||
storage: | ||
disk: | ||
profile_dir: '/data/profile' |
Oops, something went wrong.