-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
38 lines (33 loc) · 1.05 KB
/
context.go
File metadata and controls
38 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"fmt"
"net/http"
"strings"
)
// ValidateNetwork validates and normalizes the network parameter
// Returns the normalized network name or error if invalid
func (app *App) ValidateNetwork(network string) (string, error) {
// Normalize network name
network = strings.ToLower(strings.TrimSpace(network))
// Default to configured default network if empty
if network == "" {
return app.config.DefaultNetwork, nil
}
// Validate network
switch network {
case "mainnet", "testnet":
return network, nil
default:
return "", fmt.Errorf("invalid network: %s (must be 'mainnet' or 'testnet')", network)
}
}
// getNetworkParam extracts, validates, and returns the network query parameter.
// Returns the validated network and true, or writes a 400 error and returns false.
func (app *App) getNetworkParam(w http.ResponseWriter, r *http.Request) (string, bool) {
network, err := app.ValidateNetwork(r.URL.Query().Get("network"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return "", false
}
return network, true
}