Skip to content

Commit

Permalink
Merge branch 'release/1.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas committed Dec 29, 2023
2 parents be4a6dd 178dd5f commit 000b283
Show file tree
Hide file tree
Showing 15 changed files with 2,202 additions and 1,437 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.20.x]
go-version: [1.20.x, 1.21.x]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
85 changes: 46 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 1,8 @@
# wess

![GoVersion](https://img.shields.io/github/go-mod/go-version/gildas/wess)
[![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/gildas/wess)
[![License](https://img.shields.io/github/license/gildas/wess)](https://github.com/gildas/wess/blob/master/LICENSE)
[![GoDoc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/gildas/wess)
[![License](https://img.shields.io/github/license/gildas/wess)](https://github.com/gildas/wess/blob/master/LICENSE)
[![Report](https://goreportcard.com/badge/github.com/gildas/wess)](https://goreportcard.com/report/github.com/gildas/wess)

![master](https://img.shields.io/badge/branch-master-informational)
Expand All @@ -23,9 23,9 @@ Running a WESS instance is very easy, by default on port 80 all interfaces:

```go
func main() {
server := wess.NewServer(wess.ServerOptions{})
shutdown, stop, _ := server.Start(context.Background())
err = <-shutdown
server := wess.NewServer(wess.ServerOptions{})
shutdown, stop, _ := server.Start(context.Background())
err = <-shutdown
}
```

Expand All @@ -34,32 34,36 @@ func main() {
Of course that server does not serve much...

You can change the port, give an address to listen to:

```go
server := wess.NewServer(wess.ServerOptions{
Address: "192.168.1.1",
Port: 8000,
})
server := wess.NewServer(wess.ServerOptions{
Address: "192.168.1.1",
Port: 8000,
})
```

You can also overwrite the default handlers used when a route is not found or a method is not Allowed:

```go
server := wess.NewServer(wess.ServerOptions{
Logger: log,
NotFoundHandler: notFoundHandler(log),
MethodNotAllowedHandler: methodNotAllowedHandler(Options),
})
server := wess.NewServer(wess.ServerOptions{
Logger: log,
NotFoundHandler: notFoundHandler(log),
MethodNotAllowedHandler: methodNotAllowedHandler(Options),
})
```

If you add a `ProbePort`, `wess` will also serve some _health_ routes for Kubernetes or other probe oriented environments. These following routes are available:

- `/healthz/liveness`
- `/healthz/readiness`

You can change the root path from `/healthz` with the `HealthRootPath` option:

```go
server := wess.NewServer(wess.ServerOptions{
ProbePort: 32000,
HealthRootPath: "/probez",
})
server := wess.NewServer(wess.ServerOptions{
ProbePort: 32000,
HealthRootPath: "/probez",
})
```

**Note:** If the probe port is the same as the main port, all routes are handled by the same web server. Otherwise, 2 web servers are instantiated.
Expand All @@ -69,34 73,35 @@ You can change the root path from `/healthz` with the `HealthRootPath` option:
You can add a simple route with `AddRoute` and `AddRouteWithFunc`:

```go
server.AddRoute("GET", "/something", somethingHandler)
server.AddRouteWithFunc("GET", "/somewhere", somewhereFunc)
server.AddRoute("GET", "/something", somethingHandler)
server.AddRouteWithFunc("GET", "/somewhere", somewhereFunc)
```

The first method accepts a [http.Handler](https://pkg.go.dev/net/http#Handler) while the second accepts a [http.HandlerFunc](https://pkg.go.dev/net/http#HandlerFunc).

Here is an embedded example:

```go
server.AddRouteWithFunc("GET", "/hello", func(w http.ResponseWriter, r *http.Request) {
log := logger.Must(logger.FromContext(r.Context())).Child(nil, "hello")

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/plain")
written, _ := w.Write([]byte("Hello, World!"))
log.Debugf("Witten %d bytes", written)
})
server.AddRouteWithFunc("GET", "/hello", func(w http.ResponseWriter, r *http.Request) {
log := logger.Must(logger.FromContext(r.Context())).Child(nil, "hello")

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "text/plain")
written, _ := w.Write([]byte("Hello, World!"))
log.Debugf("Witten %d bytes", written)
})
```

For more complex cases, you can ask for a [SubRouter](https://pkg.go.dev/github.com/gorilla/mux#Router):

```go
main() {
// ...
APIRoutes(server.SubRouter("/api/v1"), dbClient)
// ...
APIRoutes(server.SubRouter("/api/v1"), dbClient)
}

func APIRoutes(router *mux.Router, db *db.Client) {
router.Method("GET").Path("/users").Handler(GetUsers(db))
router.Method("GET").Path("/users").Handler(GetUsers(db))
}
```

Expand All @@ -119,6 124,7 @@ yarn create vite frontend --template vue
```

During the development phase of the frontend, you should run the dev server directly from the frontend folder:

```sh
cd frontend
npm install
Expand Down Expand Up @@ -149,21 155,22 @@ And add a `wess` server in the parent folder of the frontend:

```go
var (
//go:embed all:frontend/dist
frontendFS embed.FS
//go:embed all:frontend/dist
frontendFS embed.FS
)

func main() {
server := wess.NewServer(wess.ServerOptions{
Port: 8080,
})
_ = server.AddFrontend("/", frontendFS, "frontend/dist")
shutdown, stop, _ := server.Start(context.Background())
<-shutdown
server := wess.NewServer(wess.ServerOptions{
Port: 8080,
})
_ = server.AddFrontend("/", frontendFS, "frontend/dist")
shutdown, stop, _ := server.Start(context.Background())
<-shutdown
}
```

Then, create a single binary that will contain the server and the frontend code:

```sh
go build .
```
Expand Down
62 changes: 35 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 3,52 @@ module github.com/gildas/wess
go 1.20

require (
github.com/gildas/go-core v0.5.4
github.com/gildas/go-errors v0.3.5
github.com/gildas/go-logger v1.6.7
github.com/gildas/go-request v0.7.17
github.com/gorilla/mux v1.8.0
github.com/gildas/go-core v0.5.6
github.com/gildas/go-errors v0.3.6
github.com/gildas/go-logger v1.6.10
github.com/gildas/go-request v0.7.19
github.com/gorilla/mux v1.8.1
github.com/joho/godotenv v1.5.1
github.com/rs/cors v1.9.0
github.com/rs/cors v1.10.1
github.com/stretchr/testify v1.8.4
)

require (
cloud.google.com/go v0.110.6 // indirect
cloud.google.com/go/compute v1.22.0 // indirect
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/logging v1.7.0 // indirect
cloud.google.com/go/longrunning v0.5.1 // indirect
cloud.google.com/go/logging v1.9.0 // indirect
cloud.google.com/go/longrunning v0.5.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/api v0.132.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.154.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 000b283

Please sign in to comment.