Scavenger Hunt API helps the react native application, On The Hunt, communicate with the database.
- Description
- Example Code
- Technology Used
- Setting up for the Application
- Main Features
- Features in Progress
- Contact Information
- Link to Frontend Repo
Scavenger Hunt API was built with Golang using gorm and mux, and a PSQL database. There are 5 tables within the API creating two many to many relationships. Each table comes with handlers to deal with the various request coming from On The Hunt. Auth was built into the app using JWT tokens and bcrypt.
This handler takes in a Hunt List ID and several Item ids and creates a row for each one in the Selected Items table.
func newSelectedItem(w http.ResponseWriter, r *http.Request) {
setupResponse(&w, r)
switch r.Method {
case "OPTIONS":
w.WriteHeader(http.StatusOK)
return
case "POST":
reqBody, _ := ioutil.ReadAll(r.Body)
var incomingItems IncomingItems
json.Unmarshal(reqBody, &incomingItems)
items := incomingItems.HuntItemIDs
var selectedItems []SelectedItem
for i := 0; i < len(items); i++ {
var selectedItem SelectedItem
selectedItem = SelectedItem{
HuntListID: incomingItems.HuntListID,
HuntItemID: incomingItems.HuntItemIDs[i],
}
db.Create(&selectedItem)
selectedItems = append(selectedItems, selectedItem)
}
json.NewEncoder(w).Encode(&selectedItems)
default:
http.Error(w, http.StatusText(405), 405)
}
This function generates a JWT token to be sent to a user after that successfully login or signup.
func generateJWT() (string, error) {
mySigningKey := []byte(os.Getenv("SECRET"))
token := jwt.New(jwt.SigningMethodHS256)
claims := token.Claims.(jwt.MapClaims)
claims["authorized"] = true
claims["user"] = time.Now().Add(time.Minute * 30).Unix()
tokenString, err := token.SignedString(mySigningKey)
if err != nil {
fmt.Errorf("Something went wrong: %s", err.Error())
return "", err
}
return tokenString, nil
}
- Go
- Gorm
- Mux
- PSQL
- Full CRUD actions created for data manipulation
- Router built with mux
- Auth with bcrypt and JWT token
- Add validations to incoming entries to ensure a clean database.