Skip to content

Commit

Permalink
feat(backend): add endpoint for getting listings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeklavans committed Feb 27, 2024
1 parent 97850c8 commit c8e0789
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module example/web-service-gin

go 1.21.6

require github.com/gin-gonic/gin v1.9.1
require (
github.com/gin-gonic/gin v1.9.1
github.com/joho/godotenv v1.5.1
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
39 changes: 36 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package main

import (
"net/http"
"encoding/json"
"log"
"net/http"
"os"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)

type ListingProps struct {
Refs []string `json:"refs"`
Price int `json:"price"`
Beds int `json:"beds"`
Baths int `json:"baths"`
Date string `json:"date"`
Notes string `json:"notes"`
IsFavorite bool `json:"isFavorite"`
IsDismissed bool `json:"isDismissed"`
}

func basicAuth(c *gin.Context) {
user, password, hasAuth := c.Request.BasicAuth()
if hasAuth && user == "testuser" && password == "testpass" {
if hasAuth && user == os.Getenv("AUTH_USER") && password == os.Getenv("AUTH_PASS") {
c.Next()
} else {
c.Abort()
Expand All @@ -17,13 +32,31 @@ func basicAuth(c *gin.Context) {
}
}

func getListings(c *gin.Context) {
data, err := os.ReadFile("../data/listings.json")
if err != nil {
log.Print(err)
}

m := make(map[string]ListingProps)
json.Unmarshal(data, &m)

c.IndentedJSON(http.StatusOK, m)
}

func ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"pong": true})
}

func main() {
err := godotenv.Load("../.env")
if err != nil {
log.Fatalf("Error loading .env: %s", err)
}

router := gin.Default()
router.GET("/ping", basicAuth, ping)
router.GET("/listings", getListings)

router.Run("0.0.0.0:8080")
}

0 comments on commit c8e0789

Please sign in to comment.