Hallucinate a very basic planet api
This commit is contained in:
144
main.go
144
main.go
@@ -2,10 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"go-eve-pi/esi"
|
||||||
wh "go-eve-pi/webhook"
|
wh "go-eve-pi/webhook"
|
||||||
|
|
||||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||||
@@ -69,7 +72,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
logger.Info("Login requested for character %s", charNameStr)
|
logger.Info("Login requested for character %s", charNameStr)
|
||||||
// Trigger the auth flow (will register callback if needed)
|
// Trigger the auth flow (will register callback if needed)
|
||||||
token, err := sso.GetToken(context.Background(), charNameStr)
|
char, err := sso.GetCharacter(context.Background(), charNameStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to authenticate character %s: %v", charNameStr, err)
|
logger.Error("Failed to authenticate character %s: %v", charNameStr, err)
|
||||||
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
@@ -79,7 +82,144 @@ func main() {
|
|||||||
logger.Info("Successfully authenticated character %s", charNameStr)
|
logger.Info("Successfully authenticated character %s", charNameStr)
|
||||||
ctx.SetContentType("text/plain")
|
ctx.SetContentType("text/plain")
|
||||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
ctx.WriteString("Authenticated! Access token: " + token)
|
ctx.WriteString("Authenticated! Access token: " + char.AccessToken)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Get planets for a character
|
||||||
|
r.GET("/planets/{character}", func(ctx *fasthttp.RequestCtx) {
|
||||||
|
charName := ctx.UserValue("character")
|
||||||
|
charNameStr, ok := charName.(string)
|
||||||
|
if !ok || charNameStr == "" {
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||||
|
ctx.WriteString("Missing character parameter")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("Fetching planets for character %s", charNameStr)
|
||||||
|
|
||||||
|
// Get access token for character
|
||||||
|
char, err := sso.GetCharacter(context.Background(), charNameStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to get token for character %s: %v", charNameStr, err)
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
|
ctx.WriteString("Authentication failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch planets using ESI API
|
||||||
|
planets, err := esi.GetCharacterPlanets(context.Background(), int(char.ID), char.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to fetch planets for character %s: %v", charNameStr, err)
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
|
ctx.WriteString("Failed to fetch planets")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return planets as JSON
|
||||||
|
ctx.SetContentType("application/json")
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
|
json.NewEncoder(ctx).Encode(planets)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Get detailed planet information
|
||||||
|
r.GET("/planet/{character}/{planet_id}", func(ctx *fasthttp.RequestCtx) {
|
||||||
|
charName := ctx.UserValue("character")
|
||||||
|
charNameStr, ok := charName.(string)
|
||||||
|
if !ok || charNameStr == "" {
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||||
|
ctx.WriteString("Missing character parameter")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
planetIDStr := ctx.UserValue("planet_id")
|
||||||
|
planetID, err := strconv.Atoi(planetIDStr.(string))
|
||||||
|
if err != nil {
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||||
|
ctx.WriteString("Invalid planet ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("Fetching planet details for character %s, planet %d", charNameStr, planetID)
|
||||||
|
|
||||||
|
// Get access token for character
|
||||||
|
char, err := sso.GetCharacter(context.Background(), charNameStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to get token for character %s: %v", charNameStr, err)
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
|
ctx.WriteString("Authentication failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch planet details using ESI API
|
||||||
|
planetDetail, err := esi.GetPlanetDetails(context.Background(), int(char.ID), planetID, char.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to fetch planet details for character %s, planet %d: %v", charNameStr, planetID, err)
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
|
ctx.WriteString("Failed to fetch planet details")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return planet details as JSON
|
||||||
|
ctx.SetContentType("application/json")
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
|
json.NewEncoder(ctx).Encode(planetDetail)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Get all planets with their details for a character
|
||||||
|
r.GET("/planets-full/{character}", func(ctx *fasthttp.RequestCtx) {
|
||||||
|
charName := ctx.UserValue("character")
|
||||||
|
charNameStr, ok := charName.(string)
|
||||||
|
if !ok || charNameStr == "" {
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||||
|
ctx.WriteString("Missing character parameter")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("Fetching full planet data for character %s", charNameStr)
|
||||||
|
|
||||||
|
// Get access token for character
|
||||||
|
char, err := sso.GetCharacter(context.Background(), charNameStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to get token for character %s: %v", charNameStr, err)
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
|
ctx.WriteString("Authentication failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch planets list
|
||||||
|
planets, err := esi.GetCharacterPlanets(context.Background(), int(char.ID), char.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to fetch planets for character %s: %v", charNameStr, err)
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||||
|
ctx.WriteString("Failed to fetch planets")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch details for each planet
|
||||||
|
type PlanetWithDetails struct {
|
||||||
|
Planet esi.Planet
|
||||||
|
Details *esi.PlanetDetail
|
||||||
|
}
|
||||||
|
|
||||||
|
var planetsWithDetails []PlanetWithDetails
|
||||||
|
for _, planet := range planets {
|
||||||
|
details, err := esi.GetPlanetDetails(context.Background(), int(char.ID), planet.PlanetID, char.AccessToken)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warning("Failed to fetch details for planet %d: %v", planet.PlanetID, err)
|
||||||
|
// Continue with other planets even if one fails
|
||||||
|
details = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
planetsWithDetails = append(planetsWithDetails, PlanetWithDetails{
|
||||||
|
Planet: planet,
|
||||||
|
Details: details,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return planets with details as JSON
|
||||||
|
ctx.SetContentType("application/json")
|
||||||
|
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||||
|
json.NewEncoder(ctx).Encode(planetsWithDetails)
|
||||||
})
|
})
|
||||||
|
|
||||||
logger.Info("Starting web server on 0.0.0.0:%s", options.Port)
|
logger.Info("Starting web server on 0.0.0.0:%s", options.Port)
|
||||||
|
|||||||
Reference in New Issue
Block a user