Skip to content

Commit

Permalink
cleaning up and implementing reusable code
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Sep 22, 2023
1 parent 8b0292f commit 74a027a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
34 changes: 14 additions & 20 deletions japicore/routeHandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/uptrace/bunrouter"
)

var (
Version = "v0.0.0"
Module = "Jackal API Core"
)

func Handler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
return nil
Expand All @@ -24,24 +29,15 @@ func Handler() bunrouter.HandlerFunc {

func MethodNotAllowedHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
w.WriteHeader(http.StatusMethodNotAllowed)
warning := fmt.Sprintf("%s method not availble for \"%s\"", req.URL.Path, req.Method)

_, err := w.Write([]byte(warning))
if err != nil {
jutils.ProcessError("WWriteError for MethodNotAllowedHandler", err)
}
return nil
return jutils.ProcessCustomHttpError("MethodNotAllowedHandler", warning, 405, w)
}
}

func VersionHandler() bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
version := "v0.1.0"
_, err := w.Write([]byte(version))
if err != nil {
jutils.ProcessError("WWriteError for VersionHandler", err)
}
message := createJsonResponse("")
condensedWriteJSON(w, message)
return nil
}
}
Expand All @@ -68,10 +64,8 @@ func ImportHandler(fileIo *file_io_handler.FileIoHandler, queue *ScrapeQueue) bu

wg.Wait()

_, err = w.Write([]byte("Import complete"))
if err != nil {
jutils.ProcessError("WWriteError for ImportHandler", err)
}
message := createJsonResponse("Import complete")
condensedWriteJSON(w, message)
return nil
}
}
Expand Down Expand Up @@ -211,10 +205,8 @@ func UploadHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bu
return err
}

_, err = w.Write([]byte("uploadHandler"))
if err != nil {
jutils.ProcessError("WWriteError for UploadHandler", err)
}
message := createJsonResponse("Upload complete")
condensedWriteJSON(w, message)
return nil
}
}
Expand Down Expand Up @@ -242,6 +234,8 @@ func DeleteHandler(fileIo *file_io_handler.FileIoHandler, queue *FileIoQueue) bu
return err
}

message := createJsonResponse("Deletion complete")
condensedWriteJSON(w, message)
return nil
}
}
14 changes: 14 additions & 0 deletions japicore/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ type UploadResponse struct {
type fileScrape struct {
Targets []string `json:"targets"`
}

type jsonResponse struct {
Module string `json:"module"`
Version string `json:"version"`
Message string `json:"message"`
}

func createJsonResponse(message string) jsonResponse {
return jsonResponse{
Message: message,
Module: Module,
Version: Version,
}
}
18 changes: 18 additions & 0 deletions japicore/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package japicore

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"sync"

Expand Down Expand Up @@ -37,3 +40,18 @@ func processUpload(w http.ResponseWriter, fileIo *file_io_handler.FileIoHandler,

return m.Fid()
}

func condensedWriteJSON(w http.ResponseWriter, respVal interface{}) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(respVal); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("json.NewEncoder.Encode: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
written, err := w.Write(buf.Bytes())
if err != nil {
fmt.Printf("Written bytes: %d\n", written)
fmt.Println(err)
}
}

0 comments on commit 74a027a

Please sign in to comment.