-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidmaker.go
219 lines (187 loc) · 5.08 KB
/
idmaker.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/signal"
"os/user"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
type Client struct {
Ip string
CallTime string
ReturnTime string
}
type SeqId struct {
id int32
mu sync.RWMutex
}
type IdMaker struct {
SeqId SeqId
}
type Response struct {
id int32
code int
message string
}
func PrettyClient(c Client) {
fmt.Println(fmt.Sprintf("请求对象信息,ip:%s, 调用时间:%s", c.Ip, c.CallTime))
}
func PrettyLockClient(c Client) {
fmt.Println(fmt.Sprintf("请求对象信息,ip:%s, 调用时间:%s, 已经获取到锁正在处理", c.Ip, c.CallTime))
}
func PrettyClientReturn(c Client, id int32) {
fmt.Println(fmt.Sprintf("请求对象信息,ip:%s, 调用时间:%s, 已经处理完毕释放锁,获取到值时间:%s, 返回序列id:%d", c.Ip, c.CallTime, c.ReturnTime, id))
}
func (r *Response) Str() string {
return fmt.Sprintf("{'id':%d,'code':%d,'message':%s}", r.id, r.code, r.message)
}
func (im *IdMaker) GetSeqId() *SeqId {
im.SeqId.mu.RLocker()
seq := im.SeqId
im.SeqId.mu.RUnlock()
return &seq
}
func (im *IdMaker) GetNewSeqId(c Client) *SeqId {
PrettyClient(c)
im.SeqId.mu.Lock()
PrettyLockClient(c)
im.SeqId.id += 1
seq := im.SeqId
im.Record()
im.SeqId.mu.Unlock()
c.ReturnTime = time.Now().String()
PrettyClientReturn(c, seq.id)
return &seq
}
var idMaker = &IdMaker{SeqId: SeqId{id: 0}}
func GetIp(r *http.Request) string {
ip := r.Header.Get("X-Real-IP")
if len(ip) == 0 {
ip = r.Header.Get("X-Forwarded-For")
}
return ip
}
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, (&Response{id: idMaker.GetNewSeqId(Client{
Ip: GetIp(r), CallTime: time.Now().String(),
}).id, code: 200, message: "success"}).Str())
}
func PathExist(_path string) bool {
_, err := os.Stat(_path)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
func Load() {
if !PathExist(PathJoin()) {
return
}
bytes, err := ioutil.ReadFile(PathJoin())
if err != nil {
panic(err)
}
if len(string(bytes)) != 0 {
contentId, err := strconv.ParseInt(string(bytes), 10, 32)
if err != nil {
panic(err)
}
idMaker.SeqId.id = int32(contentId)
}
}
func homeUnix() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
// If that fails, try the shell
var stdout bytes.Buffer
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
result := strings.TrimSpace(stdout.String())
if result == "" {
return "", errors.New("blank output when reading home directory")
}
return result, nil
}
var idMakerFile = "idMaker.txt"
func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home := drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}
return home, nil
}
func GetHomePath() (string, string) {
user, err := user.Current()
if nil == err {
return user.HomeDir, "linux"
}
// cross compile support
if "windows" == runtime.GOOS {
pathWindows, _ := homeWindows()
return pathWindows, "windows"
}
// Unix-like system, so just assume Unix
pathUnix, _ := homeUnix()
return pathUnix, "unix"
}
func PathJoin() string {
dir, osType := GetHomePath()
switch osType {
case "windows":
return dir + "\\" + idMakerFile
default:
return dir + "/" + idMakerFile
}
}
func (im *IdMaker) Record() {
id := im.SeqId.id
fmt.Println("------------------------------------------------------------------------------------")
fmt.Println(fmt.Sprintf("program execution done, record id : %d, time : %s", id, time.Now().String()))
fmt.Println("------------------------------------------------------------------------------------")
ioutil.WriteFile(PathJoin(), []byte(strconv.Itoa(int(id))), 0664)
}
func BeautyExit(ch chan os.Signal) {
fmt.Println("------------------------------------------------------------------------------------")
fmt.Println("program execution begin , listening health...")
for s := range ch {
fmt.Println("------------------------------------------------------------------------------------")
fmt.Println(fmt.Sprintf("program execution exit, receive signal:%s", s))
fmt.Println("------------------------------------------------------------------------------------")
idMaker.Record()
}
}
func main() {
fmt.Println(fmt.Sprintf("receive out param: port: %s, path: %s", os.Args[1],os.Args[2]))
Load()
http.HandleFunc(os.Args[2], index)
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
go BeautyExit(ch)
err1 := http.ListenAndServe(":" + os.Args[1], nil)
if err1 != nil {
fmt.Println(err1)
}else {
fmt.Println(fmt.Sprintf("program execution started, please curl url: http://localhost:%s%s",os.Args[1],os.Args[2]))
}
fmt.Println("------------------------------------------------------------------------------------")
}