-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
69 lines (63 loc) · 2.1 KB
/
app.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
package main
import (
"log"
"strings"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/HaoyuHu/gosimhash-doc-server/handler"
"github.com/HaoyuHu/gosimhash-doc-server/config"
"github.com/HaoyuHu/gosimhash-doc-server/common"
"os"
)
func main() {
commonConf := config.GetCommonConfig()
redisConf := config.GetRedisConfig()
if commonConf == nil || redisConf == nil {
log.Fatal("Cannot initialize common and redis configurations")
}
host, hostExists := os.LookupEnv("HOST")
port, portExists := os.LookupEnv("PORT")
if !hostExists {
value, exists := commonConf["host"]
if exists {
host = value.(string)
hostExists = exists
}
}
if !portExists {
value, exists := commonConf["port"]
if exists {
port = value.(string)
portExists = exists
}
}
if !hostExists || !portExists {
log.Fatal("Incorrect host or port in env or common.json")
}
addr := strings.Join([]string{host, port}, ":")
router := httprouter.New()
router.POST("/simhash", handler.Simhash)
router.POST("/distance", handler.Distance)
router.POST("/identify", handler.IdentifyDoc)
hashType := commonConf["hash_type"]
hmm := commonConf["hmm_dict"]
idf := commonConf["idf_dict"]
user := commonConf["user_dict"]
stopWords := commonConf["stop_words"]
limit := commonConf["simhash_limit"]
if hashType == nil || hmm == nil || idf == nil || user == nil || stopWords == nil || limit == nil {
log.Fatal("Incorrect hash_type or hmm_dict or idf_dict or user_dict or stop_words or limit in common.json")
}
common.InitializeSimhasher(common.HashType(uint8(hashType.(float64))), "", hmm.(string), user.(string), idf.(string), stopWords.(string))
redisHost := redisConf["host"]
redisPort := redisConf["port"]
redisPasswd := redisConf["passwd"]
if redisHost == nil || redisPort == nil || redisPasswd == nil {
log.Fatal("Incorrect host or port or passwd in redis.json")
}
err := common.InitializeRedisCache(common.SimhashLimit(int(limit.(float64))), redisHost.(string), int(redisPort.(float64)), redisPasswd.(string))
if err != nil {
log.Fatal("Initialize redis cache failed.")
}
log.Fatal(http.ListenAndServe(addr, router))
}