Serve both UI assets and REST API from handler

This commit is contained in:
Ken Hibino
2021-10-10 06:33:38 -07:00
parent b20cf02f3b
commit ccdd6cea01
35 changed files with 620 additions and 234 deletions

121
README.md
View File

@@ -2,6 +2,10 @@
# A modern web based tool for monitoring & administrating [Asynq](https://github.com/hibiken/asynq) queues, tasks and message broker
## Overview
Asynqmon is both a library that you can include in your web application, as well as a binary that you can simply install and run.
## Version Compatibility
| Asynq version | WebUI (asynqmon) version |
@@ -9,7 +13,7 @@
| 0.18.x | 0.2.x |
| 0.16.x, 0.17.x | 0.1.x |
## Install
## Install the binary
### Release binaries
@@ -47,57 +51,7 @@ To build Docker image locally, run:
make docker
```
### Importing into projects
You can import `asynqmon` into other projects and create a single binary to serve other components of `asynq` and `asynqmon` from a single binary.
<details><summary>Example</summary>
<p>
> `staticContents` can be embedded by using the pre-built UI bundle from the Releases section.
```go
package main
import (
"embed"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/hibiken/asynq"
"github.com/hibiken/asynqmon"
)
//go:embed ui-assets/*
var staticContents embed.FS
func main() {
h := asynqmon.New(asynqmon.Options{
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
})
defer h.Close()
r := mux.NewRouter()
r.PathPrefix("/api").Handler(h)
// Add static content handler or other handlers
// r.PathPrefix("/").Handler( /* &staticContentHandler{staticContents} */ )
srv := &http.Server{
Handler: r,
Addr: ":8080",
}
log.Fatal(srv.ListenAndServe())
}
```
</p>
</details>
## Run
## Run the binary
To use the defaults, simply run and open http://localhost:8080.
@@ -171,6 +125,69 @@ Next, go to [localhost:8080](http://localhost:8080) and see Asynqmon dashboard:
![Web UI Settings and adaptive dark mode](https://user-images.githubusercontent.com/11155743/114697149-3517c380-9d26-11eb-9f7a-ae2dd00aad5b.png)
### Importing into projects
Asynqmon is also a library which can be imported into an existing web application.
Example with [net/http](https://pkg.go.dev/net/http):
```go
package main
import (
"log"
"net/http"
"github.com/hibiken/asynq"
"github.com/hibiken/asynqmon"
)
func main() {
h := asynqmon.New(asynqmon.Options{
RootPath: "/monitoring", // RootPath specifies the root for asynqmon app
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
})
http.Handle(h.RootPath(), h)
// Go to http://localhost:8080/monitoring to see asynqmon homepage.
log.Fatal(http.ListenAndServe(":8000", nil))
}
```
Example with [gorilla/mux](https://pkg.go.dev/github.com/gorilla/mux):
```go
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/hibiken/asynq"
"github.com/hibiken/asynqmon"
)
func main() {
h := asynqmon.New(asynqmon.Options{
RootPath: "/monitoring", // RootPath specifies the root for asynqmon app
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
})
r := mux.NewRouter()
r.PathPrefix(h.RootPath()).Handler(h)
srv := &http.Server{
Handler: r,
Addr: ":8080",
}
// Go to http://localhost:8080/monitoring to see asynqmon homepage.
log.Fatal(srv.ListenAndServe())
}
```
## License
Copyright (c) 2019-present [Ken Hibino](https://github.com/hibiken) and [Contributors](https://github.com/hibiken/asynqmon/graphs/contributors). `Asynqmon` is free and open-source software licensed under the [MIT License](https://github.com/hibiken/asynq/blob/master/LICENSE). Official logo was created by [Vic Shóstak](https://github.com/koddr) and distributed under [Creative Commons](https://creativecommons.org/publicdomain/zero/1.0/) license (CC0 1.0 Universal).