Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nto add-swagger-ui

Signed-off-by: Patricia Reinoso <[email protected]>
  • Loading branch information
patriciareinoso committed Jul 17, 2024
2 parents 70a5bcb + 5ed83b6 commit 1b7d2c2
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- uses: actions/checkout@v4

- name: reuse lint
uses: fsfe/reuse-action@v3
uses: fsfe/reuse-action@v4

fossa-check:
runs-on: ubuntu-latest
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ $(GO_BIN_PATH)/$(WEBCONSOLE): server.go $(WEBCONSOLE_GO_FILES)

vpath %.go $(addprefix $(GO_SRC_PATH)/, $(GO_NF))

webconsole-ui: $(GO_BIN_PATH)/$(WEBCONSOLE)-ui

$(GO_BIN_PATH)/$(WEBCONSOLE)-ui: server.go $(WEBCONSOLE_GO_FILES)
@echo "Start building $(@F)...."
go build --tags ui -o $(ROOT_PATH)/$@ ./server.go

clean:
rm -rf $(WEBCONSOLE)/$(GO_BIN_PATH)/$(WEBCONSOLE)
rm -rf $(ROOT_PATH)/$(GO_BIN_PATH)/$(WEBCONSOLE)
rm -rf $(ROOT_PATH)/$(GO_BIN_PATH)/$(WEBCONSOLE)-ui

docker-build:
@go mod vendor
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ features Configuration Service provides APIs for subscriber management.
4. 5G clients can connect & get complete configuration copy through grpc interface.
5. 4G clients communicate with Webconsole through REST interface

# UI

Webconsole can optionally serve static files, which constitute the frontend part of the application.

To build webui with a frontend, place the static files under `webconsole/ui/frontend_files` before compilation.

To build the webconsole including the UI option:
```
make webconsole-ui
```

Access the UI at:
```
http://<webconsole-ip>:5000/
```

An example static file has been placed in the `webconsole/ui/frontend_files` directory.

## Webconsole Architecture diagram

![Architecture](/docs/images/architecture1.png)
Expand Down
4 changes: 2 additions & 2 deletions backend/webui_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ swag init -g backend/webui_service/swagger_ui_service.go --outputTypes go
```
The `docs.go` file will automatically be created in `webconsole/docs`

The swagger UI runs by default on `localhost`. If the webconsole server runs remotely, set the following environment variable.
The swagger UI operations are executed by default on `localhost`. If the webconsole server runs remotely, set the following environment variable.
```
export SWAGGER_HOST=<webconsole-ip>
export WEBUI_ENDPOINT=<webconsole-ip>:5000
```
Build the webconsole including the UI option:
```
Expand Down
16 changes: 16 additions & 0 deletions backend/webui_service/dummy_ui_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 Canonical Ltd.

//go:build !ui

package webui_service

import (
"github.com/gin-gonic/gin"
"github.com/omec-project/webconsole/backend/logger"
)

func AddUiService(engine *gin.Engine) *gin.RouterGroup {
logger.WebUILog.Infoln("UI service will not be added")
return nil
}
7 changes: 3 additions & 4 deletions backend/webui_service/swagger_ui_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import (
// @BasePath /
func AddSwaggerUiService(engine *gin.Engine) {
logger.WebUILog.Infoln("Adding Swagger UI service")
host := os.Getenv("SWAGGER_HOST")
if host != "" {
docs.SwaggerInfo.Host = host + ":5000"
logger.WebUILog.Infoln(docs.SwaggerInfo.Host)
endpoint := os.Getenv("WEBUI_ENDPOINT")
if endpoint != "" {
docs.SwaggerInfo.Host = endpoint
}
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
42 changes: 42 additions & 0 deletions backend/webui_service/ui_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 Canonical Ltd.

// +build ui

package webui_service

import (
"io/fs"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/omec-project/webconsole/backend/logger"
"github.com/omec-project/webconsole/ui"
)

func AddUiService(engine *gin.Engine) {
logger.WebUILog.Infoln("Adding UI service")
staticFilesSystem, err := fs.Sub(ui.FrontendFS, "frontend_files")
if err != nil {
logger.WebUILog.Fatal(err)
}

engine.Use(func(c *gin.Context) {
if !isApiUrlPath(c.Request.URL.Path){
htmlPath := strings.TrimPrefix(c.Request.URL.Path, "/") + ".html"
if _, err := staticFilesSystem.Open(htmlPath); err == nil {
c.Request.URL.Path = htmlPath
}
fileServer := http.FileServer(http.FS(staticFilesSystem))
fileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
})
}

func isApiUrlPath(path string) bool{
return strings.HasPrefix(path, "/config/v1/") || strings.HasPrefix(path, "/api/");
}


1 change: 1 addition & 0 deletions backend/webui_service/webui_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (webui *WEBUI) Start() {
/* First HTTP Server running at port to receive Config from ROC */
subconfig_router := logger_util.NewGinWithLogrus(logger.GinLog)
AddSwaggerUiService(subconfig_router)
AddUiService(subconfig_router)
configapi.AddServiceSub(subconfig_router)
configapi.AddService(subconfig_router)

Expand Down
13 changes: 13 additions & 0 deletions ui/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 Canonical Ltd.

// +build ui

package ui

import (
"embed"
)

//go:embed all:frontend_files
var FrontendFS embed.FS
14 changes: 14 additions & 0 deletions ui/frontend_files/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- SPDX-License-Identifier: Apache-2.0 -->
<!-- Copyright 2024 Canonical Ltd. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Webui</title>
</head>
<body>
<h1>Welcome to the webconsole UI</h1>
<h3>This is an example of a static file from a frontend application.</h3>
</body>
</html>

0 comments on commit 1b7d2c2

Please sign in to comment.