summary refs log tree commit diff stats
path: root/TODO
diff options
context:
space:
mode:
Diffstat (limited to 'TODO')
-rw-r--r--TODO13
1 files changed, 13 insertions, 0 deletions
diff --git a/TODO b/TODO
new file mode 100644
index 00000000..44ebe6e9
--- /dev/null
+++ b/TODO
@@ -0,0 +1,13 @@
+Console
+
+   ( ) #0   09/12/06  console commands
+   ( ) #1   09/12/06  quick find
+   ( ) #2   09/12/06  open with
+   ( ) #3   09/12/06  MVC for widgets
+   ( ) #4   09/12/06  history for console
+
+
+General
+
+   ( ) #5   09/12/06  move code from fm into objects
+   ( ) #6   09/12/06  move main to fm
n Morrison <ben@gbmor.dev> 2019-05-21 04:13:21 -0400 committer Ben Morrison <ben@gbmor.dev> 2019-05-21 04:13:21 -0400 database push/pull functions' href='/gbmor/getwtxt/commit/cache.go?h=v0.1.1&id=c896e6b85ff32c3c0c45336de8b75752a31574ca'>c896e6b ^
c896e6b ^
d6d0c18 ^




c896e6b ^

c896e6b ^
d6d0c18 ^




c896e6b ^

711012e ^

c896e6b ^












c896e6b ^

d6d0c18 ^
c38385c ^
c896e6b ^





d6d0c18 ^
c896e6b ^
8b6ab99 ^
c896e6b ^
55c8ffa ^
c896e6b ^



8b6ab99 ^
cd635e6 ^


c896e6b ^

b29e1c1 ^

c896e6b ^

b29e1c1 ^
c896e6b ^
b29e1c1 ^
4f0847b ^
c38385c ^
b29e1c1 ^

4f0847b ^
b29e1c1 ^
c896e6b ^
b29e1c1 ^

c896e6b ^

b29e1c1 ^
c896e6b ^
8b6ab99 ^
c896e6b ^
b29e1c1 ^
c896e6b ^
8b6ab99 ^
c896e6b ^
c896e6b ^



cd635e6 ^


c896e6b ^



c896e6b ^

b29e1c1 ^
c896e6b ^



bded77f ^

c896e6b ^
bded77f ^
2053db4 ^

c896e6b ^
bded77f ^
a6f47c0 ^
bded77f ^
c38385c ^

c896e6b ^
bded77f ^











bded77f ^




c896e6b ^
bded77f ^
bded77f ^
c38385c ^
bded77f ^
c896e6b ^


bded77f ^
c896e6b ^

c896e6b ^






c896e6b ^
2b0d4a5 ^









25972a6 ^









f316396 ^

25972a6 ^

2b0d4a5 ^
25972a6 ^





2b0d4a5 ^
25972a6 ^


2b0d4a5 ^
2b0d4a5 ^










1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211


            
               
                   
             
             
            






                                             
                            




                                                                       

 
                         




                                                                   

 

                                           












                                                                                        

                     
                             
                                         





                                               
                               
 
                                   
                                                 
                                                        



                                                                                                               
                                     


                                      

 

                                              

                           
                    
 
                             
                                       
                                            

                                                               
                                                                    
                                                               
                                            

                                                                         

                 
                               
 
                                   
                                                 
                                                                    
         
                                     
 



                                                       


                                     



                  

                      
                    



                                        

                                           
 
                                                

                                 
 
                                     
                                                  
                                             

                                                                
                         











                                                          




                                                                                  
                         
 
                                            
                                                     
                                              


                                                  
                                                                                  

                                                    






                                                                                   
 









                                                   









                                                           

                                       

                                         
 





                                                                                
 


                                                          
 










                                                               
package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"net"
	"os"
	"strings"
	"time"

	"github.com/getwtxt/registry"
	"github.com/syndtr/goleveldb/leveldb"
)

func checkCacheTime() bool {
	confObj.Mu.RLock()
	answer := time.Since(confObj.LastCache) > confObj.CacheInterval
	confObj.Mu.RUnlock()

	return answer
}

func checkDBtime() bool {
	confObj.Mu.RLock()
	answer := time.Since(confObj.LastPush) > confObj.DBInterval
	confObj.Mu.RUnlock()

	return answer
}

// Launched by init as a coroutine to watch
// for the update intervals to pass.
func cacheAndPush() {
	for {
		if checkCacheTime() {
			refreshCache()
		}
		if checkDBtime() {
			if err := pushDatabase(); err != nil {
				log.Printf("Error pushing cache to database: %v\n", err)
			}
		}
	}
}

func refreshCache() {

	twtxtCache.Mu.RLock()
	for k := range twtxtCache.Users {
		err := twtxtCache.UpdateUser(k)
		if err != nil {
			log.Printf("%v\n", err)
			continue
		}
	}
	twtxtCache.Mu.RUnlock()

	remoteRegistries.Mu.RLock()
	for _, v := range remoteRegistries.List {
		err := twtxtCache.CrawlRemoteRegistry(v)
		if err != nil {
			log.Printf("Error while refreshing local copy of remote registry user data: %v\n", err)
		}
	}
	remoteRegistries.Mu.RUnlock()
	confObj.Mu.Lock()
	confObj.LastCache = time.Now()
	confObj.Mu.Unlock()
}

// Pushes the registry's cache data to a local
// database for safe keeping.
func pushDatabase() error {
	db := <-dbChan
	dbChan <- db

	twtxtCache.Mu.RLock()
	var dbBasket = &leveldb.Batch{}
	for k, v := range twtxtCache.Users {
		dbBasket.Put([]byte(k+"*Nick"), []byte(v.Nick))
		dbBasket.Put([]byte(k+"*URL"), []byte(v.URL))
		dbBasket.Put([]byte(k+"*IP"), []byte(v.IP.String()))
		dbBasket.Put([]byte(k+"*Date"), []byte(v.Date))
		for i, e := range v.Status {
			rfc := i.Format(time.RFC3339)
			dbBasket.Put([]byte(k+"*Status*"+rfc), []byte(e))
		}
	}
	twtxtCache.Mu.RUnlock()

	remoteRegistries.Mu.RLock()
	for k, v := range remoteRegistries.List {
		dbBasket.Put([]byte("remote*"+string(k)), []byte(v))
	}
	remoteRegistries.Mu.RUnlock()

	if err := db.Write(dbBasket, nil); err != nil {
		return err
	}

	confObj.Mu.Lock()
	confObj.LastPush = time.Now()
	confObj.Mu.Unlock()

	return nil
}

func pullDatabase() {
	db := <-dbChan
	dbChan <- db

	iter := db.NewIterator(nil, nil)

	for iter.Next() {
		key := string(iter.Key())
		val := string(iter.Value())

		split := strings.Split(key, "*")
		urls := split[0]
		field := split[1]

		if urls != "remote" {
			data := registry.NewUser()
			twtxtCache.Mu.RLock()
			if _, ok := twtxtCache.Users[urls]; ok {
				data = twtxtCache.Users[urls]
			}
			twtxtCache.Mu.RUnlock()

			switch field {
			case "IP":
				data.IP = net.ParseIP(val)
			case "Nick":
				data.Nick = val
			case "URL":
				data.URL = val
			case "Date":
				data.Date = val
			case "Status":
				thetime, err := time.Parse(time.RFC3339, split[2])
				if err != nil {
					log.Printf("%v\n", err)
				}
				data.Status[thetime] = val
			}

			twtxtCache.Mu.Lock()
			twtxtCache.Users[urls] = data
			twtxtCache.Mu.Unlock()

		} else {
			remoteRegistries.Mu.Lock()
			remoteRegistries.List = append(remoteRegistries.List, val)
			remoteRegistries.Mu.Unlock()
		}
	}

	iter.Release()
	err := iter.Error()
	if err != nil {
		log.Printf("Error while pulling DB into registry cache: %v\n", err)
	}
}

// pingAssets checks if the local static assets
// need to be re-cached. If they do, they are
// pulled back into memory from disk.
func pingAssets() {

	cssStat, err := os.Stat("assets/style.css")
	if err != nil {
		log.Printf("%v\n", err)
	}

	indexStat, err := os.Stat("assets/tmpl/index.html")
	if err != nil {
		log.Printf("%v\n", err)
	}

	indexMod := staticCache.indexMod
	cssMod := staticCache.cssMod

	if !indexMod.Equal(indexStat.ModTime()) {
		tmpls = initTemplates()

		var b []byte
		buf := bytes.NewBuffer(b)

		confObj.Mu.RLock()
		err = tmpls.ExecuteTemplate(buf, "index.html", confObj.Instance)
		confObj.Mu.RUnlock()
		if err != nil {
			log.Printf("%v\n", err)
		}

		staticCache.index = buf.Bytes()
		staticCache.indexMod = indexStat.ModTime()
	}

	if !cssMod.Equal(cssStat.ModTime()) {

		css, err := ioutil.ReadFile("assets/style.css")
		if err != nil {
			log.Printf("%v\n", err)
		}

		staticCache.css = css
		staticCache.cssMod = cssStat.ModTime()
	}
}