Skip to content

Commit

Permalink
utils.go removed and merged into appropriate files: 'bad practice'
Browse files Browse the repository at this point in the history
  • Loading branch information
n0rrman committed May 26, 2024
1 parent adb2aa4 commit 2ff4ee3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Subhub is a small WebSub hub demo implemented in Go using echo, http/net, and Po
**db.go** holds all Postgres logic: connecting to the database, creating tables, and making queries. All SQL and database code is contained within this file.


**utils.go** contains a few general utility functions, such as random hex string generator, sha256 hasher, and random advice fetcher.

### Flow
The following diagrams shows the main action flow through the hub, from client to database. The three columns represent the different code files and how they interract.

Expand Down
46 changes: 46 additions & 0 deletions hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package main

import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -203,3 +209,43 @@ func sendContent(url string, secret string, topic string, content string) bool {
// Returns true of succesful, otherwise false
return resp.StatusCode == 200
}

// Random hex string generator
//
// Returns a random 64 byte hex string
func generateChallenge() string {
buf := make([]byte, 64)
_, err := rand.Read(buf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
c := hex.EncodeToString(buf)
return c
}

// SHA256 hasher of provided content using secret as key
//
// Returns sha256 hash for content using provided secret
func getHash(content []byte, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write(content)
hash := hex.EncodeToString(h.Sum(nil))
return hash
}

// Random advice generator
//
// Fetches random advice
// Returns random advice from https://api.adviceslip.com
func getRandomAdvice() string {
resp, _ := http.Get("https://api.adviceslip.com/advice")

var data map[string]interface{}
json.NewDecoder(resp.Body).Decode(&data)
advice, _ := data["slip"].(map[string]interface{})["advice"].(string)

defer resp.Body.Close()
return advice
}
51 changes: 0 additions & 51 deletions hub/utils.go

This file was deleted.

0 comments on commit 2ff4ee3

Please sign in to comment.