Skip to content

Commit

Permalink
Added middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
djthorpe committed Jun 4, 2024
1 parent aaf3169 commit 379fa77
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/nginx-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func main() {
auth, err := auth.Config{
TokenJar: jar.(auth.TokenJar),
TokenBytes: 8,
Bearer: true, // Use bearer token in requests for authorization
}.New()
if err != nil {
log.Fatal(err)
Expand All @@ -77,12 +78,14 @@ func main() {
Service: n.(server.ServiceEndpoints),
Middleware: []server.Middleware{
logger.(server.Middleware),
auth.(server.Middleware),
},
},
"auth": { // /api/auth/...
Service: auth.(server.ServiceEndpoints),
Middleware: []server.Middleware{
logger.(server.Middleware),
auth.(server.Middleware),
},
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/handler/auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Config struct {
TokenJar TokenJar `hcl:"token_jar" description:"Persistent storage for tokens"`
TokenBytes int `hcl:"token_bytes" description:"Number of bytes in a token"`
Bearer bool `hcl:"bearer" description:"Use bearer token for authorization"`
}

// Check interfaces are satisfied
Expand Down
6 changes: 6 additions & 0 deletions pkg/handler/auth/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
types "github.com/mutablelogic/go-server/pkg/types"
)

///////////////////////////////////////////////////////////////////////////////
// TYPES

// Check interfaces are satisfied
var _ server.ServiceEndpoints = (*auth)(nil)

///////////////////////////////////////////////////////////////////////////////
// GLOBALS

Expand Down
27 changes: 27 additions & 0 deletions pkg/handler/auth/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package auth

import (
"context"
"net/http"

// Packages
"github.com/mutablelogic/go-server"
"github.com/mutablelogic/go-server/pkg/provider"
)

///////////////////////////////////////////////////////////////////////////////
// TYPES

// Check interfaces are satisfied
var _ server.Middleware = (*auth)(nil)

///////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS

func (middleware *auth) Wrap(ctx context.Context, next http.HandlerFunc) http.HandlerFunc {
logger := provider.Logger(ctx)
return func(w http.ResponseWriter, r *http.Request) {
logger.Printf(ctx, "TODO Auth: %v", r.URL)
next(w, r)
}
}
1 change: 0 additions & 1 deletion pkg/handler/auth/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type auth struct {

// Check interfaces are satisfied
var _ server.Task = (*auth)(nil)
var _ server.ServiceEndpoints = (*auth)(nil)

///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/tokenjar/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (jar *tokenjar) Run(ctx context.Context) error {
if err := jar.Write(); err != nil {
logger.Print(ctx, err)
} else {
logger.Print(ctx, "Sync %q", filepath.Base(jar.filename))
logger.Printf(ctx, "Sync %q", filepath.Base(jar.filename))
}
}
case <-ctx.Done():
Expand Down

0 comments on commit 379fa77

Please sign in to comment.