-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapphandler.go
151 lines (121 loc) · 3.2 KB
/
webapphandler.go
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
package main
import (
"encoding/json"
"github.com/httpreserve/httpreserve"
"log"
"sync"
"time"
)
// This structure is used to communicate with the server
// we may also use some static storage in the form of Bolt DB
// the final signal to the webapp will be a empty payload
// but with the complete flag set to true so that we know there
// is no more work to be processed. ls contains a link stat
// data structure if we can recreate one from the JSON we receive
// else the js variable will contain a single JSON document to
// be processed.
type processLog struct {
complete bool
ls httpreserve.LinkStats
js string
lmap map[string]interface{}
}
func clockOut() string {
t := time.Now()
return t.Format("Mon Jan _2 15:04:05 2006")
}
// webapprun lets us start the server for the user to access
func webappRun() {
//defer serverWG.Done()
// pause to let our buffers begin to fill...
// TODO: look for safer, more idiomatic ways to solve...
//time.Sleep(3 * time.Second)
log.Println("Server starting on localhost: http://127.0.0.1:2041")
if port == "" {
port = "2041"
}
err := DefaultServer(port)
if err != nil {
log.Println(err)
}
}
var lpcopyfrom, lpcopyto int
var processedSlices []processLog
func processlinkpool(wg *sync.WaitGroup) {
defer wg.Done()
res := tsdatacopy(&lpcopyfrom, &lpcopyto, linkpool)
if len(res) > 0 {
var ls httpreserve.LinkStats
for x := range res {
ce := res[x]
err := json.Unmarshal([]byte(ce), &ls)
if err != nil {
log.Println("Problem unmarshalling data.", err)
}
// retrieve a map from the structure and write it out to the
// http server...
lmap := storeStruct(ls, ce)
if len(lmap) > 0 {
var ps processLog
ps.js = ce
ps.ls = ls
ps.lmap = lmap
processedSlices = append(processedSlices, ps)
}
}
if pscomplete {
var ps processLog
ps.complete = true
processedSlices = append(processedSlices, ps)
}
}
}
var linkpool []string
func makelinkpool(js string, wg *sync.WaitGroup) {
defer wg.Done()
linkpool = append(linkpool, js)
}
// temporary webappHandler that gets the app working reliably...
func webappHandler(js string) {
wg := new(sync.WaitGroup)
wg.Add(1)
go makehtmpool(js, wg)
wg.Wait()
return
}
var htmpool []processLog
func makehtmpool(js string, wg *sync.WaitGroup) {
defer wg.Done()
var ls httpreserve.LinkStats
err := json.Unmarshal([]byte(js), &ls)
if err != nil {
log.Println("Problem unmarshalling data.", err)
return
}
// retrieve a map from the structure and write it out to the
// http server...
lmap := storeStruct(ls, js)
if len(lmap) > 0 {
var ps processLog
ps.js = js
ps.ls = ls
ps.lmap = lmap
htmpool = append(htmpool, ps)
}
log.Println("processed record:", len(htmpool))
return
}
// webappHanlder enables us to establish the web server and create
// the structures we need to present our data to the user...
func concurrentversionNotWorkingWebappHandler(js string) {
wg := new(sync.WaitGroup)
//TODO: Understand the essence of wait groups where the ordering
//of these, slightly backwards works... does processlinkpool just
//wait for makelinkpool????
wg.Add(1)
go processlinkpool(wg)
wg.Add(1)
go makelinkpool(js, wg)
wg.Wait()
return
}