Skip to content

Commit

Permalink
Implement REST API to provide settings to the frontend, as defined in…
Browse files Browse the repository at this point in the history
… openapi spec and unmarshalled from the config file
  • Loading branch information
jdrews committed Jul 30, 2022
1 parent 80f739f commit 70302dd
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
14 changes: 14 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# logstation API

The logstation API provides access to restful actions for the logstation UI. All the logs go over the websocket, but certain things like the syntax colors are queried via REST, which this API provides.

## Building
The REST API is codegen'd on the server and client side through [OpenAPITools/openapi-generator](https://github.com/OpenAPITools/openapi-generator).

### Generate API Server
Install the openapi-generator and run the following in the `logstation` root folder:
```
java -jar .\openapi-generator-cli.jar generate -i .\api\logstation-rest-api.yaml -g go-echo-server -o api\server --additional-properties hideGenerationTimestamp=true
```
This will create a `logstation/api/server` folder which gets imported into `main.go`.
> Note: You may need to remove the `go.mod` after codegen so go doesn't treat `api/server` as it's own go module
22 changes: 22 additions & 0 deletions api/server/handlers/api_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package handlers

import (
"github.com/jdrews/logstation/api/server/models"
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
"net/http"
)

// GetLogstationName - Get Logstation Name
func (c *Container) GetLogstationName(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, models.LogstationName{
Name: viper.GetString("logStationName"),
})
}

// GetSettingsSyntax - Get Syntax Colors
func (c *Container) GetSettingsSyntax(ctx echo.Context) error {
var syntaxColors models.SyntaxColors
viper.UnmarshalKey("syntaxColors", &syntaxColors)
return ctx.JSON(http.StatusOK, syntaxColors)
}
11 changes: 11 additions & 0 deletions api/server/handlers/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package handlers

// Container will hold all dependencies for your application.
type Container struct {
}

// NewContainer returns an empty or an initialized container for your handlers.
func NewContainer() (Container, error) {
c := Container{}
return c, nil
}
8 changes: 8 additions & 0 deletions api/server/models/model_logstation_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

// LogstationName -
type LogstationName struct {

// Name of the logstation server
Name string `json:"name"`
}
11 changes: 11 additions & 0 deletions api/server/models/model_syntax_color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

// SyntaxColor - A syntax color and regex string for highlighting text
type SyntaxColor struct {
Color string `json:"color,omitempty"`

Regex string `json:"regex,omitempty"`
}

// SyntaxColors - An array/slice of syntax colors
type SyntaxColors []SyntaxColor
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/fstab/grok_exporter/tailer/fswatcher"
"github.com/fstab/grok_exporter/tailer/glob"
"github.com/gorilla/websocket"
"github.com/jdrews/logstation/api/server/handlers"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -40,6 +41,14 @@ func main() {

e.Use(middleware.Logger())

c, _ := handlers.NewContainer()

// GetLogstationName - Get Logstation Name
e.GET("/settings/logstation-name", c.GetLogstationName)

// GetSettingsSyntax - Get Syntax Colors
e.GET("/settings/syntax", c.GetSettingsSyntax)

fsys, err := fs.Sub(embeddedFiles, "web/build")
if err != nil {
panic(err)
Expand Down Expand Up @@ -84,7 +93,6 @@ func handleConfigFile() {
}
}
logger.Info("Loaded ", viper.ConfigFileUsed())
logger.Info(viper.AllKeys()) // TODO: Use all the configs to set parameters in logstation
}

func wshandler(c echo.Context, pubSub *pubsub.PubSub) error {
Expand Down

0 comments on commit 70302dd

Please sign in to comment.