Browse Source

reloading tags and manufacturers periodically

Klaas, Wilfried 5 years ago
parent
commit
7edf704d55

+ 72 - 52
schematic-service-go/dao/mongodao.go

@@ -223,6 +223,66 @@ func (m *MongoDAO) initIndexManufacturers() {
 }
 
 func (m *MongoDAO) initTags() {
+	m.reloadTags()
+}
+
+func (m *MongoDAO) initManufacturers() {
+	m.reloadManufacturers()
+}
+
+func (m *MongoDAO) initUsers() {
+	m.reloadUsers()
+
+	go func() {
+		background := time.NewTicker(userReloadPeriod)
+		for _ = range background.C {
+			m.reloadUsers()
+			m.reloadTags()
+			m.reloadManufacturers()
+		}
+	}()
+}
+
+func (m *MongoDAO) reloadUsers() {
+	ctx, _ := context.WithTimeout(context.Background(), timeout)
+	usersCollection := m.database.Collection(usersCollectionName)
+	cursor, err := usersCollection.Find(ctx, bson.M{})
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer cursor.Close(ctx)
+	localUsers := make(map[string]string)
+	for cursor.Next(ctx) {
+		var user bson.M
+		if err = cursor.Decode(&user); err != nil {
+			log.Fatal(err)
+		} else {
+			username := strings.ToLower(user["name"].(string))
+			password := user["password"].(string)
+			localUsers[username] = BuildPasswordHash(password)
+		}
+	}
+	m.users = localUsers
+	if len(m.users) == 0 {
+		admin := model.User{
+			Name:     "w.klaas@gmx.de",
+			Password: "akteon0000",
+			Admin:    true,
+		}
+		m.AddUser(admin)
+		guest := model.User{
+			Name:     "gast",
+			Password: "gast1234",
+			Admin:    false,
+			Guest:    true,
+		}
+		m.AddUser(guest)
+	}
+}
+
+func (m *MongoDAO) reloadTags() {
+	myTags := model.NewTags()
+
 	ctx, _ := context.WithTimeout(context.Background(), timeout)
 	tagsCollection := m.database.Collection(tagsCollectionName)
 	cursor, err := tagsCollection.Find(ctx, bson.M{})
@@ -252,12 +312,14 @@ func (m *MongoDAO) initTags() {
 			log.Println(err)
 		}
 		//log.Printf("tag: %s has %d schematics.", tagName, n)
-		m.tags.Add(tagName, int(n))
+		myTags.Add(tagName, int(n))
 	}
-
+	m.tags = myTags
 }
 
-func (m *MongoDAO) initManufacturers() {
+func (m *MongoDAO) reloadManufacturers() {
+	myManufacturer := model.NewManufacturers()
+
 	ctx, _ := context.WithTimeout(context.Background(), timeout)
 	manufacturersCollection := m.database.Collection(manufacturersCollectionName)
 	cursor, err := manufacturersCollection.Find(ctx, bson.M{})
@@ -287,56 +349,9 @@ func (m *MongoDAO) initManufacturers() {
 			log.Println(err)
 		}
 		//log.Printf("manufacturer: %s has %d schematics.", manuName, n)
-		m.manufacturers.Add(manuName, int(n))
-	}
-}
-
-func (m *MongoDAO) initUsers() {
-	m.reloadUsers()
-
-	go func() {
-		background := time.NewTicker(userReloadPeriod)
-		for _ = range background.C {
-			m.reloadUsers()
-		}
-	}()
-}
-
-func (m *MongoDAO) reloadUsers() {
-	ctx, _ := context.WithTimeout(context.Background(), timeout)
-	usersCollection := m.database.Collection(usersCollectionName)
-	cursor, err := usersCollection.Find(ctx, bson.M{})
-	if err != nil {
-		log.Fatal(err)
-	}
-	defer cursor.Close(ctx)
-	localUsers := make(map[string]string)
-	for cursor.Next(ctx) {
-		var user bson.M
-		if err = cursor.Decode(&user); err != nil {
-			log.Fatal(err)
-		} else {
-			username := user["name"].(string)
-			password := user["password"].(string)
-			localUsers[username] = BuildPasswordHash(password)
-		}
-	}
-	m.users = localUsers
-	if len(m.users) == 0 {
-		admin := model.User{
-			Name:     "w.klaas@gmx.de",
-			Password: "akteon0000",
-			Admin:    true,
-		}
-		m.AddUser(admin)
-		guest := model.User{
-			Name:     "gast",
-			Password: "gast1234",
-			Admin:    false,
-			Guest:    true,
-		}
-		m.AddUser(guest)
+		myManufacturer.Add(manuName, int(n))
 	}
+	m.manufacturers = myManufacturer
 }
 
 // AddFile adding a file to the storage, stream like
@@ -656,6 +671,7 @@ func (m *MongoDAO) GetManufacturersCount() int {
 
 // CheckUser checking username and password... returns true if the user is active and the password for this user is correct
 func (m *MongoDAO) CheckUser(username string, password string) bool {
+	username = strings.ToLower(username)
 	pwd, ok := m.users[username]
 	if ok {
 		if pwd == password {
@@ -684,6 +700,7 @@ func (m *MongoDAO) CheckUser(username string, password string) bool {
 
 // GetUser getting the usermolde
 func (m *MongoDAO) GetUser(username string) (model.User, bool) {
+	username = strings.ToLower(username)
 	ctx, _ := context.WithTimeout(context.Background(), timeout)
 	usersCollection := m.database.Collection(usersCollectionName)
 	var user model.User
@@ -728,6 +745,7 @@ func (m *MongoDAO) AddUser(user model.User) error {
 	if user.Name == "" {
 		return errors.New("username should not be empty")
 	}
+	user.Name = strings.ToLower(user.Name)
 	_, ok := m.users[user.Name]
 	if ok {
 		return errors.New("username already exists")
@@ -751,6 +769,7 @@ func (m *MongoDAO) DeleteUser(username string) error {
 	if username == "" {
 		return errors.New("username should not be empty")
 	}
+	username = strings.ToLower(username)
 	_, ok := m.users[username]
 	if !ok {
 		return errors.New("username not exists")
@@ -773,6 +792,7 @@ func (m *MongoDAO) ChangePWD(username string, newpassword string, oldpassword st
 	if username == "" {
 		return errors.New("username should not be empty")
 	}
+	username = strings.ToLower(username)
 	pwd, ok := m.users[username]
 	if !ok {
 		return errors.New("username not registered")

+ 2 - 2
schematic-service-go/devdata/mongo.txt

@@ -1,5 +1,5 @@
-use schematic
-db.createUser({ user: "schematic", pwd: "schematic", roles: [ "readWrite", "dbAdmin", { role: "dbOwner", db: "schematic" } ]})
+use schematics
+db.createUser({ user: "schematic", pwd: "schematic", roles: [ "readWrite", "dbAdmin", { role: "dbOwner", db: "schematics" } ]})
 
 db.runCommand({compact: "attachments.files"})
 db.runCommand({compact: "attachments.chunks"})

+ 45 - 1
schematic-service-go/model/tag.go

@@ -2,16 +2,19 @@ package model
 
 import "go.mongodb.org/mongo-driver/bson/primitive"
 
+//Tag a single tag
 type Tag struct {
 	ID    primitive.ObjectID `json:"-" bson:"_id,omitempty"`
 	Name  string             `json:"name" bson:"name,omitempty"`
 	Count int                `json:"count" bson:"count,omitempty"`
 }
 
+//Tags tag list object
 type Tags struct {
 	List []Tag
 }
 
+//NewTags creates a new tag list
 func NewTags() Tags {
 	m := Tags{
 		List: make([]Tag, 0),
@@ -31,10 +34,51 @@ func (m *Tags) Contains(name string) bool {
 
 //Add adding a new manufacturer to the list
 func (m *Tags) Add(name string, count int) {
-	if !m.Contains(name) {
+	tag, ok := m.Get(name)
+	if !ok {
 		m.List = append(m.List, Tag{
 			Name:  name,
 			Count: count,
 		})
+	} else {
+		if tag.Count != count {
+			m.Remove(name)
+			m.List = append(m.List, Tag{
+				Name:  name,
+				Count: count,
+			})
+		}
+	}
+}
+
+//Remove remove a single tag
+func (m *Tags) Remove(name string) {
+	index := m.Index(name)
+	m.List[index] = m.List[len(m.List)-1]
+	m.List = m.List[:len(m.List)-1]
+}
+
+//Index getting a tag index
+func (m *Tags) Index(name string) int {
+	for i, a := range m.List {
+		if a.Name == name {
+			return i
+		}
 	}
+	return -1
+}
+
+//Get getting a tag
+func (m *Tags) Get(name string) (Tag, bool) {
+	for _, a := range m.List {
+		if a.Name == name {
+			return a, true
+		}
+	}
+	return Tag{}, false
+}
+
+//Cleear clearing the list
+func (m *Tags) Clear() {
+	m.List = nil
 }