func main() { port, err := kernel.GetSetting(settings, "PORT") checkError(err) host, err := kernel.GetSetting(settings, "HOST") checkError(err) log.Printf("server run on %s:%s", host, port) http.Handle("/", routers.Register(&g)) err = http.ListenAndServe(fmt.Sprintf("%s:%s", host, port), nil) checkError(err) }
func mountDB() *sql.DB { dbinfo, err := kernel.GetSetting(settings, "DBINFO") checkError(err) db, _ = sql.Open("mysql", dbinfo) db.SetMaxOpenConns(30) db.SetMaxIdleConns(30) checkError(db.Ping()) return db }
func mountRedis() *redis.Client { addr, err := kernel.GetSetting(settings, "REDISADDR") checkError(err) passwd, err := kernel.GetSetting(settings, "REDISPASSWORD") checkError(err) redisdb, err := kernel.GetSetting(settings, "REDISDB") checkError(err) db, err := strconv.ParseInt(redisdb, 10, 64) checkError(err) cc = redis.NewClient(&redis.Options{ Addr: addr, Password: passwd, // no password set DB: db, // use default DB }) _, e := cc.Ping().Result() checkError(e) return cc }
func init() { runtime.GOMAXPROCS(runtime.NumCPU()) //加载配置文件 loadConf() debug, e := kernel.GetSetting(settings, "DEBUG") checkError(e) //挂载数据库 mountDB() //挂载redis mountRedis() dict, e1 := kernel.GetSetting(settings, "DICT") checkError(e1) stop, e2 := kernel.GetSetting(settings, "STOP") checkError(e2) //初始化分词, 采用引擎自带分词器 seg := segmenter.InitChinaCut(dict) //初始化搜索引擎 var searcher search.Engine //获取mongodb集合数 indextmp, e3 := kernel.GetSetting(settings, "INDEXSTORENUM") checkError(e3) indexstorenum, err := strconv.Atoi(indextmp) checkError(err) //获取mongodb库名 mongodbname, e4 := kernel.GetSetting(settings, "MONGODBNAME") checkError(e4) //获取mongodb连接url mongodburl, e5 := kernel.GetSetting(settings, "MONGODBURL") checkError(e5) //获取mongodb集合前缀 collectionprefix, e5 := kernel.GetSetting(settings, "COLLECTIONPREFIX") checkError(e5) searcher.Init(search.EngineInitOptions{ //分词器采用引擎自带的分词器 Segmenter: seg, StopTokenFile: stop, UsePersistentStorage: func() bool { if debug == "True" { return false } else { return true } }(), IndexerInitOptions: &search.IndexerInitOptions{ IndexType: search.LocationsIndex, BM25Parameters: &search.BM25Parameters{ K1: 2.0, B: 0.75, }, }, //pipline采用引擎自带的mongo pipline SearchPipline: pipeline.InitMongo( mongodbname, indexstorenum, mongodburl, collectionprefix), //索引器接口实现,采用自带的wukong索引器 CreateIndexer: func() search.SearchIndexer { return indexer.NewWuKongIndexer() }, //排序器生成方法 CreateRanker: func() search.SearchRanker { return ranker.NewWuKongRanker() }, }) g = kernel.G{ //可以处理的http方法字典 Ml: map[string]string{ "GET": "Get", "POST": "Post", "OPTIONS": "Options", "HEAD": "Head", "PUT": "Put", "DELETE": "Delete", "CONNECT": "Connect", }, Init: initHandler, DefaultHandler: defaultHandler, End: endHandler, DB: db, CC: cc, Settings: settings, DIY: map[string]interface{}{ "seg": seg, "searcher": searcher, }, } }