-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpapi.go
More file actions
42 lines (36 loc) · 957 Bytes
/
httpapi.go
File metadata and controls
42 lines (36 loc) · 957 Bytes
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
// sdns web api.
// This is used to receive commands from sdnsweb.
package main
import (
"fmt"
"strconv"
"net/http"
"github.com/gorilla/mux"
)
func HTTPRootHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Nothing here.")
}
func HTTPSetHostHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if vars["key"] != config.httpApiKey {
fmt.Fprintf(w, "error: no")
return
}
if queryLog {
fmt.Println("Set host called:", vars["hostname"], vars["dstIP"])
}
err := DefaultDomainHandler.setHost(vars["hostname"], vars["dstIP"])
if err == nil {
fmt.Fprintf(w, "ok")
} else {
fmt.Fprintf(w, "error: %v", err)
}
}
func startHTTPListener(port int) {
fmt.Printf("Starting HTTP listener on %d\n", port)
r := mux.NewRouter()
r.HandleFunc("/", HTTPRootHandler)
r.HandleFunc("/set/host/{key}/{hostname}/{dstIP}/", HTTPSetHostHandler)
http.Handle("/", r)
http.ListenAndServe(":" + strconv.Itoa(port), nil)
}