You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

35 lines
682 B

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. )
  7. func GenShardHash(b []byte) (final byte) {
  8. for i, b := range b {
  9. final = (b ^ final ^ byte(i)) + final + byte(i) + final*byte(i)
  10. }
  11. return final
  12. }
  13. func WriteResponse(res http.ResponseWriter, statusCode int, v any) {
  14. res.Header().Set("Content-Type", "application/json")
  15. res.WriteHeader(statusCode)
  16. if statusCode == http.StatusNoContent {
  17. return
  18. }
  19. if err, isError := v.(error); isError {
  20. v = map[string]any{
  21. "error": fmt.Sprintf("%v", err),
  22. "status_code": statusCode,
  23. }
  24. } else {
  25. v = map[string]any{
  26. "data": v,
  27. "status_code": statusCode,
  28. }
  29. }
  30. json.NewEncoder(res).Encode(v)
  31. }