Skip to content

Commit 8b22871

Browse files
author
zoidbergwill
committed
Support config via query params
Closes #12
1 parent b52b838 commit 8b22871

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

main.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
// Version is the default value that should be overridden in the
2222
// build/release process.
23+
// TODO: Actually set this
2324
var Version = "dev"
2425

2526
func home(w http.ResponseWriter, _ *http.Request) {
@@ -39,13 +40,13 @@ func healthz(w http.ResponseWriter, _ *http.Request) {
3940
}
4041

4142
func createEvent(cfg *libhoney.Config) (*libhoney.Event, error) {
42-
libhoney.UserAgentAddition = fmt.Sprintf("buildevents/%s", Version)
43-
libhoney.UserAgentAddition += fmt.Sprintf(" (%s)", "GitLab-CI")
43+
libhoney.UserAgentAddition = fmt.Sprintf("buildevents/%s (GitLab-CI)", Version)
4444

4545
if cfg.APIKey == "" {
4646
cfg.Transmission = &transmission.WriterSender{}
4747
}
4848

49+
// TODO: I thinks this should maybe be not using the default package-level client
4950
ev := libhoney.NewEvent()
5051
ev.AddField("ci_provider", "GitLab-CI")
5152
ev.AddField("meta.version", Version)
@@ -214,7 +215,7 @@ func handleJob(cfg *libhoney.Config, w http.ResponseWriter, body []byte) {
214215
}
215216
}
216217

217-
func handleRequest(cfg *libhoney.Config, w http.ResponseWriter, req *http.Request) {
218+
func handleRequest(defaultConfig *libhoney.Config, w http.ResponseWriter, req *http.Request) {
218219
if req.Method != http.MethodPost {
219220
http.Error(w, "Unsupported method", http.StatusMethodNotAllowed)
220221
return
@@ -241,6 +242,27 @@ func handleRequest(cfg *libhoney.Config, w http.ResponseWriter, req *http.Reques
241242
return
242243
}
243244

245+
// Load potential custom query parameters
246+
// e.g. API key, dataset name, and honeycomb host
247+
// TODO: check for additional unsupported query parameters
248+
cfg := &libhoney.Config{}
249+
cfg.APIKey = defaultConfig.APIKey
250+
cfg.Dataset = defaultConfig.Dataset
251+
cfg.APIHost = defaultConfig.APIHost
252+
query := req.URL.Query()
253+
key := query.Get("api_key")
254+
if key != "" {
255+
cfg.APIKey = key
256+
}
257+
dataset := query.Get("dataset")
258+
if dataset != "" {
259+
cfg.Dataset = dataset
260+
}
261+
host := query.Get("api_host")
262+
if host != "" {
263+
cfg.APIHost = host
264+
}
265+
244266
switch eventType {
245267
case "Pipeline Hook":
246268
log.Println("Received pipeline webhook:", string(body))

0 commit comments

Comments
 (0)