Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add static file serving support in webconsole #191

Merged
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
patriciareinoso marked this conversation as resolved.
Show resolved Hide resolved

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
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
}
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 @@ -169,6 +169,7 @@ func (webui *WEBUI) Start() {

/* First HTTP Server running at port to receive Config from ROC */
subconfig_router := logger_util.NewGinWithLogrus(logger.GinLog)
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>