Files
openfsd/web/util.go
Reese Norris 95a30294f4 add data API calls
- /api/v1/data/status.txt
- /api/v1/data/servers.json
- /api/v1/data/servers.txt
2024-10-13 10:53:59 -07:00

34 lines
645 B
Go

package web
import (
"bytes"
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io"
"net/http"
)
func writeResponseError(w http.ResponseWriter, status int, resp any) {
w.WriteHeader(status)
if respBytes, err := json.Marshal(&resp); err == nil {
io.Copy(w, bytes.NewReader(respBytes))
}
}
func generateRandomPassword() (string, error) {
randBytes := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, randBytes); err != nil {
return "", err
}
return hex.EncodeToString(randBytes), nil
}
func getEtag(str string) string {
sum := sha1.Sum([]byte(str))
sumSlice := sum[:]
return hex.EncodeToString(sumSlice)
}