Skip to content

Commit

Permalink
feat: /api/v1/version
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephKav committed May 1, 2022
1 parent 9db0453 commit 37d8280
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 45 deletions.
22 changes: 22 additions & 0 deletions web/api/types/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright [2022] [Hymenaios]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

// VersionAPI used to /api/v1/version
type VersionAPI struct {
Version string `json:"version"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
}
15 changes: 0 additions & 15 deletions web/api/types/api.go → web/api/types/hymenaios.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ import (
"time"
)

// WebSocketMessage is the message format to send/receive/forward.
type WebSocketMessage struct {
Version *int `json:"version,omitempty"`
Page *string `json:"page"`
Type *string `json:"type"`
SubType *string `json:"sub_type,omitempty"`
Target *string `json:"target,omitempty"`
Order *[]string `json:"order,omitempty"`
ServiceData *ServiceSummary `json:"service_data,omitempty"`
WebHookData map[string]*WebHookSummary `json:"webhook_data,omitempty"`
InfoData *Info `json:"info_data,omitempty"`
FlagsData *Flags `json:"flags_data,omitempty"`
ConfigData *Config `json:"config_data,omitempty"`
}

// ServiceSummary is the Summary of a Service.
type ServiceSummary struct {
ID *string `json:"id"`
Expand Down
30 changes: 30 additions & 0 deletions web/api/types/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright [2022] [Hymenaios]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

// WebSocketMessage is the message format to send/receive/forward.
type WebSocketMessage struct {
Version *int `json:"version,omitempty"`
Page *string `json:"page"`
Type *string `json:"type"`
SubType *string `json:"sub_type,omitempty"`
Target *string `json:"target,omitempty"`
Order *[]string `json:"order,omitempty"`
ServiceData *ServiceSummary `json:"service_data,omitempty"`
WebHookData map[string]*WebHookSummary `json:"webhook_data,omitempty"`
InfoData *Info `json:"info_data,omitempty"`
FlagsData *Flags `json:"flags_data,omitempty"`
ConfigData *Config `json:"config_data,omitempty"`
}
21 changes: 0 additions & 21 deletions web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@
package v1

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

"github.com/gorilla/mux"
"github.com/hymenaios-io/Hymenaios/config"
"github.com/hymenaios-io/Hymenaios/utils"
"github.com/hymenaios-io/Hymenaios/web/ui"
"github.com/vearutop/statigz"
"github.com/vearutop/statigz/brotli"
)

// API is the API to use for the webserver.
Expand All @@ -50,20 +46,3 @@ func NewAPI(cfg *config.Config, log *utils.JLog) *API {
baseRouter.Handle(routePrefix, http.RedirectHandler(routePrefix+"/", 302))
return api
}

// SetupRoutesNodeJS will setup the HTTP routes to the NodeJS files.
func (api *API) SetupRoutesNodeJS() {
nodeRoutes := []string{
"/approvals",
"/config",
"/flags",
"/status",
}
for _, route := range nodeRoutes {
prefix := strings.TrimRight(api.RoutePrefix, "/") + route
api.Router.Handle(route, http.StripPrefix(prefix, statigz.FileServer(ui.GetFS().(fs.ReadDirFS), brotli.AddEncoding)))
}

// Catch-all for JS, CSS, etc...
api.Router.PathPrefix("/").Handler(http.StripPrefix(api.RoutePrefix, statigz.FileServer(ui.GetFS().(fs.ReadDirFS), brotli.AddEncoding)))
}
64 changes: 64 additions & 0 deletions web/api/v1/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright [2022] [Hymenaios]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1

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

"github.com/hymenaios-io/Hymenaios/utils"
api_types "github.com/hymenaios-io/Hymenaios/web/api/types"
"github.com/hymenaios-io/Hymenaios/web/ui"
"github.com/vearutop/statigz"
"github.com/vearutop/statigz/brotli"
)

// SetupRoutesAPI will setup the HTTP API routes.
func (api *API) SetupRoutesAPI() {
api.Router.HandleFunc("/api/v1/version", func(w http.ResponseWriter, r *http.Request) {
api.httpVersion(w, r)
})
}

// SetupRoutesNodeJS will setup the HTTP routes to the NodeJS files.
func (api *API) SetupRoutesNodeJS() {
nodeRoutes := []string{
"/approvals",
"/config",
"/flags",
"/status",
}
for _, route := range nodeRoutes {
prefix := strings.TrimRight(api.RoutePrefix, "/") + route
api.Router.Handle(route, http.StripPrefix(prefix, statigz.FileServer(ui.GetFS().(fs.ReadDirFS), brotli.AddEncoding)))
}

// Catch-all for JS, CSS, etc...
api.Router.PathPrefix("/").Handler(http.StripPrefix(api.RoutePrefix, statigz.FileServer(ui.GetFS().(fs.ReadDirFS), brotli.AddEncoding)))
}

// httpVersion serves Hymenaios version JSON over HTTP.
func (api *API) httpVersion(w http.ResponseWriter, r *http.Request) {
logFrom := utils.LogFrom{Primary: "apiVersion"}
api.Log.Verbose("-", logFrom, true)
err := json.NewEncoder(w).Encode(api_types.VersionAPI{
Version: utils.Version,
BuildDate: utils.BuildDate,
GoVersion: utils.GoVersion,
})
api.Log.Error(err, logFrom, err != nil)
}
2 changes: 1 addition & 1 deletion web/api/v1/websocket-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (c *Client) writePump() {
switch *msg.Type {
case "VERSION":
// Approval/Skip
c.api.wsVersion(c, msg)
c.api.wsServiceAction(c, msg)
case "WEBHOOK":
// Get WebHook data for a service
c.api.wsWebHook(c, msg)
Expand Down
11 changes: 5 additions & 6 deletions web/api/v1/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

"github.com/hymenaios-io/Hymenaios/utils"
"github.com/hymenaios-io/Hymenaios/web/api/types"
api_types "github.com/hymenaios-io/Hymenaios/web/api/types"
)

Expand Down Expand Up @@ -101,13 +100,13 @@ func (api *API) wsDefaults(client *Client) {
}
}

// wsVersion handles approvals/rejections of the latest version of a service.
// wsServiceAction handles approvals/rejections of the latest version of a service.
//
// Required params:
//
// service_data.id - Service ID to approve/deny release.
func (api *API) wsVersion(client *Client, payload api_types.WebSocketMessage) {
logFrom := utils.LogFrom{Primary: "wsVersion", Secondary: client.ip}
func (api *API) wsServiceAction(client *Client, payload api_types.WebSocketMessage) {
logFrom := utils.LogFrom{Primary: "wsServiceAction", Secondary: client.ip}
api.Log.Verbose("-", logFrom, true)

if payload.ServiceData.ID == nil {
Expand Down Expand Up @@ -335,7 +334,7 @@ func (api *API) wsConfigDefaults(client *Client) {
api.Config.Defaults.Service.AccessToken,
"<secret>"),
AllowInvalidCerts: api.Config.Defaults.Service.AllowInvalidCerts,
DeployedVersionLookup: &types.DeployedVersionLookup{
DeployedVersionLookup: &api_types.DeployedVersionLookup{
AllowInvalidCerts: api.Config.Defaults.Service.DeployedVersionLookup.AllowInvalidCerts,
},
},
Expand Down Expand Up @@ -538,7 +537,7 @@ func (api *API) wsConfigService(client *Client) {

// DeployedVersionLookup
if service.DeployedVersionLookup != nil {
deployedVersionLookup := types.DeployedVersionLookup{}
deployedVersionLookup := api_types.DeployedVersionLookup{}
// URL
if service.DeployedVersionLookup.URL != "" {
deployedVersionLookup.URL = service.DeployedVersionLookup.URL
Expand Down
2 changes: 1 addition & 1 deletion web/ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/ui/static/static/js/main.a1148610.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func NewRouter(cfg *config.Config, jLog *utils.JLog, hub *api_v1.Hub) *mux.Route
api_v1.ServeWs(api, hub, w, r)
})

// HTTP API
api.SetupRoutesAPI()
// NodeJS
api.SetupRoutesNodeJS()

Expand Down

0 comments on commit 37d8280

Please sign in to comment.