cleaning up code smells

This commit is contained in:
dtookey 2023-08-01 10:29:57 -04:00
parent ca65acbea1
commit 5c7143ffd7
4 changed files with 24 additions and 30 deletions

View File

@ -36,12 +36,7 @@ func createMethodHandler(m *map[string]VinegarHandlerFunction) VinegarHandlerFun
if exists {
fn(w, req)
} else {
SendApiError(
w,
200,
404,
"Method ["+req.Method+"] does not exist for endpoint["+req.URL.Path+"].",
)
http.NotFound(w, req)
}
}
}

View File

@ -38,7 +38,7 @@ type (
func NewServlet(port string) *VinegarWebServlet {
errs := make(map[int]*TemplateRoute)
srv := VinegarWebServlet{Port: port, ErrorRoutes: errs, interrupts: make(chan struct{})}
srv := VinegarWebServlet{Port: port, ErrorRoutes: errs, interrupts: make(chan struct{}, 500)}
return &srv
}
@ -58,7 +58,7 @@ func (s *VinegarWebServlet) AddErrorRoute(code int, route *TemplateRoute) {
func (s *VinegarWebServlet) ServeHTTP(w http.ResponseWriter, req *http.Request) {
err := s.Router.RouteRequest(w, req)
if err != nil {
s.SendError(w, req, 404, "Resource not found", err)
http.NotFound(w, req)
}
}
@ -79,8 +79,8 @@ func (s *VinegarWebServlet) Start() error {
}
}()
log.Printf("Listening on [%s]\n", s.Port)
for {
running := true
for running {
select {
case err := <-s.errors:
log.Printf("server on port %s failed: %v", s.Port, err)
@ -90,10 +90,11 @@ func (s *VinegarWebServlet) Start() error {
if err != nil {
return err
}
return nil
}
running = false
default:
time.Sleep(5)
}
}
return nil
}
@ -113,30 +114,23 @@ func (s *VinegarWebServlet) SendError(w http.ResponseWriter, req *http.Request,
if exists {
err := tmpl.TemplateManager.AddMixin("code", strconv.Itoa(code))
if err != nil {
writeGeneric(w, code, msg)
http.Error(w, msg, code)
return
}
err = tmpl.TemplateManager.AddMixin("msg", msg)
if err != nil {
writeGeneric(w, code, msg)
http.Error(w, msg, code)
return
}
w.WriteHeader(code)
bitties, err := tmpl.TemplateManager.RenderTemplate(fmt.Sprintf("%d.html", code))
_, err = w.Write([]byte(bitties))
if err != nil {
writeGeneric(w, code, msg)
http.Error(w, msg, code)
return
}
return
}
writeGeneric(w, code, msg)
http.Error(w, msg, code)
return
}
func writeGeneric(w http.ResponseWriter, code int, msg string) {
w.WriteHeader(code)
errorPayload := ErrorResponse{Code: code, Message: msg}
genericError, _ := json.Marshal(errorPayload)
w.Write(genericError)
}

View File

@ -1,6 +1,7 @@
package servlet
import (
"fmt"
"io"
"log"
"net/http"
@ -37,14 +38,19 @@ func TestServletServeTraffic(t *testing.T) {
srv.Router.AddRoute(&VinegarWebRoute{
Pattern: regexp.MustCompile("/test"),
Handler: func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
fmt.Printf("We got into the handler\n")
msg := []byte("hello")
l, err := w.Write(msg)
fmt.Printf("Wrote %d bytes\n", l)
if err != nil {
srv.errors <- err
}
},
})
// Start the server in a goroutine
go srv.Start()
defer srv.Shutdown()
time.Sleep(10 * time.Second)
// Make a request to the test route
res, err := http.Get("http://localhost:8080/test")
if err != nil {
@ -59,7 +65,7 @@ func TestServletServeTraffic(t *testing.T) {
body, _ := io.ReadAll(res.Body)
res.Body.Close()
if string(body) != "hello" {
t.Errorf("Unexpected response body %s", string(body))
t.Errorf("Unexpected response body [%s]", string(body))
}
}

View File

@ -157,7 +157,7 @@ func createSingleFileServletFunction(route *FileRoute) VinegarHandlerFunction {
}
if !exists {
route.srv.SendError(w, req, 404, "File not found.", errors.New("could not find file: "+route.fileRoot))
http.NotFound(w, req)
return
}
@ -195,8 +195,7 @@ func createCompressibleFileServletFunction(route *FileRoute, basePattern string,
if !exists {
content, err := util.GetDiskContent(resourcePath)
if err != nil {
route.srv.SendError(w, req, 404, "Couldn't find your content.", errors.New("could not find valid file at ["+resourcePath+"]"))
return
http.NotFound(w, req)
}
if route.UseCache {
@ -253,7 +252,7 @@ func createUncompressedFileServletFunction(route *FileRoute, basePattern string,
return
} else {
route.srv.SendError(w, req, 404, "Couldn't find your content.", errors.New("could not find file for ["+stub+"]"))
http.NotFound(w, req)
}
}
return fun