Skip to content

Commit

Permalink
Merge pull request #151 from eljeko/main
Browse files Browse the repository at this point in the history
UI update
  • Loading branch information
eljeko authored Aug 12, 2024
2 parents ceee8b9 + 64fbf0e commit 16c0383
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 249 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ require (
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.3.0 // indirect
github.com/hamba/avro/v2 v2.20.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ github.com/googleapis/gax-go/v2 v2.12.5 h1:8gw9KZK8TiVKB6q3zHY3SBzLnrGp6HQjyfYBY
github.com/googleapis/gax-go/v2 v2.12.5/go.mod h1:BUDKcWo+RaKq5SC9vVYL0wLADa3VcfswbOMMRmB9H3E=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.3.0 h1:XYlkq7KcpOB2ZhHBPv5WpjMIxrQosiZanfoy1HLZFzg=
github.com/gorilla/sessions v1.3.0/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
Expand Down
217 changes: 0 additions & 217 deletions pkg/cmd/html/emitters.html

This file was deleted.

11 changes: 0 additions & 11 deletions pkg/cmd/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ <h1 class="p-1 m-2 border-3">Web UI</h1>

<!--div class="d-flex justify-content-center m-2"-->
<div class="d-flex justify-content-center p-2">

<div class="card m-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"><i class="fa-solid fa-rocket"></i> Emitters web UI</h5>
<p class="card-text">First (alpha) version to manage the configured JR Emitters</p>
</div>
<div class="d-flex justify-content-center m-2">
<a href="/emitters.html" class="btn btn-primary">Go to Emitters</a>
</div>
</div>

<div class="card m-2" style="width: 18rem;">

<div class="card-body">
Expand Down
61 changes: 40 additions & 21 deletions pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ package cmd

import (
"bytes"
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"path/filepath"
"strings"
"text/template"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/gorilla/sessions"
"github.com/spf13/cobra"
"github.com/ugol/jr/pkg/configuration"
"github.com/ugol/jr/pkg/constants"
"github.com/ugol/jr/pkg/emitter"
"github.com/ugol/jr/pkg/functions"
"log"
"net/http"
"path/filepath"
"strings"
"text/template"
"time"
)

//go:embed html/index.html
var index_html string

//go:embed html/emitters.html
var emitters_html string

//go:embed html/templatedev.html
var templatedev_html string

Expand Down Expand Up @@ -63,12 +63,11 @@ var font_awesome_js string
//go:embed html/images/jr_logo.png
var jr_logo_png []byte

var lastTemplateSubmittedValue []byte
var lastTemplateSubmittedisJsonOutputValue []byte

var firstRun = make(map[string]bool)
var emitterToRun = make(map[string][]emitter.Emitter)

var store = sessions.NewCookieStore([]byte("templates"))

var serverCmd = &cobra.Command{
Use: "server",
Short: "Starts the jr http server",
Expand All @@ -95,6 +94,7 @@ var serverCmd = &cobra.Command{
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.Timeout(60 * time.Second))
router.Use(SessionMiddleware)

//comment for local dev
embeddedFileRoutes(router)
Expand Down Expand Up @@ -135,6 +135,14 @@ var serverCmd = &cobra.Command{
},
}

func SessionMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
r = r.WithContext(context.WithValue(r.Context(), "session", session))
next.ServeHTTP(w, r)
})
}

func embeddedFileRoutes(router chi.Router) {

router.Get("/", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -145,10 +153,6 @@ func embeddedFileRoutes(router chi.Router) {
w.Write([]byte(index_html))
})

router.Get("/emitters.html", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(emitters_html))
})

router.Get("/templatedev.html", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(templatedev_html))
})
Expand Down Expand Up @@ -357,9 +361,18 @@ func loadLastStatus(w http.ResponseWriter, r *http.Request) {
var response bytes.Buffer

response.WriteString("{")
lastTemplateSubmittedValueB64 := base64.StdEncoding.EncodeToString(lastTemplateSubmittedValue)

session := r.Context().Value("session").(*sessions.Session)
lastTemplateSubmittedValue_without_type, _ := session.Values["lastTemplateSubmittedValue"]
lastTemplateSubmittedValue, _ := lastTemplateSubmittedValue_without_type.(string)

lastTemplateSubmittedisJsonOutputValue_without_type, _ := session.Values["lastTemplateSubmittedisJsonOutputValue"]
lastTemplateSubmittedisJsonOutputValue, _ := lastTemplateSubmittedisJsonOutputValue_without_type.(string)

lastTemplateSubmittedValueB64 := base64.StdEncoding.EncodeToString([]byte(lastTemplateSubmittedValue))

response.WriteString("\"template\": \"" + lastTemplateSubmittedValueB64 + "\",")
response.WriteString("\"isJsonOutput\": \"" + string(lastTemplateSubmittedisJsonOutputValue) + "\"")
response.WriteString("\"isJsonOutput\": \"" + lastTemplateSubmittedisJsonOutputValue + "\"")
response.WriteString("}")

_, err := w.Write(response.Bytes())
Expand All @@ -371,16 +384,22 @@ func loadLastStatus(w http.ResponseWriter, r *http.Request) {
func executeTemplate(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "plain/text")

errorFormParse := r.ParseForm()
if errorFormParse != nil {
log.Println("errorFormParse ", errorFormParse)
http.Error(w, errorFormParse.Error(), http.StatusInternalServerError)
}

lastTemplateSubmittedValue = []byte(r.Form.Get("template"))
lastTemplateSubmittedisJsonOutputValue = []byte(r.Form.Get("isJsonOutput"))
var lastTemplateSubmittedValue = r.Form.Get("template")
var lastTemplateSubmittedisJsonOutputValue = r.Form.Get("isJsonOutput")

session := r.Context().Value("session").(*sessions.Session)
session.Values["lastTemplateSubmittedValue"] = lastTemplateSubmittedValue
session.Values["lastTemplateSubmittedisJsonOutputValue"] = lastTemplateSubmittedisJsonOutputValue
session.Save(r, w)

templateParsed, errValidity := template.New("").Funcs(functions.FunctionsMap()).Parse(string(lastTemplateSubmittedValue))
templateParsed, errValidity := template.New("").Funcs(functions.FunctionsMap()).Parse(lastTemplateSubmittedValue)

if errValidity != nil {
log.Println("errValidity ", errValidity)
Expand Down

0 comments on commit 16c0383

Please sign in to comment.