Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/server/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/appthrust/appserver-go/appserver/httpx"
"github.com/appthrust/appserver-go/appserver/identity"
"github.com/appthrust/appserver-go/appserver/platformclient"
"github.com/appthrust/appserver-go/appserver/requestlog"
"github.com/appthrust/tracker/cmd/server/internal/directory"
"github.com/appthrust/tracker/cmd/server/internal/domain"
"github.com/appthrust/tracker/cmd/server/internal/store"
Expand Down Expand Up @@ -156,7 +157,9 @@ func (a *API) route(pattern string, mutation, roles, unregistered bool, fn endpo
return
}
if download, ok := body.(attachmentDownload); ok {
download.write(w)
if err := download.write(w); err != nil {
requestlog.SetOutcome(r, "incomplete", "attachment_write_failed")
}
return
}
if text, ok := body.(markdown); ok {
Expand All @@ -166,6 +169,9 @@ func (a *API) route(pattern string, mutation, roles, unregistered bool, fn endpo
return
}
if replay, ok := body.(store.Replay); ok {
if replay.Status >= http.StatusInternalServerError {
requestlog.SetOutcome(r, "error", "idempotent_response_failed")
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(replay.Status)
_, _ = w.Write(replay.Body)
Expand Down
5 changes: 4 additions & 1 deletion cmd/server/internal/api/avatars.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/appthrust/appserver-go/appserver/httpx"
"github.com/appthrust/appserver-go/appserver/link"
"github.com/appthrust/appserver-go/appserver/requestlog"
)

// Avatars use Directory's data and authority, not a Tracker transaction.
Expand Down Expand Up @@ -73,5 +74,7 @@ func (a *API) avatar(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
_, _ = w.Write(data)
if _, err := w.Write(data); err != nil {
requestlog.SetOutcome(r, "incomplete", "avatar_write_failed")
}
}
5 changes: 3 additions & 2 deletions cmd/server/internal/api/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,14 @@ type attachmentDownload struct {
Data []byte
}

func (d attachmentDownload) write(w http.ResponseWriter) {
func (d attachmentDownload) write(w http.ResponseWriter) error {
w.Header().Set("Content-Type", d.Attachment.MediaType)
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": d.Attachment.Filename}))
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Length", strconv.Itoa(len(d.Data)))
_, _ = io.Copy(w, bytes.NewReader(d.Data))
_, err := io.Copy(w, bytes.NewReader(d.Data))
return err
}
func (a *API) attachmentGet(s *request) (any, int, error) {
item, data, err := s.tx.Attachment(s.r.PathValue("id"), true)
Expand Down
2 changes: 2 additions & 0 deletions cmd/server/internal/api/people.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"github.com/appthrust/appserver-go/appserver/requestlog"
"github.com/appthrust/tracker/cmd/server/internal/domain"
)

Expand All @@ -16,6 +17,7 @@ func (a *API) people(s *request) (any, int, error) {
}
list, err := a.directory.Search(s.r.Context(), s.bearer, q)
if err != nil {
requestlog.SetOutcome(s.r, "degraded", "directory_search_unavailable")
return peopleResponse{Items: []domain.Person{}, Source: "unavailable"}, 200, nil
}
return peopleResponse{Items: list, Source: "directory"}, 200, nil
Expand Down
11 changes: 10 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/appthrust/appserver-go/appserver/platformenv"
"github.com/appthrust/appserver-go/appserver/requestlog"
)

func main() {
Expand Down Expand Up @@ -43,7 +44,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
server := &http.Server{Addr: *listen, Handler: handler, ReadHeaderTimeout: 10 * time.Second}
server := newServer(*listen, handler)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go func() {
Expand All @@ -59,3 +60,11 @@ func main() {
log.Fatal(err)
}
}

func newServer(addr string, handler http.Handler) *http.Server {
return &http.Server{
Addr: addr,
Handler: requestlog.Wrap(handler, requestlog.Options{Service: "tracker"}),
ReadHeaderTimeout: 10 * time.Second,
}
}
4 changes: 3 additions & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/appthrust/appserver-go/appserver/platformclient"
"github.com/appthrust/appserver-go/appserver/platformenv"
"github.com/appthrust/appserver-go/appserver/releaseidentity"
"github.com/appthrust/appserver-go/appserver/requestlog"
"github.com/appthrust/tracker/cmd/server/internal/api"
"net/http"
"net/url"
Expand Down Expand Up @@ -44,7 +45,8 @@ func newHandler(staticDir, upstreamURL, projectID string, tracker http.Handler,
writeJSON(w, http.StatusOK, map[string]string{"projectId": projectID, "publicUrl": os.Getenv("TRACKER_PUBLIC_URL"), "directoryUrl": os.Getenv("TRACKER_DIRECTORY_PUBLIC_URL")})
})
mux.Handle("GET /launcher", launcher.NewHandler(catalog, authenticator.Authenticate))
mux.Handle("GET /releasez", releaseidentity.NewHandler("TRACKER", func(w http.ResponseWriter, _ *http.Request) {
mux.Handle("GET /releasez", releaseidentity.NewHandler("TRACKER", func(w http.ResponseWriter, r *http.Request) {
requestlog.SetOutcome(r, "unavailable", "release_identity_unavailable")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte("UI release status unavailable\n"))
Expand Down
40 changes: 40 additions & 0 deletions cmd/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -88,3 +90,41 @@ func TestPendingSurfaceAndRelease(t *testing.T) {
require.Equal(t, status, w.Code, path)
}
}

func TestRequestLogForUnavailableRelease(t *testing.T) {
for _, key := range []string{"TRACKER_RELEASE_VERSION", "TRACKER_SOURCE_REVISION", "TRACKER_ARTIFACT_DIGEST", "PLATFORM_OIDC_ISSUER", "PLATFORM_OIDC_ISSUER_CA_PEM"} {
t.Setenv(key, "")
}
mux, err := newHandler(t.TempDir(), "http://127.0.0.1:1", "pj-test", nil, nil)
require.NoError(t, err)

output, err := os.CreateTemp(t.TempDir(), "stderr")
require.NoError(t, err)
defer output.Close()
// Wrap captures stderr at construction; restore it before serving the request.
stderr := os.Stderr
os.Stderr = output
server := newServer(":0", mux)
os.Stderr = stderr

w := httptest.NewRecorder()
server.Handler.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/releasez?private=query-marker", nil))
require.Equal(t, http.StatusServiceUnavailable, w.Code)
require.Equal(t, "UI release status unavailable\n", w.Body.String())

line, err := os.ReadFile(output.Name())
require.NoError(t, err)
require.Equal(t, 1, bytes.Count(line, []byte{'\n'}))
require.NotContains(t, string(line), "query-marker")
var entry struct {
Msg, Service, Path, Outcome, Reason string
Status int
}
require.NoError(t, json.Unmarshal(line, &entry))
require.Equal(t, "request", entry.Msg)
require.Equal(t, "tracker", entry.Service)
require.Equal(t, "/releasez", entry.Path)
require.Equal(t, w.Code, entry.Status)
require.Equal(t, "unavailable", entry.Outcome)
require.Equal(t, "release_identity_unavailable", entry.Reason)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/appthrust/tracker
go 1.26.0

require (
github.com/appthrust/appserver-go v0.5.1
github.com/appthrust/appserver-go v0.7.0
github.com/lib/pq v1.12.3
github.com/oklog/ulid/v2 v2.1.1
github.com/stretchr/testify v1.11.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/appthrust/appserver-go v0.5.1 h1:L9IfLUZjR2GxQmEK+d7rjo8P1bItbbb+kqKotRRoey4=
github.com/appthrust/appserver-go v0.5.1/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo=
github.com/appthrust/appserver-go v0.7.0 h1:Fd1G+l/Ys0aAAiI2ZmMKqAz4qY6Dv7WwsF/rZtUgPUA=
github.com/appthrust/appserver-go v0.7.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo=
github.com/coreos/go-oidc/v3 v3.20.0 h1:EtE0WIBHk03N+DqGkY4+UONzzZHk7amKt6IyNd7OsZE=
github.com/coreos/go-oidc/v3 v3.20.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down