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
12 changes: 11 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"flag"
"io"
"log"
"net/http"
"os"
Expand All @@ -13,6 +14,7 @@ import (

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

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

func newServer(addr string, handler http.Handler, logOutput io.Writer) *http.Server {
return &http.Server{
Addr: addr,
Handler: requestlog.Wrap(handler, requestlog.Options{Service: "agent-guide", Output: logOutput}),
ReadHeaderTimeout: 10 * time.Second,
}
}
23 changes: 23 additions & 0 deletions cmd/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
Expand Down Expand Up @@ -53,6 +54,28 @@ func userBearer(subject, email string) string {
return "Bearer eyJhbGciOiJub25lIn0." + base64.RawURLEncoding.EncodeToString(claims) + "."
}

func TestServerLogsFailedReadiness(t *testing.T) {
environment(t)
handler := fixtureHandler(t, "invalid-yaml", "https://api.example.test")
var logs bytes.Buffer
server := newServer(":0", handler, &logs)
response := call(server.Handler, "/readyz", "")
require.Equal(t, http.StatusServiceUnavailable, response.Code)

require.Equal(t, 1, bytes.Count(logs.Bytes(), []byte{'\n'}))
var entry struct {
Service string `json:"service"`
Path string `json:"path"`
Status int `json:"status"`
ErrorCode string `json:"error_code"`
}
require.NoError(t, json.Unmarshal(logs.Bytes(), &entry))
require.Equal(t, "agent-guide", entry.Service)
require.Equal(t, "/readyz", entry.Path)
require.Equal(t, response.Code, entry.Status)
require.Equal(t, "content_unavailable", entry.ErrorCode)
}

func TestPublicRoutesAndBearerBoundaries(t *testing.T) {
environment(t)
handler := fixtureHandler(t, "valid", "https://api.example.test")
Expand Down
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/agent-guide
go 1.26.0

require (
github.com/appthrust/appserver-go v0.5.1
github.com/appthrust/appserver-go v0.7.0
github.com/stretchr/testify v1.11.1
gopkg.in/yaml.v3 v3.0.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