Exemple #1
0
func (this *SysconfigController) updateSitemap(resp *helper.Response) {
	content := this.GetString("content")
	if content == "" {
		resp.Status = RS.RS_params_error
		resp.Tips(helper.WARNING, RS.RS_params_error)
		return
	}
	_, err := os.Stat(models.SiteFile)
	if err != nil && !strings.Contains(err.Error(), "no such file") {
		log.Error(err)
		return
	} else {
		os.Remove(models.SiteFile)
	}
	f, err := os.Create(models.SiteFile)
	if err != nil {
		log.Error(err)
		return
	}
	defer f.Close()
	_, err = f.WriteString(content)
	if err != nil {
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "错误|" + err.Error()}
	}
}
Exemple #2
0
func (conf *Config) UpdateConf() {
	data, err := json.Marshal(conf.SiteVerify)
	if err != nil {
		log.Error(err)
		return
	}
	err = db.Update(DB, C_CONFIG, bson.M{}, bson.M{"$set": bson.M{SITE_VERIFY: string(data)}})
	if err != nil {
		log.Error(err)
	}
}
Exemple #3
0
func DeepCopy(val interface{}, newVal interface{}) {
	data, err := bson.Marshal(val)
	if err != nil {
		log.Error("bson.Marshal: ", err)
		return
	}

	if err := bson.Unmarshal(data, newVal); err != nil {
		log.Error("bson.Unmarshal: ", err)
		return
	}
}
Exemple #4
0
func doFeed() {
	temp, err := template.ParseFiles(models.TemplateFile)
	if err != nil {
		log.Error(err)
		return
	}
	ts := models.TMgr.GetTopics()
	var Topics []*Topic
	for i, v := range ts {
		if i == 0 && v.CreateTime.Before(buildDate) {
			return
		}
		t := &Topic{}
		t.Title = v.Title
		t.URL = fmt.Sprintf("%s/%s/%d.html", controllers.Domain, v.CreateTime.Format(helper.Layout_y_m_d), v.ID)
		t.PubDate = v.CreateTime.Format(time.RFC1123Z)
		t.Author = v.Author
		t.Category = v.CategoryID
		t.Desc = v.Content
		Topics = append(Topics, t)
	}
	buildDate = time.Now()
	params := make(map[string]interface{})
	params["Title"] = models.Blogger.BlogName + "'s Blog"
	params["Domain"] = controllers.Domain
	params["Desc"] = models.Blogger.Introduce
	params["PubDate"] = buildDate.Format(time.RFC1123Z)
	params["BuildDate"] = buildDate.Format(time.RFC1123Z)
	params["Year"] = year
	params["Version"] = version
	params["Author"] = models.Blogger.BlogName
	params["Topics"] = Topics

	_, err = os.Stat(models.FeedFile)
	if err != nil && !strings.Contains(err.Error(), "no such file") {
		log.Error(err)
		return
	} else {
		os.Remove(models.FeedFile)
	}
	f, err := os.Create(models.FeedFile)
	if err != nil {
		log.Error(err)
		return
	}
	defer f.Close()
	err = temp.Execute(f, params)
	if err != nil {
		log.Error(err)
		return
	}
}
Exemple #5
0
func (b *BaseData) CleanData(t time.Time) {
	daysAgo20 := t.AddDate(0, 0, -20)
	err := db.Remove(DB, C_REQUEST, bson.M{"time": bson.M{"$lt": daysAgo20}})
	if err != nil && !strings.Contains(err.Error(), "not found") {
		log.Error(err)
	}
}
Exemple #6
0
func (this *ProxyController) Get() {
	var err error
	defer func() {
		if err != nil {
			log.Error(err)
			this.Ctx.WriteString(err.Error())
		}
	}()
	url := this.Ctx.Input.Param(":url")
	if icon := models.Icons[url]; icon != nil {
		icon.Time = time.Now()
		this.Ctx.Output.Body(icon.Data)
		return
	}
	response, err := http.Get("http://" + url)
	if err != nil {
		return
	}
	defer response.Body.Close()
	b, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return
	}
	models.Icons[url] = &models.Icon{Data: b, Time: time.Now()}
	this.Ctx.Output.Body(b)
}
Exemple #7
0
func doReadBuildVersionConfig() {
	b, err := ioutil.ReadFile(path + "/version")
	if err != nil {
		log.Error(err)
	}
	Cache.BuildVersion = string(b)
}
Exemple #8
0
func (this *TopicsController) doDeleteTopics(resp *helper.Response) {
	ids := this.GetString("ids")
	log.Debugf("%s", ids)
	if ids == "" {
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "ID错误|走正常途径哦。"}
		return
	}
	sliceID := strings.Split(ids, ",")
	for _, v := range sliceID {
		id, err := strconv.Atoi(v)
		if err != nil {
			log.Error(err)
			resp.Status = RS.RS_failed
			resp.Err = helper.Error{Level: helper.WARNING, Msg: "ID错误|走正常途径哦。"}
			return
		}
		err = models.TMgr.DelTopic(int32(id))
		if err != nil {
			resp.Status = RS.RS_failed
			resp.Err = helper.Error{Level: helper.WARNING, Msg: "删除失败|" + err.Error()}
			return
		}
	}
}
Exemple #9
0
func LoadConf() *Config {
	conf := &Config{SiteVerify: make(map[string]*Verification)}
	ms, c := db.Connect(DB, C_CONFIG)
	defer ms.Close()
	tmp := make(map[string]string)
	err := c.Find(nil).Select(bson.M{SITE_VERIFY: 1, "_id": 0}).One(tmp)
	if err != nil && !strings.Contains(err.Error(), "not found") {
		log.Error(err)
	}
	if str := tmp[SITE_VERIFY]; str != "" {
		err := json.Unmarshal([]byte(str), &conf.SiteVerify)
		if err != nil {
			log.Error(err)
		}
	}
	return conf
}
Exemple #10
0
func IsEmpty(db, collection string) bool {
	ms, c := Connect(db, collection)
	defer ms.Close()
	count, err := c.Count()
	if err != nil {
		log.Error(err)
	}
	return count == 0
}
Exemple #11
0
func (m *TopicMgr) Update() int {
	for _, topic := range m.Topics {
		err := db.Update(DB, C_TOPIC, bson.M{"id": topic.ID}, bson.M{"$set": bson.M{"pv": topic.PV}})
		if err != nil {
			log.Error(err)
			return RS.RS_update_failed
		}
	}
	return RS.RS_success
}
Exemple #12
0
func (m *RequestManage) Saver() {
	t := time.NewTicker(time.Minute * 10)
	for {
		select {
		case request := <-m.Ch:
			err := db.Insert(DB, C_REQUEST, request)
			if err != nil {
				log.Error(err)
			}

		case <-t.C:
			ManageData.loadData(TODAY)
		}
	}
}
Exemple #13
0
func KeyIsExsit(db, collection, key, value string) bool {
	ms, c := Connect(db, collection)
	defer ms.Close()

	mu.Lock()
	defer mu.Unlock()
	count, err := c.Find(bson.M{key: value}).Count()
	if err == mgo.ErrNotFound || count > 0 {
		return true
	}
	if err != nil { // 查找出错, 为了以防万一还是返回存在
		log.Error(err)
		return true
	}
	return false
}
Exemple #14
0
func NextVal(db, countername string) int32 {
	ms, c := Connect(db, "COUNTERS")
	defer ms.Close()

	change := mgo.Change{
		Update:    bson.M{"$inc": bson.M{"nextval": 1}},
		Upsert:    true,
		ReturnNew: true,
	}

	next := &Counter{}
	info, err := c.Find(bson.M{"name": countername}).Apply(change, &next)
	if err != nil {
		log.Error(info, err)
		return -1
	}

	// round the nextval to 2^31
	return int32(next.NextVal % 2147483648)
}
Exemple #15
0
func (this *SocialController) saveSocial(resp *helper.Response) {
	content := this.GetString("json")
	var sc models.Social
	err := json.Unmarshal([]byte(content), &sc)
	if err != nil {
		log.Error(err)
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "内容错误|要仔细检查哦。"}
		return
	}
	if sc.ID == "TEST" {
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "内容错误|请修改你需要添加的社交。"}
		return
	}
	if social := models.Blogger.GetSocialByID(sc.ID); social != nil {
		*social = sc
		sort.Sort(models.Blogger.Socials)
	} else {
		sc.CreateTime = time.Now()
		models.Blogger.AddSocial(&sc)
	}
}
Exemple #16
0
func (this *BlogrollController) saveBlogroll(resp *helper.Response) {
	content := this.GetString("json")
	var br models.Blogroll
	err := json.Unmarshal([]byte(content), &br)
	if err != nil {
		log.Error(err)
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "内容错误|要仔细检查哦。"}
		return
	}
	if br.ID == "TEST" {
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "内容错误|请修改你需要添加的工具。"}
		return
	}
	if blogroll := models.Blogger.GetBlogrollByID(br.ID); blogroll != nil {
		*blogroll = br
		sort.Sort(models.Blogger.Blogrolls)
	} else {
		br.CreateTime = time.Now()
		models.Blogger.AddBlogroll(&br)
	}
}
Exemple #17
0
func (this *CategoryController) saveCategory(resp *helper.Response) {
	content := this.GetString("json")
	var cat models.Category
	err := json.Unmarshal([]byte(content), &cat)
	if err != nil {
		log.Error(err)
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "内容错误|要仔细检查哦。"}
		return
	}
	if cat.ID == "TEST" {
		resp.Status = RS.RS_failed
		resp.Err = helper.Error{Level: helper.WARNING, Msg: "内容错误|请修改你需要添加的分类。"}
		return
	}
	if category := models.Blogger.GetCategoryByID(cat.ID); category != nil {
		*category = cat
		sort.Sort(models.Blogger.Categories)
	} else {
		cat.CreateTime = time.Now()
		models.Blogger.AddCategory(&cat)
	}
}
Exemple #18
0
func checkErr(err error) {
	if err != nil {
		log.Error(err)
	}
}
Exemple #19
0
func (b *BaseData) loadData(typ string) {
	b.lock.RLock()
	defer b.lock.RUnlock()
	ms, c := db.Connect(DB, C_REQUEST)
	c.EnsureIndexKey("time")
	defer ms.Close()

	now := tm.New(time.Now())
	var Begin, End time.Time
	if typ == TODAY {
		Begin = now.BeginningOfDay()
		End = now.EndOfDay()
	} else if typ == YESTERDAY {
		Begin = now.BeginningOfDay().Add(-24 * time.Hour)
		End = now.EndOfDay().Add(-24 * time.Hour)
	}
	if typ == TODAY {
		err := c.Find(nil).Sort("-time").Skip(0).Limit(pageCount).All(&b.Latest)
		if err != nil {
			log.Error(err)
		}
	}
	count, err := c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Count()
	if err != nil {
		log.Error(err)
	}
	b.PV[typ] = count
	var sessions []string
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Distinct("sessionid", &sessions)
	if err != nil {
		log.Error(err)
	}
	b.UV[typ] = len(sessions)
	var ips []string
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Distinct("remoteaddr", &ips)
	if err != nil {
		log.Error(err)
	}
	b.China = make(map[string]*Area)
	b.World = make(map[string]*Area)
	for _, v := range ips {
		info, err := ip17mon.Find(v)
		if err != nil {
			log.Warn(err)
			continue
		}
		if info.Country == "中国" {
			if city := b.China[info.City]; city == nil {
				if info.Region == "台湾" {
					b.China[info.Region] = &Area{Name: info.Region, Value: 1}
					continue
				}
				b.China[info.City] = &Area{Name: info.Region, Value: 1}
			} else {
				city.Value++
			}
		} else {
			if country := b.World[info.Country]; country == nil {
				b.World[info.Country] = &Area{Name: info.Country, Value: 1}
			} else {
				country.Value++
			}
		}
	}

	b.IP[typ] = len(ips)
	var ts []*Request
	err = c.Find(bson.M{"time": bson.M{"$gte": Begin, "$lt": End}}).Select(bson.M{"time": 1}).All(&ts)
	if err != nil {
		log.Error(err)
	}
	b.TimePV[typ] = make([]int, 145)
	for _, v := range ts {
		b.TimePV[typ][ParseTime(v.Time)]++
	}
}