Browse Source

重建仓库,隐藏敏感信息

tangs 5 years ago
parent
commit
acec863c73
8 changed files with 154 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 8 0
      LICENSE
  3. 6 0
      config.conf
  4. 45 0
      main.go
  5. 11 0
      runUserline.sh
  6. 7 0
      src/userline/api.go
  7. 21 0
      src/userline/db.go
  8. 53 0
      src/userline/getNum.go

+ 3 - 0
.gitignore

@@ -24,3 +24,6 @@ _testmain.go
 *.test
 *.prof
 
+# Configuration files for the development environment
+.idea
+.vscode

+ 8 - 0
LICENSE

@@ -0,0 +1,8 @@
+MIT License
+Copyright (c) <year> <copyright holders>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 6 - 0
config.conf

@@ -0,0 +1,6 @@
+LOG_PATH=log/userline.log
+HOST_ADDR=
+DB_USER=userline
+DB_PASSWD=123
+DB_CONN=db.server
+DB_NAME=userline

+ 45 - 0
main.go

@@ -0,0 +1,45 @@
+package main
+
+import (
+	"fmt"
+	"github.com/tsdrm/go-tool/config"
+	"github.com/tsdrm/go-tool/log"
+	"userline"
+)
+
+func main() {
+	var conf = "config.conf"
+	var cfg = config.NewConfig()
+	var err = cfg.Config(conf)
+	if err != nil {
+		fmt.Printf("Main read config: %v error: %v", conf, err)
+		log.E("Main read config: %v error: %v", conf, err)
+		return
+	}
+	cfg.Print()
+
+	err = log.RedirectFile(cfg.String("LOG_PATH"))
+	if err != nil {
+		log.W("Main redirect log to file: %v error: %v", cfg.String("LOG_PATH"), err)
+		return
+	}
+
+	userline.HOST_ADDR = cfg.String("HOST_ADDR")
+
+	// dial to mongodb
+	err = userline.Dial(cfg.String("DB_CONN"), cfg.String("DB_NAME"))
+	if err != nil {
+		log.E("Main dial mongodb with DB_CONN: %v, DB_NAME: %v error: %v", cfg.String("DB_CONN"), cfg.String("DB_NAME"), err)
+		return
+	}
+	log.D("Main dial mongodb with DB_CONN: %v, DB_NAME: %v success", cfg.String("DB_CONN"), cfg.String("DB_NAME"))
+
+	// register http handler
+
+	log.D("Main server will start !!!")
+
+	// start
+	userline.GetNum()
+
+	log.D("Main server will stop !!!")
+}

+ 11 - 0
runUserline.sh

@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+set -e
+
+echo "#### start run usreline"
+
+export dirPath=$(cd `dirname $0`; pwd)
+echo $dirPath
+
+export GOPATH=$dirPath:$GOPATH
+
+go run main.go

+ 7 - 0
src/userline/api.go

@@ -0,0 +1,7 @@
+package userline
+
+import "net/http"
+
+func Load(w http.ResponseWriter, r *http.Response) {
+	
+}

+ 21 - 0
src/userline/db.go

@@ -0,0 +1,21 @@
+package userline
+
+import (
+	"gopkg.in/mgo.v2"
+)
+
+var C = func(name string) *mgo.Collection {
+	panic("mongodb is not init!!!")
+}
+
+func Dial(url string, dbName string) error {
+	session, err := mgo.Dial(url)
+	if err != nil {
+		return err
+	}
+
+	C = func(name string) *mgo.Collection {
+		return session.DB(dbName).C(name)
+	}
+	return nil
+}

+ 53 - 0
src/userline/getNum.go

@@ -0,0 +1,53 @@
+package userline
+
+import (
+	"fmt"
+	"github.com/tsdrm/go-tool/log"
+	"github.com/tsdrm/go-tool/util"
+	"gopkg.in/mgo.v2/bson"
+	"time"
+)
+
+var HOST_ADDR = ""
+
+func GetNum() {
+	for {
+		var minutes = time.Now().Minute()
+		if minutes%5 != 0 {
+			time.Sleep(45 * time.Second)
+			continue
+		}
+
+		var t = util.Now13() / (60 * 1000) * (60 * 1000)
+		log.D("GetNum will record the num with t: %v", t)
+
+		var err = recordNum(t)
+		if err != nil {
+			log.E("GetNum with t: %v error: %v", t, err)
+			time.Sleep(20 * time.Second)
+			continue
+		}
+		time.Sleep(time.Minute)
+	}
+}
+
+func recordNum(t int64) error {
+	var url = fmt.Sprintf("http://%v/?_data_=1", HOST_ADDR)
+	result, err := util.HTTPGetMap("%v", url)
+	if err != nil {
+		return err
+	}
+	var num = result.Int("number")
+
+	// record
+	_, err = C("active_num").Upsert(bson.M{
+		"time": t,
+	}, bson.M{
+		"$setOnInsert": bson.M{"_id": bson.NewObjectId().Hex()},
+		"$set": bson.M{
+			"num":  num,
+			"time": t,
+		},
+	})
+	return err
+}