Skip to content

Commit

Permalink
Merge branch 'main' into wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
ugol authored Aug 23, 2024
2 parents aab31d2 + 6a01114 commit 3a78e76
Show file tree
Hide file tree
Showing 43 changed files with 461 additions and 388 deletions.
2 changes: 1 addition & 1 deletion .go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.22
1.23
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM golang AS builder
MAINTAINER Ugo Landini <ugo@confluent.io>

ARG VERSION=0.3.9
ARG VERSION=0.4.0
ARG GOVERSION=$(go version)
ARG USER=$(id -u -n)
ARG TIME=$(date)
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM golang:1.22-alpine AS builder
MAINTAINER Ugo Landini <ugo@confluent.io>

ARG VERSION=0.3.9
ARG VERSION=0.4.0
ARG GOVERSION=$(go version)
ARG USER=$(id -u -n)
ARG TIME=$(date)
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.scratch
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM golang:1.22-alpine AS builder
MAINTAINER Ugo Landini <ugo@confluent.io>

ARG VERSION=0.3.9
ARG VERSION=0.4.0
ARG GOVERSION=$(go version)
ARG USER=$(id -u -n)
ARG TIME=$(date)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.3.9
VERSION=0.4.0
GOVERSION=$(shell go version)
USER=$(shell id -u -n)
TIME=$(shell date)
Expand Down Expand Up @@ -89,7 +89,7 @@ vet:
go vet

lint:
golangci-lint run --enable-all
golangci-lint run --config .localci/lint/golangci.yml --out-format tab

help: hello
@echo ''
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/squeeze69/generacodicefiscale v1.0.5
github.com/stretchr/testify v1.9.0
github.com/vadv/gopher-lua-libs v0.5.0
github.com/yuin/gopher-lua v1.1.1
go.mongodb.org/mongo-driver v1.16.0
Expand Down Expand Up @@ -73,6 +74,7 @@ require (
github.com/cbroglie/mustache v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cheggaaa/pb/v3 v3.0.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand Down Expand Up @@ -116,6 +118,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/createTopic.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ jr createTopic newDefaultTopic
Run: func(cmd *cobra.Command, args []string) {
kafkaConfig, _ := cmd.Flags().GetString("kafkaConfig")

kManager := &kafka.KafkaManager{}
kManager := &kafka.Manager{}
kManager.Initialize(kafkaConfig)
partitions, _ := cmd.Flags().GetInt("partitions")
replica, _ := cmd.Flags().GetInt("replica")
kManager.CreateTopicFull(args[0], partitions, replica)
kManager.CreateTopicFull(cmd.Context(), args[0], partitions, replica)

},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/emitterRun.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func RunEmitters(ctx context.Context, emitterNames []string, ems map[string][]em
defer emitter.WriteStats()
defer emitter.CloseProducers(ctx, ems)
emittersToRun := emitter.Initialize(ctx, emitterNames, ems, dryrun)
emitter.DoLoop(emittersToRun)
emitter.DoLoop(ctx, emittersToRun)
}

func init() {
Expand Down
68 changes: 35 additions & 33 deletions pkg/cmd/functionList.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,24 @@ func doList(cmd *cobra.Command, args []string) {
isMarkdown, _ := cmd.Flags().GetBool("markdown")
noColor, _ := cmd.Flags().GetBool("nocolor")

if category && len(args) > 0 {
switch {
case category && len(args) > 0:
var functionNames []string
for k, v := range functions.DescriptionMap() {
if v.Category == args[0] {
functionNames = append(functionNames, k)
}
}
sortAndPrint(functionNames, isMarkdown, noColor)
} else if find && len(args) > 0 {
case find && len(args) > 0:
var functionNames []string
for k, v := range functions.DescriptionMap() {
if strings.Contains(v.Description, args[0]) || strings.Contains(v.Name, args[0]) {
functionNames = append(functionNames, k)
}
}
sortAndPrint(functionNames, isMarkdown, noColor)
} else if len(args) == 1 {

case len(args) == 1:
if run {
f, found := printFunction(args[0], isMarkdown, noColor)
if found {
Expand All @@ -87,9 +87,7 @@ func doList(cmd *cobra.Command, args []string) {
} else {
printFunction(args[0], isMarkdown, noColor)
}
} else {

//l := len(functions.FunctionsMap())
default:
l := len(functions.DescriptionMap())
functionNames := make([]string, l)

Expand All @@ -101,14 +99,15 @@ func doList(cmd *cobra.Command, args []string) {
sortAndPrint(functionNames, isMarkdown, noColor)

}

fmt.Println()
}

func sortAndPrint(functionNames []string, isMarkdown bool, noColor bool) {
slices.Sort(functionNames)
for _, k := range functionNames {
printFunction(k, isMarkdown, noColor)
//fmt.Println(k)
// fmt.Println(k)
}
fmt.Println()
fmt.Printf("Total functions: %d\n", len(functionNames))
Expand All @@ -124,34 +123,37 @@ func printFunction(name string, isMarkdown bool, noColor bool) (functions.Functi
Reset = "\033[0m"
}

if found {
if isMarkdown {
fmt.Println()
fmt.Printf("### %s \n", f.Name)
fmt.Printf("**Category:** %s\\\n", f.Category)
fmt.Printf("**Description:** %s\\\n", f.Description)

if len(f.Parameters) > 0 {
fmt.Printf("**Parameters:** `%s`\\\n", f.Parameters)
} else {
fmt.Printf("**Parameters:** %s \\\n", f.Parameters)
}
fmt.Printf("**Localizable:** `%v`\\\n", f.Localizable)
fmt.Printf("**Return:** `%s`\\\n", f.Return)
fmt.Printf("**Example:** `%s`\\\n", f.Example)
fmt.Printf("**Output:** `%s`\n", f.Output)
if !found {
return f, found
}

if isMarkdown {
fmt.Println()
fmt.Printf("### %s \n", f.Name)
fmt.Printf("**Category:** %s\\\n", f.Category)
fmt.Printf("**Description:** %s\\\n", f.Description)

if len(f.Parameters) > 0 {
fmt.Printf("**Parameters:** `%s`\\\n", f.Parameters)
} else {
fmt.Println()
fmt.Printf("%sName: %s%s\n", Cyan, Reset, f.Name)
fmt.Printf("%sCategory: %s%s\n", Cyan, Reset, f.Category)
fmt.Printf("%sDescription: %s%s\n", Cyan, Reset, f.Description)
fmt.Printf("%sParameters: %s%s\n", Cyan, Reset, f.Parameters)
fmt.Printf("%sLocalizable: %s%v\n", Cyan, Reset, f.Localizable)
fmt.Printf("%sReturn: %s%s\n", Cyan, Reset, f.Return)
fmt.Printf("%sExample: %s%s\n", Cyan, Reset, f.Example)
fmt.Printf("%sOutput: %s%s\n", Cyan, Reset, f.Output)
fmt.Printf("**Parameters:** %s \\\n", f.Parameters)
}
fmt.Printf("**Localizable:** `%v`\\\n", f.Localizable)
fmt.Printf("**Return:** `%s`\\\n", f.Return)
fmt.Printf("**Example:** `%s`\\\n", f.Example)
fmt.Printf("**Output:** `%s`\n", f.Output)
} else {
fmt.Println()
fmt.Printf("%sName: %s%s\n", Cyan, Reset, f.Name)
fmt.Printf("%sCategory: %s%s\n", Cyan, Reset, f.Category)
fmt.Printf("%sDescription: %s%s\n", Cyan, Reset, f.Description)
fmt.Printf("%sParameters: %s%s\n", Cyan, Reset, f.Parameters)
fmt.Printf("%sLocalizable: %s%v\n", Cyan, Reset, f.Localizable)
fmt.Printf("%sReturn: %s%s\n", Cyan, Reset, f.Return)
fmt.Printf("%sExample: %s%s\n", Cyan, Reset, f.Example)
fmt.Printf("%sOutput: %s%s\n", Cyan, Reset, f.Output)
}

return f, found

}
Expand Down
71 changes: 45 additions & 26 deletions pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ var emitterToRun = make(map[string][]emitter.Emitter)

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

type serverKey string

const (
sessionKey serverKey = "session"
)

var serverCmd = &cobra.Command{
Use: "server",
Short: "Starts the jr http server",
Expand Down Expand Up @@ -153,14 +159,24 @@ var serverCmd = &cobra.Command{

addr := fmt.Sprintf(":%d", port)
log.Info().Int("port", port).Msg("Starting HTTP server")
log.Fatal().Err(http.ListenAndServe(addr, router))

// TODO: must validate values
s := &http.Server{
Addr: addr,
ReadHeaderTimeout: 20 * time.Second,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 2 * time.Minute,
Handler: router,
}

log.Fatal().Err(s.ListenAndServe())
},
}

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))
r = r.WithContext(context.WithValue(r.Context(), sessionKey, session))
next.ServeHTTP(w, r)
})
}
Expand Down Expand Up @@ -244,7 +260,7 @@ func FileServer(r chi.Router, path string, root http.FileSystem) {
}

if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
path += "*"
Expand All @@ -260,7 +276,7 @@ func FileServer(r chi.Router, path string, root http.FileSystem) {
func listEmitters(w http.ResponseWriter, r *http.Request) {
emitters_json, _ := json.Marshal(emitters)

_, err := w.Write([]byte(emitters_json))
_, err := w.Write(emitters_json)
if err != nil {
log.Error().Err(err).Msg("Error writing response")
}
Expand Down Expand Up @@ -342,22 +358,25 @@ func runEmitter(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
url := chi.URLParam(r, "emitter")
if firstRun[url] == false {
for i := 0; i < len(emitters); i++ {
if functions.Contains([]string{url}, emitters[i].Name) {
emitters[i].Initialize(r.Context(), configuration.GlobalCfg)
emitterToRun[url] = append(emitterToRun[url], emitters[i])
if emitters[i].Preload > 0 {
emitters[i].Run(emitters[i].Preload, w)
} else {
emitters[i].Run(emitters[i].Num, w)
}
firstRun[url] = true
}
}
} else {

if firstRun[url] {
for _, e := range emitterToRun[url] {
e.Run(e.Num, w)
e.Run(r.Context(), e.Num, w)
}

return
}

for i := 0; i < len(emitters); i++ {
if functions.Contains([]string{url}, emitters[i].Name) {
emitters[i].Initialize(r.Context(), configuration.GlobalCfg)
emitterToRun[url] = append(emitterToRun[url], emitters[i])
if emitters[i].Preload > 0 {
emitters[i].Run(r.Context(), emitters[i].Preload, w)
} else {
emitters[i].Run(r.Context(), emitters[i].Num, w)
}
firstRun[url] = true
}
}

Expand All @@ -380,12 +399,12 @@ func loadLastStatus(w http.ResponseWriter, r *http.Request) {

response.WriteString("{")

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

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

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

Expand All @@ -412,7 +431,7 @@ func executeTemplate(w http.ResponseWriter, r *http.Request) {
var lastTemplateSubmittedValue = r.Form.Get("template")
var lastTemplateSubmittedisJsonOutputValue = r.Form.Get("isJsonOutput")

session := r.Context().Value("session").(*sessions.Session)
session := r.Context().Value(sessionKey).(*sessions.Session)
session.Values["lastTemplateSubmittedValue"] = lastTemplateSubmittedValue
session.Values["lastTemplateSubmittedisJsonOutputValue"] = lastTemplateSubmittedisJsonOutputValue
session.Save(r, w)
Expand All @@ -434,7 +453,7 @@ func executeTemplate(w http.ResponseWriter, r *http.Request) {
return
}

_, err := w.Write([]byte(b.String()))
_, err := w.Write(b.Bytes())
if err != nil {
log.Error().Err(err).Msg("Error writing response")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/templateShow.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var templateShowCmd = &cobra.Command{
if err != nil {
log.Fatal().Err(err).Msg("Failed to ReadFile")
}
valid, err := isValidTemplate([]byte(templateScript))
valid, err := isValidTemplate(templateScript)
if err != nil {
log.Fatal().Err(err).Msg("Failed to read a template")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"
)

var JrContext Context
var JrContext *Context

// Context is the object passed on the templates which contains all the needed details.
type Context struct {
Expand All @@ -50,7 +50,7 @@ type Context struct {

func init() {

JrContext = Context{
JrContext = &Context{
StartTime: time.Now(),
GeneratedBytes: 0,
GeneratedObjects: 0,
Expand Down
Loading

0 comments on commit 3a78e76

Please sign in to comment.