Skip to content

Commit 959abd5

Browse files
committed
add steam api client
1 parent f5b00c3 commit 959abd5

File tree

4 files changed

+91
-43
lines changed

4 files changed

+91
-43
lines changed

src/go/config/file.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"github.com/GSH-LAN/Unwindia_common/src/go/matchservice"
7+
template2 "github.com/GSH-LAN/Unwindia_common/src/go/template"
78
"github.com/GSH-LAN/Unwindia_common/src/go/unwindiaError"
89
"github.com/fsnotify/fsnotify"
910
"github.com/rs/zerolog/log"
@@ -40,21 +41,14 @@ func (c *ConfigFileImpl) GetGameServerTemplateForMatch(info matchservice.MatchIn
4041
if !gameValid {
4142
return nil, unwindiaError.NewInvalidGameError(gameName)
4243
}
43-
44-
funcs := map[string]any{
45-
"contains": strings.Contains,
46-
"hasPrefix": strings.HasPrefix,
47-
"hasSuffix": strings.HasSuffix,
48-
}
49-
5044
// TODO: make this shit reliable even with kinda broken configs
5145
// we now parse environments to replace custom variables and convert numeric values
5246
var newEnvironment = make(map[string]interface{})
5347
for envName, envValue := range gsTemplate.Environment {
5448
if val, ok := envValue.(string); ok {
5549

5650
parsedEnvVar := strings.Builder{}
57-
err := template.Must(template.New("serverEnvironment").Funcs(funcs).Parse(val)).Execute(&parsedEnvVar, nil)
51+
err := template.Must(template.New("serverEnvironment").Funcs(template2.TemplateFunctions).Parse(val)).Execute(&parsedEnvVar, nil)
5852
if err != nil {
5953
log.Error().Err(err).Msg("Error parsing environment template")
6054
} else {
@@ -63,10 +57,10 @@ func (c *ConfigFileImpl) GetGameServerTemplateForMatch(info matchservice.MatchIn
6357

6458
if intValue, err := strconv.Atoi(val); err == nil {
6559
envValue = intValue
66-
log.Trace().Str("environment", envName).Int("value", intValue).Msg("Environemt is type int (parsed from string)")
60+
log.Trace().Str("environment", envName).Int("value", intValue).Msg("Environment is type int (parsed from string)")
6761
} else {
6862
envValue = val
69-
log.Trace().Str("environment", envName).Str("value", val).Msg("Environemt is type string")
63+
log.Trace().Str("environment", envName).Str("value", val).Msg("Environment is type string")
7064
}
7165
}
7266
newEnvironment[envName] = envValue

src/go/config/model.go

+18-19
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,24 @@ type UnwindiaPteroConfig struct {
3333
}
3434

3535
type GamerServerConfigTemplate struct {
36-
UserId int `json:"userId"`
37-
LocationId int `json:"locationId"`
38-
NestId int `json:"nestId"`
39-
ServerNamePrefix string `json:"serverNamePrefix"`
40-
ServerNameGOTVPrefix string `json:"serverNameGOTVPrefix"`
41-
DefaultStartup string `json:"defaultStartup"`
42-
DefaultDockerImage string `json:"defaultDockerImage"`
43-
DefaultServerPassword string `json:"defaultServerPassword"`
44-
DefaultRconPassword string `json:"defaultRconPassword"`
45-
EggId int `json:"eggId"`
46-
Limits crocgodyl.Limits `json:"limits"` // Limits which will be set for new servers.
47-
ForceLimits bool `json:"forceLimits"` // if true, server which does not meet Limits settings will be deleted. If false, existing suspended servers will be reused, no matter of matching Limits
48-
Environment map[string]interface{} `json:"environment"` // Environment settings for a game. Can be values or matching properties of a gameserver match thingy object in go-template like format // TODO: set correct object description
49-
TvSlots int `json:"tvSlots"`
50-
TvPortOffset int `json:"tvPortOffset"`
51-
DeleteAfterDuration Duration `json:"deleteAfterDuration"`
52-
ServerReadyRconCommands []string `json:"serverReadyRconCommands"`
53-
ServerReadyRconWaitTime Duration `json:"serverReadyRconWaitTime"`
54-
EnvironmentMapping map[string]string `json:"envMapping"`
36+
UserId int `json:"userId"`
37+
LocationId int `json:"locationId"`
38+
NestId int `json:"nestId"`
39+
ServerNamePrefix string `json:"serverNamePrefix"`
40+
ServerNameGOTVPrefix string `json:"serverNameGOTVPrefix"`
41+
DefaultStartup string `json:"defaultStartup"`
42+
DefaultDockerImage string `json:"defaultDockerImage"`
43+
DefaultServerPassword string `json:"defaultServerPassword"`
44+
DefaultRconPassword string `json:"defaultRconPassword"`
45+
EggId int `json:"eggId"`
46+
Limits crocgodyl.Limits `json:"limits"` // Limits which will be set for new servers.
47+
ForceLimits bool `json:"forceLimits"` // if true, server which does not meet Limits settings will be deleted. If false, existing suspended servers will be reused, no matter of matching Limits
48+
Environment map[string]interface{} `json:"environment"` // Environment settings for a game. Can be values or matching properties of a gameserver match thingy object in go-template like format // TODO: set correct object description
49+
TvSlots int `json:"tvSlots"`
50+
TvPortOffset int `json:"tvPortOffset"`
51+
DeleteAfterDuration Duration `json:"deleteAfterDuration"`
52+
SteamApiTokenAppId int `json:"steamApiTokenAppId"`
53+
EnvironmentMapping map[string]string `json:"envMapping"`
5554
}
5655

5756
type Duration struct {

src/go/steam-api-token/client.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package steam_api_token
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"net/url"
8+
"path"
9+
"strconv"
10+
)
11+
12+
type Client struct {
13+
baseUrl *url.URL
14+
authorized bool
15+
authorizationToken string
16+
httpClient *http.Client
17+
}
18+
19+
func NewClient(baseUrl *url.URL, authorizationToken string) *Client {
20+
return &Client{
21+
baseUrl: baseUrl,
22+
authorized: authorizationToken != "",
23+
authorizationToken: fmt.Sprintf("Bearer %s", authorizationToken),
24+
httpClient: &http.Client{},
25+
}
26+
}
27+
28+
func (c *Client) GetSteamApiToken(ctx context.Context, appId int, description string) (string, error) {
29+
url := *c.baseUrl
30+
url.Path = path.Join(url.Path, "token", strconv.Itoa(appId), description)
31+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
32+
if err != nil {
33+
return "", err
34+
}
35+
if c.authorizationToken != "" {
36+
req.Header.Add("Authorization", c.authorizationToken)
37+
}
38+
39+
resp, err := c.httpClient.Do(req)
40+
if err != nil {
41+
return "", err
42+
}
43+
defer resp.Body.Close()
44+
45+
if resp.StatusCode != http.StatusOK {
46+
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
47+
}
48+
49+
var token string
50+
if _, err = fmt.Fscan(resp.Body, &token); err != nil {
51+
return "", err
52+
}
53+
54+
return token, nil
55+
}

src/go/template/template.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ import (
99
"text/template"
1010
)
1111

12+
var TemplateFunctions = map[string]any{
13+
"contains": strings.Contains,
14+
"hasPrefix": strings.HasPrefix,
15+
"hasSuffix": strings.HasSuffix,
16+
"splitHostPort": func(s string) (map[string]string, error) {
17+
host, port, err := net.SplitHostPort(s)
18+
if err != nil {
19+
return nil, err
20+
}
21+
return map[string]string{"host": host, "port": port}, nil
22+
},
23+
}
24+
1225
func ParseTemplateForMatch(tpl string, matchinfo *matchservice.MatchInfo) (string, error) {
1326
if matchinfo == nil {
1427
return "", errors.New("empty matchinfo")
1528
}
1629

17-
funcs := map[string]any{
18-
"contains": strings.Contains,
19-
"hasPrefix": strings.HasPrefix,
20-
"hasSuffix": strings.HasSuffix,
21-
"splitHostPort": func(s string) (map[string]string, error) {
22-
host, port, err := net.SplitHostPort(s)
23-
if err != nil {
24-
return nil, err
25-
}
26-
return map[string]string{"host": host, "port": port}, nil
27-
},
28-
}
29-
30-
tmpl, err := template.New("match").Option("missingkey=error").Funcs(funcs).Parse(tpl)
30+
tmpl, err := template.New("match").Option("missingkey=error").Funcs(TemplateFunctions).Parse(tpl)
3131
if err != nil {
3232
log.Err(err).Msg("Error parsing template")
3333
return "", err

0 commit comments

Comments
 (0)