This repository was archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
135 lines (115 loc) · 3.17 KB
/
server.go
File metadata and controls
135 lines (115 loc) · 3.17 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
// "github.com/urfave/negroni"
)
const (
GET = "GET"
POST = "POST"
PUT = "PUT"
)
var (
// Channels for IPC
chanFromMeasure chan RMQMessage = make(chan RMQMessage)
chanTriggerScheduler chan string = make(chan string)
mainRouter *mux.Router
)
func respondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
// json.NewEncoder(w).Encode(data)
s, err := json.MarshalIndent(data, "", " ")
if err == nil {
w.Write(s)
}
}
func handlerClauses(w http.ResponseWriter, r *http.Request) {
if r.Method == GET {
clauses := PrintClauses()
respondJSON(w, http.StatusOK, clauses)
} else if r.Method == POST {
r.ParseForm()
clause := r.Form.Get("clause")
log.Printf(clause)
AddClause(clause)
respondJSON(w, http.StatusOK, clause)
}
}
func handlerSenses(w http.ResponseWriter, r *http.Request) {
if r.Method == GET {
memory := PrintMemory()
respondJSON(w, http.StatusOK, memory)
} else if r.Method == POST {
r.ParseForm()
subject := r.Form.Get("subject")
value := r.Form.Get("value")
log.Printf("%s %s", subject, value)
// Memorize(subject, value)
respondJSON(w, http.StatusOK, subject+value)
} else if r.Method == "DELETE" {
log.Print("hit DELETE")
r.ParseForm()
subject := r.Form.Get("subject")
ClearMemory(subject)
respondJSON(w, http.StatusOK, subject)
}
}
func handlerGoals(w http.ResponseWriter, r *http.Request) {
if r.Method == GET {
// clauses := PrintClauses()
// respondJSON(w, http.StatusOK, clauses)
Test()
} else if r.Method == POST {
log.Printf("hit POST")
respondJSON(w, http.StatusOK, "")
} else if r.Method == PUT {
log.Printf("hit PUT")
// mReader, err := r.MultipartReader()
// if err != nil {
// respondJSON(w, http.StatusOK, "ERROR")
// }
yamlFile, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
}
var goal Goal
_ = yaml.Unmarshal(yamlFile, &goal)
log.Printf("%v", goal)
RegisterGoal(goal)
chanTriggerScheduler <- "api server"
respondJSON(w, http.StatusOK, "")
}
}
func createRouter() {
log.Printf("initializing...")
mainRouter = mux.NewRouter()
r := mainRouter
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"id": "Local scheduler"}`)
})
api := r.PathPrefix("/api/v1").Subrouter()
api.Handle("/kb/rules", http.HandlerFunc(handlerClauses)).Methods(http.MethodGet, http.MethodPost)
api.Handle("/kb/senses", http.HandlerFunc(handlerSenses)).Methods(http.MethodGet, http.MethodPost, http.MethodDelete)
api.Handle("/goals", http.HandlerFunc(handlerGoals)).Methods(http.MethodGet, http.MethodPost, http.MethodPut)
log.Fatalln(http.ListenAndServe("0.0.0.0:8080", r))
}
func main() {
dryRun := flag.Bool("dry-run", false, "To emulate scheduler")
flag.Parse()
InitializeKB()
InitializeK3s()
if !*dryRun {
InitializeMeasureCollector("localhost:5672")
go RunMeasureCollector(chanFromMeasure)
}
go RunScheduler(chanTriggerScheduler, dryRun)
go RunKnowledgebase(chanFromMeasure, chanTriggerScheduler)
createRouter()
}