Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
update force name param instead
Browse files Browse the repository at this point in the history
  • Loading branch information
xstar97 committed Mar 9, 2024
1 parent a69b008 commit b285792
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 57 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@ Replace the default values as needed when running the binary.
- `/rcon/`: This route lists all available servers and their information.
- /api: The PalWorld server list api
- accepts query params
- requires a ?q param to search by server name.
- requires a ?name param to search by server name.
- additional params can further filter the list.

#### API Route Params

| Query Key | Example |
|-----------------|------------------------|
| server_id | ?server_id=123 |
| namespace | ?namespace=example |
| type | ?type=public |
| region | ?region=US |
| name | ?name=MyServer |
| map_name | ?map_name=Map1 |
| description | ?description=Description |
| address | ?address=example.com |
| port | ?port=8080 |
| is_password | ?is_password=true |
| version | ?version=1.0 |
| created_at | ?created_at=1234567890 |
| update_at | ?update_at=1234567890 |
| world_guid | ?world_guid=123 |
| current_players | ?current_players=5 |
| max_players | ?max_players=10 |
| days | ?days=30 |
| server_time | ?server_time=1234567890|
| Query Key | Example | Required |
|-----------------|------------------------|----------|
| name | ?name=MyServer | ✓ |
| server_id | ?server_id=123 | ☐ |
| namespace | ?namespace=example | ☐ |
| type | ?type=public | ☐ |
| region | ?region=US | ☐ |
| map_name | ?map_name=Map1 | ☐ |
| description | ?description=Description | ☐ |
| address | ?address=example.com | ☐ |
| port | ?port=8080 | ☐ |
| is_password | ?is_password=true | ☐ |
| version | ?version=1.0 | ☐ |
| created_at | ?created_at=1234567890 | ☐ |
| update_at | ?update_at=1234567890 | ☐ |
| world_guid | ?world_guid=123 | ☐ |
| current_players | ?current_players=5 | ☐ |
| max_players | ?max_players=10 | ☐ |
| days | ?days=30 | ☐ |
| server_time | ?server_time=1234567890| ☐ |

### Docker Installation

Expand Down
87 changes: 51 additions & 36 deletions internal/routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,74 @@ type ServerListResponse struct {
Region string `json:"region"`
IsNextPage bool `json:"is_next_page"`
ServerList []Server `json:"server_list"`
NextPageURL string `json:"next_page_url"`
}

func ApiHandler(w http.ResponseWriter, r *http.Request) {
// Parse query parameters
queryParams := r.URL.Query()

// Construct the search URL
searchURL := config.ApiConfig.Base + config.ApiConfig.Search + "?q=" + url.QueryEscape(queryParams.Get("q"))

// Send a request to the search endpoint
response, err := http.Get(searchURL)
if err != nil {
log.Printf("Error searching for server: %s", err)
http.Error(w, fmt.Sprintf("Error searching for server: %s", err), http.StatusInternalServerError)
return
}
defer response.Body.Close()

// Decode the response JSON
var serverListResponse ServerListResponse
err = json.NewDecoder(response.Body).Decode(&serverListResponse)
if err != nil {
log.Printf("Error decoding search response: %s", err)
http.Error(w, fmt.Sprintf("Error decoding search response: %s", err), http.StatusInternalServerError)
// Check if the "name" query parameter is provided
nameQuery := queryParams.Get("name")
if nameQuery == "" {
http.Error(w, "Name query parameter is required", http.StatusBadRequest)
return
}

// If no servers found, return empty response
if len(serverListResponse.ServerList) == 0 {
log.Println("No servers found.")
http.Error(w, "No servers found.", http.StatusNotFound)
return
// Set the default search field to "q" if not provided
if queryParams.Get("q") == "" {
queryParams.Set("q", nameQuery)
}

// If there is only one result, return the object directly
if len(serverListResponse.ServerList) == 1 {
serverJSON, err := json.Marshal(serverListResponse.ServerList[0])
// Construct the initial search URL
searchURL := config.ApiConfig.Base + config.ApiConfig.Search + "?q=" + url.QueryEscape(queryParams.Get("q"))

var allServers []Server

// Pagination loop
for {
// Send a request to the search endpoint
response, err := http.Get(searchURL)
if err != nil {
log.Printf("Error marshalling server to JSON: %s", err)
http.Error(w, fmt.Sprintf("Error marshalling server to JSON: %s", err), http.StatusInternalServerError)
log.Printf("Error searching for server: %s", err)
http.Error(w, fmt.Sprintf("Error searching for server: %s", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(serverJSON)
return
defer response.Body.Close()

// Decode the response JSON
var serverListResponse ServerListResponse
err = json.NewDecoder(response.Body).Decode(&serverListResponse)
if err != nil {
log.Printf("Error decoding search response: %s", err)
http.Error(w, fmt.Sprintf("Error decoding search response: %s", err), http.StatusInternalServerError)
return
}

// If no servers found, return empty response
if len(serverListResponse.ServerList) == 0 {
log.Println("No servers found.")
http.Error(w, "No servers found.", http.StatusNotFound)
return
}

// Append servers to the result
allServers = append(allServers, serverListResponse.ServerList...)

// Check if there are more pages
if !serverListResponse.IsNextPage {
break
}

// Update the search URL for the next page
searchURL = config.ApiConfig.Base + serverListResponse.NextPageURL
}

// Filter servers based on query parameters other than "q"
filteredServers := serverListResponse.ServerList
filteredServers := allServers
for key, values := range queryParams {
// Skip filtering if the query parameter is "q"
if key == "q" {
// Skip filtering if the query parameter is "q" or "name"
if key == "q" || key == "name" {
continue
}
if len(values) > 0 {
Expand Down Expand Up @@ -167,4 +182,4 @@ func filterServersByParamByKey(servers []Server, key string, value string) []Ser
log.Printf("\nNumber of servers before filtering: %d\n", len(servers))
log.Printf("Number of servers after filtering: %d\n", len(filteredServers))
return filteredServers
}
}

0 comments on commit b285792

Please sign in to comment.