-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathzcommon.go
99 lines (85 loc) · 1.81 KB
/
zcommon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gimg
import (
_ "encoding/json"
"fmt"
"github.com/gographics/imagick/imagick"
)
const (
PROJECT_VERSION = "1.0.1"
MAX_LINE = 1024
CACHE_KEY_SIZE = 128
RETRY_TIME_WAIT = 1000
CACHE_MAX_SIZE = 1048576 //1024*1024
PATH_MAX_SIZE = 512
)
type ZRequest struct {
Md5 string
ImageType string
Width int
Height int
Proportion int
Gary int
X int
Y int
Rotate int
Format string
Save int
Quality int
}
type ZImageInfo struct {
Size int `json:"size"`
Width int `json:"width"`
Height int `json:"height"`
Quality int `json:"quality"`
Format string `json:"format"`
}
type ZContext struct {
Config AppConfig
Logger *ZLogger
Cache *ZCache
Image *ZImage
Redis *ZRedisDB
}
func NewContext(cfgFile string) (*ZContext, error) {
imagick.Initialize()
cfg, err := LoadConfig(cfgFile)
if err != nil {
return nil, err
}
var log *ZLogger
logOutput := cfg.System.LogOutput
if logOutput == "file" {
log, err = NewFileLogger("gimg", 0, cfg.System.LogName)
} else if logOutput == "console" {
log, err = NewLogger("gimg", 0)
} else {
return nil, fmt.Errorf("init logger faile.")
}
if err != nil {
return nil, err
}
var cache *ZCache
if cfg.Cache.Cache == 1 {
// var client *memcache.Client
// cacheAddr := fmt.Sprintf("%s:%d", cfg.Cache.MemcacheHost, cfg.Cache.MemcachePort)
// client = memcache.New(cacheAddr)
cache = NewCache(cfg.Cache.MemcacheHost, cfg.Cache.MemcachePort)
} else {
cache = nil
}
img := NewImage()
redisDB, err := NewRedisDB(cfg.Storage.SsdbHost, cfg.Storage.SsdbPort)
if err != nil {
return nil, err
}
return &ZContext{Config: cfg,
Logger: log,
Cache: cache,
Image: img,
Redis: redisDB,
}, nil
}
func (z *ZContext) Release() {
z.Logger.Close()
imagick.Terminate()
}