-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
169 lines (138 loc) · 4.17 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/minio/minio-go/v7"
"github.com/rs/xid"
"github.com/spf13/viper"
"github.com/gleanerio/gleaner/internal/common"
"github.com/gleanerio/gleaner/internal/millers"
"github.com/gleanerio/gleaner/internal/summoner"
)
var viperVal string
// var setupVal bool // not implemented in web version yet
// Profile is a struct for holding results tot be sent to the
// user about the index request
type Profile struct {
ID string
QLen int32
Qpos int32
}
// MyServer struct for mux router
type MyServer struct {
r *mux.Router
}
func init() {
log.SetFlags(log.Lshortfile)
// log.SetOutput(ioutil.Discard) // turn off all logging
// flag.BoolVar(&setupVal, "setup", false, "Run Gleaner configuration check and exit")
flag.StringVar(&viperVal, "cfg", "config", "Configuration file")
}
func main() {
log.Println("EarthCube Gleaner (Web)")
flag.Parse() // parse any command line flags...
htmlRouter := mux.NewRouter()
htmlRouter.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./static"))))
http.Handle("/", &MyServer{htmlRouter})
jp := mux.NewRouter()
jp.HandleFunc("/job", MakeJob).Methods("POST")
http.Handle("/job", &MyServer{jp})
jg := mux.NewRouter()
jg.HandleFunc("/job/{ID}", CheckJob).Methods("GET")
http.Handle("/job/", &MyServer{jg})
log.Printf("Listening on 9900. Go to http://127.0.0.1:9900/")
err := http.ListenAndServe(":9900", nil)
// http 2.0 http.ListenAndServeTLS(":443", "server.crt", "server.key", nil)
if err != nil {
log.Fatal(err)
}
}
func readConfig(filename string, defaults map[string]interface{}) (*viper.Viper, error) {
v := viper.New()
for key, value := range defaults {
v.SetDefault(key, value)
}
v.SetConfigName(filename)
v.AddConfigPath(".")
v.AutomaticEnv()
err := v.ReadInConfig()
return v, err
}
// MakeJob generates a new go func call for the indexing
func MakeJob(w http.ResponseWriter, r *http.Request) {
var v1 *viper.Viper
var err error
guid := xid.New()
// Viper config
v1, err = readConfig("./config", map[string]interface{}{
"sources": map[string]string{
"name": fmt.Sprintf("%s_samplesearth", guid.String()), // error.. will overwrite others.. guid+name
"url": "https://samples.earth/sitemap.xml",
"headless": "false",
},
"gleaner": map[string]string{
"runid": fmt.Sprintf("%s_web", guid.String()),
"summon": "true",
"mill": "true",
},
})
if err != nil {
panic(fmt.Errorf("error when reading config: %v", err))
}
log.Println(v1)
//vars := mux.Vars(r)
b, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
log.Println(string(b))
// make the return struct
profile := Profile{guid.String(), 1, 1}
js, err := json.Marshal(profile)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
mc := common.MinioConnection(v1)
go cli(mc, v1) // this is a hack.. need a semaphore system here...
w.WriteHeader(http.StatusAccepted)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// func cli(mc *minio.Client, cs utils.Config) {
func cli(mc *minio.Client, v1 *viper.Viper) {
mcfg := v1.GetStringMapString("gleaner")
if mcfg["summon"] == "true" {
summoner.Summoner(mc, v1)
}
if mcfg["mill"] == "true" {
millers.Millers(mc, v1) // need to remove rundir and then fix the compile
}
}
// CheckJob will return the status of a job
func CheckJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["ID"]
fmt.Fprintf(w, "Job check: %s\n", id)
}
func (s *MyServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
rw.Header().Set("Access-Control-Allow-Headers",
"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
// Let the Gorilla work
s.r.ServeHTTP(rw, req)
}
func addDefaultHeaders(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fn(w, r)
}
}