Committing API code, and clean up.

This commit is contained in:
Henry Camacho 2017-03-13 11:43:28 -05:00
parent 74591fd150
commit b88817e4d1
5 changed files with 121 additions and 61 deletions

17
rvpn/api/setup.go Normal file
View File

@ -0,0 +1,17 @@
package api
import (
"log"
"os"
)
var (
loginfo *log.Logger
logdebug *log.Logger
logFlags = log.Ldate | log.Lmicroseconds | log.Lshortfile
)
func init() {
loginfo = log.New(os.Stdout, "INFO: api: ", logFlags)
logdebug = log.New(os.Stdout, "DEBUG: api:", logFlags)
}

View File

@ -0,0 +1,25 @@
package genericlistener
import "net/http"
type apiEndPoint struct {
pack string
endpoint string
method func(w http.ResponseWriter, r *http.Request)
}
type APIEndPoints struct {
endPoint map[string]*apiEndPoint
}
//NewAPIEndPoints -- Constructor
func NewAPIEndPoints() (p *APIEndPoints) {
p = new(apiEndPoints)
p.endPoint = make(map[string]*apiEndPoint)
return
}
func (p *apiEndPoints) add(pack string, endpoint string, method func(w http.ResponseWriter, r *http.Request)) {
router.HandleFunc("/api/"+rDNSPackageName+"servers", apiServers)
}

View File

@ -0,0 +1,79 @@
package genericlistener
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
)
const (
rDNSPackageName = "com.daplie.rvpn"
)
var connectionTable *Table
//handleAdminClient -
// - expecting an existing oneConnListener with a qualified wss client connected.
// - auth will happen again since we were just peeking at the token.
func handleAdminClient(ctx context.Context, oneConn *oneConnListener) {
connectionTable = ctx.Value(ctxConnectionTable).(*Table)
router := mux.NewRouter().StrictSlash(true)
endpoints := make(map[string]string)
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
loginfo.Println("HandleFunc /")
switch url := r.URL.Path; url {
case "/":
// check to see if we are using the administrative Host
if strings.Contains(r.Host, "rvpn.daplie.invalid") {
http.Redirect(w, r, "/admin", 301)
}
default:
http.Error(w, "Not Found", 404)
}
})
router.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintln(w, "<html>Welcome..press <a href=/api/servers>Servers</a> to access stats</html>")
})
router.HandleFunc("/api/"+rDNSPackageName+"servers", apiServers)
s := &http.Server{
Addr: ":80",
Handler: router,
}
err := s.Serve(oneConn)
if err != nil {
loginfo.Println("Serve error: ", err)
}
select {
case <-ctx.Done():
loginfo.Println("Cancel signal hit")
return
}
}
func apiServers(w http.ResponseWriter, r *http.Request) {
fmt.Println("here")
serverContainer := NewServerAPIContainer()
for c := range connectionTable.Connections() {
serverAPI := NewServerAPI(c)
serverContainer.Servers = append(serverContainer.Servers, serverAPI)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(w).Encode(serverContainer)
}

View File

@ -5,8 +5,6 @@ import (
"context"
"crypto/tls"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net"
"strconv"
@ -337,65 +335,6 @@ func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname
}
}
//handleAdminClient -
// - expecting an existing oneConnListener with a qualified wss client connected.
// - auth will happen again since we were just peeking at the token.
func handleAdminClient(ctx context.Context, oneConn *oneConnListener) {
connectionTable := ctx.Value(ctxConnectionTable).(*Table)
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
loginfo.Println("HandleFunc /")
switch url := r.URL.Path; url {
case "/":
// check to see if we are using the administrative Host
if strings.Contains(r.Host, "rvpn.daplie.invalid") {
http.Redirect(w, r, "/admin", 301)
}
default:
http.Error(w, "Not Found", 404)
}
})
router.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintln(w, "<html>Welcome..press <a href=/api/servers>Servers</a> to access stats</html>")
})
router.HandleFunc("/api/servers", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("here")
serverContainer := NewServerAPIContainer()
for c := range connectionTable.Connections() {
serverAPI := NewServerAPI(c)
serverContainer.Servers = append(serverContainer.Servers, serverAPI)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(w).Encode(serverContainer)
})
s := &http.Server{
Addr: ":80",
Handler: router,
}
err := s.Serve(oneConn)
if err != nil {
loginfo.Println("Serve error: ", err)
}
select {
case <-ctx.Done():
loginfo.Println("Cancel signal hit")
return
}
}
//handleWssClient -
// - expecting an existing oneConnListener with a qualified wss client connected.
// - auth will happen again since we were just peeking at the token.