Ejemplo n.º 1
0
func (s *Server) Init(conf *goconfig.ConfigFile) error {
	ip, err := conf.GetString("common", "listen")
	if err != nil || ip == "" {
		s.listenIp = DEF_IP
	} else {
		s.listenIp = ip
	}
	port, err := conf.GetString("common", "port")
	if err != nil || ip == "" {
		s.listenPort = DEF_PORT
	} else {
		s.listenPort = port
	}
	rootDir, err := conf.GetString("common", "rootDir")
	if err != nil || rootDir == "" {
		return errors.New("rootDir cannot empty")
	}
	s.rootDir = strings.Split(rootDir, ",")
	maxSize, err := conf.GetString("common", "maxSize")
	if maxSize == "" {
		s.maxSize = MAX_BODY_SIZE
	} else {
		s.maxSize = min(uint32(util.ReverseFormatSize(maxSize)), MAX_BODY_SIZE)
	}
	allowExt, err := conf.GetString("common", "allowExt")
	if err != nil || allowExt == "" {
		return errors.New("allowExt cannot empty")
	}
	s.allowExt = strings.Split(allowExt, ",")

	s.password, _ = conf.GetString("common", "password")
	s.debug, _ = conf.GetBool("log", "debug")

	s.mail = new(mailConf)
	s.mail.host, _ = conf.GetString("mail", "mailHost")
	s.mail.user, _ = conf.GetString("mail", "mailUser")
	s.mail.pass, _ = conf.GetString("mail", "mailPass")
	s.mail.from, _ = conf.GetString("mail", "mailFrom")
	s.mail.to, _ = conf.GetString("mail", "mailTo")
	s.mail.title, _ = conf.GetString("mail", "mailTitle")
	s.mail.serverInfo, _ = conf.GetString("mail", "serverInfo")

	// 初始化CTFile
	s.ctFile = NewCtFile(s)

	return nil
}
Ejemplo n.º 2
0
func queue(i int64, c *goconfig.ConfigFile, ch chan int64) {
	// start rutine
	logger.Println("rutine ", i+1, " start successed.")
	log.Println("rutine ", i+1, " start successed.")
	// client: redis config
	queueName, err := c.GetString("common", "queueName")
	errHandle(err)
	redisHost, err := c.GetString("redis", "host")
	errHandle(err)
	redisPort, err := c.GetString("redis", "port")
	errHandle(err)
	//redisDb, err := c.GetInt64("redis", "db")
	//errHandle(err)
	// server: fcgi config
	fcgiHost, err := c.GetString("fcgi", "host")
	errHandle(err)
	fcgiPort, err := c.GetInt64("fcgi", "port")
	errHandle(err)
	fcgiPath, err := c.GetString("fcgi", "path")
	errHandle(err)
	// init connect
	client := client.NewClient(redisHost+":"+redisPort, 0, 0)
	server := server.NewServer(fcgiHost, int(fcgiPort))

	for shutdown == false {
		time.Sleep(100 * time.Millisecond)
		// get Content
		val, err := client.Pop(queueName)
		if err != nil || len(val) < 1 {
			// redis red no key, continue, not log
			continue
		}
		fmt.Println("rutine ", i+1, " got value: ", string(val))

		// send to Server
		data := make(map[string]string)
		data["message"] = string(val)
		res, err := server.Request(fcgiPath, data)
		if err != nil || len(res) < 1 {
			//logger.Println("queue ", i+1, " error: ", err.Error())
			log.Println("queue ", i+1, " error: ", err.Error())
			// if failed, retry, back into queue
			client.Push(queueName, val)
			continue
		}

		// parse body
		body := server.ParseBody(strings.TrimSpace(string(res)))
		fmt.Println(body)
		// parse json
		var bodyJson QueueRes
		json.Unmarshal([]byte(body), &bodyJson)
		if bodyJson.Code != 0 {
			//logger.Println("queue ", i+1, " error: ", bodyJson.message)
			log.Println("queue ", i+1, " error: ", bodyJson.Message)
			// if failed, retry, back into queue
			client.Push(queueName, val)
			continue
		}
		fmt.Println(bodyJson.Code, bodyJson.Message)
	}
	// shutdown
	client.Close()
	server.Close()
	ch <- 1
	logger.Println("rutine ", i+1, " shutdown.")
	log.Println("rutine ", i+1, " shutdown.")
}