From 0f8eab4e1157e83f39cd7298378bb5cc9ddb913a Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Tue, 23 Jun 2020 19:12:08 +0200 Subject: [feat] first simplistic implementation of a rest api It lacks authentication, and I need to debug a segfault/abort when quitting. But kind of useful for demonstration purposes. --- pkg/backend/api.go | 4 ++++ pkg/backend/webapi.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 pkg/backend/webapi.go (limited to 'pkg/backend') diff --git a/pkg/backend/api.go b/pkg/backend/api.go index 64dac9d..f63962c 100644 --- a/pkg/backend/api.go +++ b/pkg/backend/api.go @@ -76,6 +76,10 @@ func EnableMockBackend() { go enableMockBackend() } +func EnableWebAPI() { + go enableWebAPI() +} + /* these two are a bit redundant since we already add them to ctx. however, we want to have them available before everything else, to be able to parse cli arguments. In the long run, we probably want to move all vendoring to qt, so diff --git a/pkg/backend/webapi.go b/pkg/backend/webapi.go new file mode 100644 index 0000000..e3918c5 --- /dev/null +++ b/pkg/backend/webapi.go @@ -0,0 +1,35 @@ +package backend + +import ( + "fmt" + "log" + "net/http" +) + +func webOn(w http.ResponseWriter, r *http.Request) { + log.Println("Web UI: on") + SwitchOn() +} + +func webOff(w http.ResponseWriter, r *http.Request) { + log.Println("Web UI: off") + SwitchOff() +} + +func webStatus(w http.ResponseWriter, r *http.Request) { + log.Println("Web UI: status") + fmt.Fprintf(w, ctx.Status.String()) +} + +func webQuit(w http.ResponseWriter, r *http.Request) { + log.Println("Web UI: quit") + Quit() +} + +func enableWebAPI() { + http.HandleFunc("/vpn/start", webOn) + http.HandleFunc("/vpn/stop", webOff) + http.HandleFunc("/vpn/status", webStatus) + http.HandleFunc("/vpn/quit", webQuit) + http.ListenAndServe(":8080", nil) +} -- cgit v1.2.3