Skip to content

Commit

Permalink
restructure API service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulexus committed May 12, 2020
1 parent 75077ed commit 67395bf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 66 deletions.
72 changes: 72 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"context"
"encoding/json"
"log"
"net/http"
"strconv"
"strings"
)

func (s *dispatcherSets) startHTTP(ctx context.Context, addr string) {

http.HandleFunc("/check/", s.handleIPCheckRequest)
http.HandleFunc("/dispatcher/", s.handleListSetRequest)
http.HandleFunc("/dispatchers/", s.handleListSetRequest)

log.Fatalln(http.ListenAndServe(addr, nil))
}

// Check IP address for membership in a dispatcher set.
// URL: /check/<setID>/<ip>
func (s *dispatcherSets) handleIPCheckRequest(w http.ResponseWriter, r *http.Request) {
pieces := strings.Split(strings.TrimPrefix(r.URL.Path, "/check/"), "/")
if len(pieces) != 2 {
w.WriteHeader(http.StatusBadRequest)
return
}

setID, err := strconv.Atoi(pieces[0])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

if s.validateSetMember(setID, pieces[1]) {
w.WriteHeader(http.StatusOK)
return
}

w.WriteHeader(http.StatusNotFound)
return

}

// Return a given dispatcher set
// URL: /dispatcher/<setID>
func (s *dispatcherSets) handleListSetRequest(w http.ResponseWriter, r *http.Request) {
pieces := strings.Split(strings.TrimPrefix(r.URL.Path, "/dispatcher/"), "/")
if len(pieces) != 1 {
w.WriteHeader(http.StatusBadRequest)
return
}

setID, err := strconv.Atoi(pieces[0])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

selectedSet := s.getDispatcherSet(setID)
if selectedSet != nil {
w.Header().Add("Content-Type", "application/json")

if err = json.NewEncoder(w).Encode(selectedSet); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
return
}

w.WriteHeader(http.StatusNotFound)
}
68 changes: 2 additions & 66 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -157,52 +153,6 @@ func (s *dispatcherSets) maintain(ctx context.Context) error {
return ctx.Err()
}

// ServeHTTP offers a web service by which clients may validate membership of an IP address within a dispatcher set or fetch a dispatcher set
func (s *dispatcherSets) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Handle requests for /check/<setID>/<ip address> to validate membership of an IP to a dispatcher set
if strings.HasPrefix(r.URL.Path, "/check/") {
pieces := strings.Split(strings.TrimPrefix(r.URL.Path, "/check/"), "/")
if len(pieces) != 2 {
w.WriteHeader(http.StatusBadRequest)
return
}
setID, err := strconv.Atoi(pieces[0])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if s.validateSetMember(setID, pieces[1]) {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusNotFound)
return
} else if strings.HasPrefix(r.URL.Path, "/dispatcher/") { // Handle requests for /dispatcher/<setID> to fetch a dispatcher set
pieces := strings.Split(strings.TrimPrefix(r.URL.Path, "/dispatcher/"), "/")
if len(pieces) != 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
setID, err := strconv.Atoi(pieces[0])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
selectedSet := s.getDispatcherSet(setID)
if selectedSet != nil {
js, err := json.Marshal(selectedSet)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(js) //If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data
return
}
}

w.WriteHeader(http.StatusNotFound)
}

func (s *dispatcherSets) validateSetMember(id int, addr string) bool {
selectedSet, ok := s.sets[id]
if !ok {
Expand Down Expand Up @@ -298,23 +248,9 @@ func run() error {
}
})

// Run a web service to offer IP checks for each member of the dispatcher set and fetch a dispatcher set
// Run HTTP API service
if apiAddr != "" {
var srv http.Server
srv.Addr = apiAddr
srv.Handler = s

go func() {
<-ctx.Done()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalln("failed to shut down HTTP server:", err)
}
}()
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalln("failed to start HTTP server:", err)
}
}()
go s.startHTTP(ctx, apiAddr)
}

for ctx.Err() == nil {
Expand Down

0 comments on commit 67395bf

Please sign in to comment.