Skip to content

docs: add net/http usage example - #14

Merged
pragneshbagary merged 2 commits into
go-thin:mainfrom
WilliamK112:feat/http-example
Jun 18, 2026
Merged

docs: add net/http usage example#14
pragneshbagary merged 2 commits into
go-thin:mainfrom
WilliamK112:feat/http-example

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

Summary

  • add examples/http/main.go with a small net/http server using the in-memory storage backend
  • wire POST /register to svc.Register
  • wire POST /login to svc.Login and return access/refresh tokens
  • wire GET /protected to validate Authorization: Bearer <token> before serving the route

Fixes #11

Validation

  • Checked the example against the current auth.New, Register, Login, ValidateAccessToken, and memory.NewInMemoryStorage APIs
  • git diff --cached --check

Note: this Windows Codex shell does not have go, gofmt, or Docker installed on PATH, so I could not run gofmt or go test ./... locally. The file is formatted in standard gofmt style by inspection.

@pragneshbagary

Copy link
Copy Markdown
Member

Hey @WilliamK112, thanks for the example — the register and login flows look great!

One thing to update before we merge: PR #15 (bearer token middleware) has just been merged, so the library now has auth.Middleware built in. The /protected handler currently extracts the bearer token manually:

token, ok := bearerToken(r)
if !ok { ... }
claims, err := svc.ValidateAccessToken(token)

Could you replace that with the middleware instead? It would make the example actually demonstrate the feature:

mux.HandleFunc("/protected", auth.Middleware(svc)(protectedHandler(svc)))

func protectedHandler(svc *auth.Service) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        if r.Method != http.MethodGet {
            writeError(w, http.StatusMethodNotAllowed, "use GET")
            return
        }
        claims, _ := auth.ClaimsFromContext(r.Context())
        writeJSON(w, http.StatusOK, map[string]any{
            "message": "welcome to the protected route",
            "claims":  claims,
        })
    }
}

You can also remove the local bearerToken helper since it's no longer needed.

@WilliamK112

Copy link
Copy Markdown
Contributor Author

Updated the HTTP example to demonstrate the built-in middleware now that PR #15 is merged:

  • \/protected\ is registered through \�uth.Middleware(svc)\
  • the handler now reads claims via \�uth.ClaimsFromContext\
  • the local \�earerToken\ helper and \strings\ import were removed

I ran \git diff --check\ locally. I couldn't run \go test ./...\ in this Codex environment because the Go toolchain isn't installed on PATH here.

@pragneshbagary

Copy link
Copy Markdown
Member

Hey @WilliamK112, one small thing before we merge: the branch is missing the middleware code from PR #15 which was merged to main after you cut this branch. That's why auth.Middleware and auth.ClaimsFromContext are undefined when building.

Could you run git fetch origin && git rebase origin/main and force push? That will pull in the middleware and the build should pass.

@WilliamK112

Copy link
Copy Markdown
Contributor Author

Done, thanks for the heads-up. I ran the requested rebase onto current origin/main and force-pushed the branch.

The branch now includes the merged middleware code from #15, and the final PR diff is still just examples/http/main.go. The protected route uses auth.Middleware(svc) and reads claims with auth.ClaimsFromContext.

Local validation:

git diff --check origin/main...HEAD
rg -n 'Middleware|ClaimsFromContext|bearerToken|strings|Handle\("/protected"' examples/http/main.go pkg internal

I still cannot run go test ./... in this Windows Codex shell because the Go toolchain is not installed on PATH here.

@pragneshbagary
pragneshbagary merged commit 5f6961c into go-thin:main Jun 18, 2026
@pragneshbagary

Copy link
Copy Markdown
Member

I've added the CI workflows, it should automatically run the go tests for future PRs. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add net/http usage example

2 participants