71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/bootdotdev/learn-cicd-starter/internal/database"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (cfg *apiConfig) handlerUsersCreate(w http.ResponseWriter, r *http.Request) {
|
|
type parameters struct {
|
|
Name string `json:"name"`
|
|
}
|
|
decoder := json.NewDecoder(r.Body)
|
|
params := parameters{}
|
|
err := decoder.Decode(¶ms)
|
|
if err != nil {
|
|
respondWithError(w, http.StatusInternalServerError, "Couldn't decode parameters")
|
|
return
|
|
}
|
|
|
|
apiKey, err := generateRandomSHA256Hash()
|
|
if err != nil {
|
|
respondWithError(w, http.StatusInternalServerError, "Couldn't gen apikey")
|
|
return
|
|
}
|
|
|
|
err = cfg.DB.CreateUser(r.Context(), database.CreateUserParams{
|
|
ID: uuid.New().String(),
|
|
CreatedAt: time.Now().UTC(),
|
|
UpdatedAt: time.Now().UTC(),
|
|
Name: params.Name,
|
|
ApiKey: apiKey,
|
|
})
|
|
if err != nil {
|
|
log.Println(err)
|
|
respondWithError(w, http.StatusInternalServerError, "Couldn't create user")
|
|
return
|
|
}
|
|
|
|
user, err := cfg.DB.GetUserByAPIKey(r.Context(), apiKey)
|
|
if err != nil {
|
|
log.Println(err)
|
|
respondWithError(w, http.StatusInternalServerError, "Couldn't get user")
|
|
return
|
|
}
|
|
|
|
respondWithJSON(w, http.StatusOK, databaseUserToUser(user))
|
|
}
|
|
|
|
func generateRandomSHA256Hash() (string, error) {
|
|
randomBytes := make([]byte, 32)
|
|
_, err := rand.Read(randomBytes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
hash := sha256.Sum256(randomBytes)
|
|
hashString := hex.EncodeToString(hash[:])
|
|
return hashString, nil
|
|
}
|
|
|
|
func (cfg *apiConfig) handlerUsersGet(w http.ResponseWriter, r *http.Request, user database.User) {
|
|
respondWithJSON(w, http.StatusOK, databaseUserToUser(user))
|
|
}
|