-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjob.go
45 lines (37 loc) · 1.06 KB
/
cronjob.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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/kokhang/distributed-cronjob/manager"
"github.com/kokhang/distributed-cronjob/models"
)
func GetJobEndpoint(w http.ResponseWriter, req *http.Request) {
params := mux.Vars(req)
for _, item := range models.Jobs {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&models.Job{})
}
func ListJobsEndpoint(w http.ResponseWriter, req *http.Request) {
json.NewEncoder(w).Encode(models.Jobs)
}
func CreateJobEndpoint(w http.ResponseWriter, req *http.Request) {
var job models.Job
_ = json.NewDecoder(req.Body).Decode(&job)
// TODO validate(job)
manager.ScheduleNewJob(job)
json.NewEncoder(w).Encode(models.Jobs)
}
func main() {
router := mux.NewRouter()
manager.InitWorkers()
router.HandleFunc("/jobs", ListJobsEndpoint).Methods("GET")
router.HandleFunc("/jobs/{id}", GetJobEndpoint).Methods("GET")
router.HandleFunc("/jobs", CreateJobEndpoint).Methods("POST")
log.Fatal(http.ListenAndServe(":30000", router))
}