-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.go
77 lines (66 loc) · 1.37 KB
/
wechat.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
package main
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/boltdb/bolt"
"github.com/cfkxzsat/wechat-reply/wechat"
)
func init() {
//读取数据文件,初始化数据库
file, err := os.Open(wechat.SnippetPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
reader := bufio.NewReader(file)
db, er := bolt.Open(wechat.DBName, 0666, nil)
defer db.Close()
if er != nil {
log.Fatal(er)
}
count := 0
for {
snippet, errr := reader.ReadString('`')
if errr == io.EOF {
break
} else if errr != nil {
log.Fatal(err)
}
snippet = strings.Trim(snippet, "`\r\n")
db.Update(func(tx *bolt.Tx) error {
b, _ := tx.CreateBucketIfNotExists(wechat.DBBucket)
err := b.Put([]byte(strconv.Itoa(count)), []byte(snippet))
if err != nil {
log.Fatal(err)
}
return nil
})
count++
}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(wechat.DBBucket)
status := b.Stats()
fmt.Println(status.KeyN)
return nil
})
// fmt.Println("main's init")
}
func main() {
wechat.Initialize()
//for test
// go wechat.PrintMsg()
// fmt.Print("1") //\r make 1 overwrite some data
log.Println("Wechat Service: Start!")
http.HandleFunc("/", wechat.ProcRequest)
err := http.ListenAndServe(":80", nil)
if err != nil {
log.Fatal("Wechat Service: ListenAndServe failed, ", err)
}
log.Println("Wechat Service: Stop!")
}