forked from Juniper/jtimon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
50 lines (38 loc) · 1.03 KB
/
api.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
/*
* Copyright (c) 2018, Juniper Networks, Inc.
* All rights reserved.
*/
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func apiInit(jctx *JCtx) {
if jctx.config.API.Port == 0 {
return
}
portstr := fmt.Sprintf(":%v", jctx.config.API.Port)
jctx.pause.pch = make(chan int64)
jctx.pause.upch = make(chan struct{})
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/api/pause/{pauseTime}", jctx.pauseHandler)
router.HandleFunc("/api/unpause", jctx.upauseHandler)
log.Fatal(http.ListenAndServe(portstr, router))
}
func (jctx *JCtx) pauseHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pauseTime := vars["pauseTime"]
fmt.Printf("Pause Time: %v\n", pauseTime)
if pauseV, err := strconv.ParseInt(pauseTime, 10, 64); err != nil {
fmt.Printf("/api/pause - invalid input\n")
} else {
jctx.pause.pch <- pauseV
}
}
func (jctx *JCtx) upauseHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request for unpause\n")
jctx.pause.upch <- struct{}{}
}