idk even know anymore
i think i added games
This commit is contained in:
127
main.go
127
main.go
@@ -9,8 +9,12 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/joho/godotenv"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
@@ -38,6 +42,8 @@ func sendJSON(w http.ResponseWriter, data any, status int) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
godotenv.Load()
|
||||
|
||||
dbPath := os.Getenv("DB_PATH")
|
||||
if dbPath == "" {
|
||||
dbPath = "./sqlite.db"
|
||||
@@ -48,7 +54,7 @@ func main() {
|
||||
log.Fatalf("failed to connect to db: %v\n", err)
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(model.User{}); err != nil {
|
||||
if err := db.AutoMigrate(model.User{}, model.Game{}); err != nil {
|
||||
log.Fatalf("failed to automigrate db: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -152,20 +158,14 @@ func main() {
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/user", func(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := r.Cookie("token")
|
||||
if err != nil {
|
||||
sendError(w, "no token cookie", err, 401)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := auth.ValidateUserToken(c.Value)
|
||||
userId, err := auth.GetUserIdFromRequest(r)
|
||||
if err != nil {
|
||||
sendError(w, "invalid token", err, 401)
|
||||
return
|
||||
}
|
||||
|
||||
user := &model.User{}
|
||||
if tx := db.First(user, "id = ?", userId); tx.Error != nil {
|
||||
if tx := db.Preload("Games").First(user, "id = ?", userId); tx.Error != nil {
|
||||
sendError(w, "user not found", err, 404)
|
||||
return
|
||||
}
|
||||
@@ -173,6 +173,115 @@ func main() {
|
||||
sendJSON(w, user, 200)
|
||||
})
|
||||
|
||||
mux.HandleFunc("GET /api/search/games", func(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := http.NewRequest("GET", "https://store.steampowered.com/search/", nil)
|
||||
if err != nil {
|
||||
sendError(w, "failed to construct api request", err, 500)
|
||||
return
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Set("term", r.URL.Query().Get("q"))
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
sendError(w, "failed to query api", err, 500)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||
if err != nil {
|
||||
sendError(w, "failed to parse html", err, 500)
|
||||
return
|
||||
}
|
||||
|
||||
type game struct {
|
||||
Name string `json:"name"`
|
||||
SteamAppID int `json:"steamAppId"`
|
||||
Image string `json:"image"`
|
||||
ReleaseDate string `json:"releaseDate"`
|
||||
}
|
||||
|
||||
games := []game{}
|
||||
doc.Find(`#search_resultsRows > a`).Each(func(i int, s *goquery.Selection) {
|
||||
name := s.Find(".search_name > .title").Text()
|
||||
appId := s.AttrOr("data-ds-appid", "")
|
||||
if appId == "" {
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(appId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
image := s.Find(".search_capsule > img").AttrOr("src", "")
|
||||
if image == "" {
|
||||
return
|
||||
}
|
||||
|
||||
releaseDate := s.Find(".search_released").Text()
|
||||
|
||||
games = append(games, game{
|
||||
Name: name,
|
||||
SteamAppID: id,
|
||||
Image: image,
|
||||
ReleaseDate: strings.TrimSpace(releaseDate),
|
||||
})
|
||||
})
|
||||
|
||||
sendJSON(w, games, 200)
|
||||
})
|
||||
|
||||
mux.HandleFunc("POST /api/user/games", func(w http.ResponseWriter, r *http.Request) {
|
||||
userId, err := auth.GetUserIdFromRequest(r)
|
||||
if err != nil {
|
||||
sendError(w, "unauthorized", err, 401)
|
||||
return
|
||||
}
|
||||
|
||||
user := &model.User{}
|
||||
if tx := db.Preload("Games").First(user, "id = ?", userId); tx.Error != nil {
|
||||
sendError(w, "user not found", err, 404)
|
||||
return
|
||||
}
|
||||
|
||||
var body struct {
|
||||
SteamAppID int `json:"steamAppId"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
sendError(w, "invalid request", err, 400)
|
||||
return
|
||||
}
|
||||
|
||||
if body.SteamAppID == 0 {
|
||||
sendError(w, "steam appid is required", nil, 400)
|
||||
return
|
||||
}
|
||||
|
||||
game, err := getSteamGameDetails(body.SteamAppID)
|
||||
if err != nil {
|
||||
sendError(w, "steam game not found", err, 404)
|
||||
return
|
||||
}
|
||||
|
||||
if tx := db.Create(game); tx.Error != nil {
|
||||
sendError(w, "failed to create game", err, 400)
|
||||
return
|
||||
}
|
||||
|
||||
user.Games = append(user.Games, *game)
|
||||
|
||||
if tx := db.Save(user); tx.Error != nil {
|
||||
sendError(w, "failed to add game to user object", tx.Error, 500)
|
||||
return
|
||||
}
|
||||
|
||||
sendJSON(w, user, 200)
|
||||
})
|
||||
|
||||
log.Print("starting http server on http://localhost:5000")
|
||||
if err := http.ListenAndServe(":5000", mux); err != nil {
|
||||
log.Fatalf("failed to start http server: %v\n", err)
|
||||
|
||||
Reference in New Issue
Block a user