diff --git a/go.mod b/go.mod index e0108436..195f685a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d36c6cea..d2679108 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/cmd/html/emitters.html b/pkg/cmd/html/emitters.html deleted file mode 100644 index 9a9926a1..00000000 --- a/pkg/cmd/html/emitters.html +++ /dev/null @@ -1,217 +0,0 @@ - - - - - - - JR Emitters - - - - - - - - - -
- JR

Emitters

-
-
- - -
- -
-
-
- Home -
-
- - - - - - - \ No newline at end of file diff --git a/pkg/cmd/html/index.html b/pkg/cmd/html/index.html index ebe88c03..a4d04c44 100644 --- a/pkg/cmd/html/index.html +++ b/pkg/cmd/html/index.html @@ -24,17 +24,6 @@

Web UI

- -
-
-
Emitters web UI
-

First (alpha) version to manage the configured JR Emitters

-
- -
-
diff --git a/pkg/cmd/server.go b/pkg/cmd/server.go index 36d3ff56..62a6f320 100644 --- a/pkg/cmd/server.go +++ b/pkg/cmd/server.go @@ -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 @@ -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", @@ -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) @@ -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) { @@ -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)) }) @@ -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()) @@ -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)