diff --git a/handle_bin_inspect.go b/handle_bin_inspect.go new file mode 100644 index 0000000..7eeab0d --- /dev/null +++ b/handle_bin_inspect.go @@ -0,0 +1,27 @@ +package main + +import ( + "net/http" +) + +func (s *server) handleBinInspect() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + binID := r.URL.Path[len("/inspect/"):] + + bin, exists := s.db.FindBin(binID) + + if !exists { + http.NotFound(w, r) + return + } + + s.renderTemplate(w, "inspect", &bin) + return + default: + http.NotFound(w, r) + return + } + } +} diff --git a/handle_bin_inspect_test.go b/handle_bin_inspect_test.go new file mode 100644 index 0000000..af9a2e1 --- /dev/null +++ b/handle_bin_inspect_test.go @@ -0,0 +1,57 @@ +package main + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/matryer/is" +) + +func TestBinInspect(t *testing.T) { + is := is.New(t) + + srv, err := newServer() + srv.db.Connect() + defer srv.db.Disconnect() + is.NoErr(err) // newServer error + + hsrv := httptest.NewServer(srv) + defer hsrv.Close() + + req, err := http.NewRequest("POST", hsrv.URL+"/new/", nil) + is.NoErr(err) // NewRequest error + client := &http.Client{ + Timeout: 1 * time.Minute, + } + + resp, err := client.Do(req) + is.NoErr(err) // Client request error + is.Equal(resp.StatusCode, 200) // Making new bin gives 200 + + inspectURL := hsrv.URL + "/inspect/" + binID := resp.Request.URL.String()[len(inspectURL):] + + var reqBody = []byte(`{"request":"Golang test request"}`) + req, err = http.NewRequest("POST", hsrv.URL+"/r/"+binID, bytes.NewBuffer(reqBody)) + is.NoErr(err) // New Request error + req.Header.Set("X-Custom-Header", "custom value 2468") + req.Header.Set("Content-type", "application/json") + resp, err = client.Do(req) + is.NoErr(err) // Client request error + is.Equal(resp.StatusCode, 200) // Initial request returns 200 + + req, err = http.NewRequest("GET", inspectURL+binID, nil) + is.NoErr(err) // New Request error + resp, err = client.Do(req) + is.NoErr(err) // Client request error + is.Equal(resp.StatusCode, 200) // Inspect returns 200 + body, err := ioutil.ReadAll(resp.Body) + is.NoErr(err) // body read error + is.True(strings.Contains(string(body), "custom value 2468")) + is.True(strings.Contains(string(body), "Golang test request")) +} diff --git a/handle_bin_new_test.go b/handle_bin_new_test.go index 8558ddb..e16b784 100644 --- a/handle_bin_new_test.go +++ b/handle_bin_new_test.go @@ -14,6 +14,9 @@ func TestBinNew(t *testing.T) { is := is.New(t) srv, err := newServer() + srv.db.Connect() + defer srv.db.Disconnect() + is.NoErr(err) // newServer error hsrv := httptest.NewServer(srv) diff --git a/handle_rekwest.go b/handle_rekwest.go new file mode 100644 index 0000000..43f3896 --- /dev/null +++ b/handle_rekwest.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "net/http" +) + +func (s *server) handleRequest() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + binID := r.URL.Path[len("/r/"):] + fmt.Println("Request made: ", binID) + + if err := s.db.AddRekwest(binID, r); err == nil { + fmt.Fprintf(w, "Request saved. (ip: %s)", r.RemoteAddr) + } else { + http.NotFound(w, r) + } + } +} diff --git a/main.go b/main.go index e664dc7..3f19ac3 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,10 @@ package main import ( "flag" "fmt" + "html/template" "net/http" "os" "path/filepath" - "text/template" ) func main() { @@ -30,6 +30,9 @@ func run(args []string) error { return err } + srv.db.Connect() + defer srv.db.Disconnect() + fmt.Printf("Rekwest Bin listening on :%d\n", *port) return http.ListenAndServe(addr, srv) } @@ -52,8 +55,6 @@ func newServer() (*server, error) { } func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { - s.db.Connect() - defer s.db.Disconnect() s.mux.ServeHTTP(w, r) } @@ -63,68 +64,9 @@ func (s *server) handleIndex() http.HandlerFunc { } } -/* -func main() { - db_controller.Connect() - defer db_controller.Disconnect() - - http.HandleFunc("/r/", binHandler) - http.HandleFunc("/", rootHandler) - log.Fatal(http.ListenAndServe(":8080", nil)) -} - -func rootHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "") -} - -func fixIPAddress(r *http.Request) { - var ipAddress string - var ipSources = []string{ - r.Header.Get("True-Client-IP"), - r.Header.Get("True-Real-IP"), - r.Header.Get("X-Forwarded-For"), - r.Header.Get("X-Originating-IP"), - } - - for _, ip := range ipSources { - if ip != "" { - ipAddress = ip - break - } - } - - if ipAddress != "" { - r.RemoteAddr = ipAddress - } -} - -func binHandler(w http.ResponseWriter, r *http.Request) { - if r.Method == "POST" { - bin, _ := db_controller.NewBin() - - http.Redirect(w, r, "/r/"+bin.BinId+"?inspect", 302) - return - } - - binID := r.URL.Path[len("/r/"):] - binAddress := fmt.Sprintf("http://%s/r/%s", r.Host, binID) - - if r.URL.RawQuery == "inspect" { - bin, exists := db_controller.FindBin(binID) - - if !exists { - http.NotFound(w, r) - return - } - - renderTemplate(w, "inspect", &bin) - } else { -} - -func renderTemplate(writer http.ResponseWriter, tmpl string, bin *db_controller.Bin) { - err := templates.ExecuteTemplate(writer, tmpl+".html", bin) +func (s *server) renderTemplate(writer http.ResponseWriter, tmpl string, bin *Bin) { + err := s.tmpl.ExecuteTemplate(writer, tmpl+".html", bin) if err != nil { http.Error(writer, err.Error(), http.StatusInternalServerError) } } -*/ diff --git a/public/index.html b/public/index.html index c5a7c1b..8c7f68e 100644 --- a/public/index.html +++ b/public/index.html @@ -8,7 +8,7 @@