Skip to content

Commit

Permalink
Merge pull request #53 from fraunhoferfokus/development
Browse files Browse the repository at this point in the history
updates & HTMX2
  • Loading branch information
JGottschick authored Nov 7, 2024
2 parents e733a3e + 8a154ad commit f15e206
Show file tree
Hide file tree
Showing 27 changed files with 465 additions and 867 deletions.
5 changes: 2 additions & 3 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ download-bootstrap:
curl -o templates/web/css/bootstrap-icons-1.11.3.zip -L https://github.com/twbs/icons/releases/download/v1.11.3/bootstrap-icons-1.11.3.zip
curl -o templates/web/js/bootstrap.bundle.min.js -L https://cdn.jsdelivr.net/npm/bootstrap@latest/dist/js/bootstrap.bundle.min.js
download-style: download-bootstrap
curl -o templates/web/js/htmx.min.js -L https://unpkg.com/htmx.org@latest
curl -o templates/web/js/htmx-sse.js -L https://unpkg.com/htmx.org/dist/ext/sse.js
curl -o templates/web/js/htmx.min.js -L https://unpkg.com/htmx.org@2/dist/htmx.min.js
curl -o templates/web/js/hyperscript.js -L https://unpkg.com/hyperscript.org@latest
curl -o templates/web/js/sse.js -L https://unpkg.com/htmx.org/dist/ext/sse.js
curl -o templates/web/js/sse.js -L https://unpkg.com/htmx-ext-sse@2/sse.js
curl -o templates/web/js/rapidoc-min.js -L https://unpkg.com/rapidoc/dist/rapidoc-min.js
curl -o templates/web/css/simple.min.css -L https://unpkg.com/simpledotcss/simple.min.css
curl -o templates/web/css/pico.min.css -L https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css
Expand Down
2 changes: 1 addition & 1 deletion core/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.10
0.3.11
9 changes: 7 additions & 2 deletions generator/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"os"
"path"
"path/filepath"
"reflect"
"slices"
Expand Down Expand Up @@ -35,6 +36,7 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) {
pagesPath := filepath.Join(frontendPath, "pages")
localesPath := filepath.Join(corePath, "locales")
publicPath := filepath.Join(frontendPath, "public")
docPath := filepath.Join(frontendPath, "doc")

fs.GenerateFolder(frontendPath)
fs.GenerateFolder(javascriptPath)
Expand All @@ -44,13 +46,13 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) {
fs.GenerateFolder(pagesPath)
fs.GenerateFolder(localesPath)
fs.GenerateFolder(publicPath)
fs.GenerateFolder(docPath)

// files in root directory
createFileFromTemplate(filepath.Join(frontendPath, "README.md"), "templates/web/README.md.tmpl", conf)

// files in javascript directory
fs.CopyWebFile("web/js", javascriptPath, "bootstrap.bundle.min.js", true)
fs.CopyWebFile("web/js", javascriptPath, "htmx-sse.js", true)
fs.CopyWebFile("web/js", javascriptPath, "htmx.min.js", true)
fs.CopyWebFile("web/js", javascriptPath, "hyperscript.js", true)
fs.CopyWebFile("web/js", javascriptPath, "sse.js", true)
Expand Down Expand Up @@ -110,7 +112,10 @@ func generateFrontend(spec *openapi3.T, conf GeneratorConfig) {
}

// files in public directory
fs.CopyWebFile("web", publicPath, "README-public.md", false)
fs.CopyWebFile(path.Join("web", "public"), publicPath, "README.md", false)

// files in doc directory
fs.CopyWebFile(path.Join("web", "doc"), docPath, "README.md", false)

// support for events
if spec.Paths.Find("/events") != nil && spec.Paths.Find("/events").Operations()[http.MethodGet] != nil && slices.Contains(spec.Paths.Find("/events").Operations()[http.MethodGet].Tags, "builtin") {
Expand Down
8 changes: 8 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"bufio"
"embed"
"errors"
"net/http"
"os"
"regexp"
Expand Down Expand Up @@ -159,6 +160,13 @@ func generateServerTemplate(spec *openapi3.T, generatorConf GeneratorConfig) (se
log.Debug().Msg("Creating server at port " + strconv.Itoa(int(conf.Port)) + "...")
createFileFromTemplate(filePath, templateFile, conf)

fileName = "mainSvc.go"
filePath = filepath.Join(config.Path, fileName)
templateFile = "templates/mainSvc.go.tmpl"
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
createFileFromTemplate(filePath, templateFile, conf)
}

return conf
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Masterminds/sprig/v3 v3.3.0
github.com/a-h/templ v0.2.663
github.com/cucumber/godog v0.12.5
github.com/getkin/kin-openapi v0.127.0
github.com/getkin/kin-openapi v0.128.0
github.com/gobeam/stringy v0.0.7
github.com/huandu/xstrings v1.5.0
github.com/labstack/echo/v4 v4.12.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/getkin/kin-openapi v0.127.0 h1:Mghqi3Dhryf3F8vR370nN67pAERW+3a95vomb3MAREY=
github.com/getkin/kin-openapi v0.127.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM=
github.com/getkin/kin-openapi v0.128.0 h1:jqq3D9vC9pPq1dGcOCv7yOp1DaEe7c/T1vzcLbITSp4=
github.com/getkin/kin-openapi v0.128.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down
15 changes: 13 additions & 2 deletions templates/Justfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,40 @@ set dotenv-load
program := `basename $PWD`
port := env_var_or_default("{{ upper ( snakecase .ModuleName ) }}_PORT_NB", "9090")

# Show this help page
help:
just -l

# Update code of templ files
templ:
templ generate web/pages/*.templ

# Build service
build: templ
go mod tidy
go build

# Install service locally
install: build
go install

# Run service locally
run: build
go run . -p {{"{{port}}"}}

# Run service locally in debug mode
debug: build
go run . -d -p {{"{{port}}"}}

# Generate code from OpenAPI.yaml
generate:
dredger generate OpenAPI.yaml -o . -f -n {{ .ModuleName }}

# Build SBOM files
sbom-build:
GOARCH=amd64 GOOS=linux cyclonedx-gomod mod -licenses -json -output sbom.json

# Upload SBOM files
sbom: sbom-build
curl -X "POST" "http://dependency-track-api.${DEPENDENCY_TRACK_SERVER}/api/v1/bom" \
-H "Content-Type:multipart/form-data" \
Expand All @@ -43,9 +52,11 @@ docker-build:
docker-run:
docker run --rm -p {{ .Port }}:{{ .Port }} --env-file=.env {{ .ModuleName }}

# Install required tools
required:
go install github.com/a-h/templ/cmd/templ@latest

# Update all required libraries
update: required
go get -u
go mod tidy
Expand All @@ -54,12 +65,12 @@ update: required
todo:
rg -ip "to.?do:"

# push to git
# Push to git
push:
git add .
git commit -a
git push

# changes for git
# Show changes for git
status:
git status -s
2 changes: 1 addition & 1 deletion templates/core/app.env.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ upper ( snakecase .ModuleName ) }}_PORT={{.Port}}
{{ upper ( snakecase .ModuleName ) }}_PORTNB={{.Port}}
{{ upper ( snakecase .ModuleName ) }}_API_KEYS=
{{ upper ( snakecase .ModuleName ) }}_POLICY=
{{ upper ( snakecase .ModuleName ) }}_OPA_SRV=
Expand Down
21 changes: 13 additions & 8 deletions templates/core/config.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type Config struct {
Args []string `ignored:"true"`
Debug bool `default:"false"`
Version bool `default:"false" ignored:"true"`
Service string `default:"{{.ModuleName}}" ignored:"true"`
Expand Down Expand Up @@ -52,17 +53,21 @@ func init() {
AppConfig.Service = service
AppConfig.Host, _ = os.Hostname()
AppConfig.User = os.Getenv("USER")
sid, _ := uuid.NewUUID()
AppConfig.Sid = sid.String()
AppConfig.Sid = uuid.NewString()

// parse command line arguments
flag.StringVar(&AppConfig.PortNb, "port", AppConfig.PortNb, "AppConfig.PortNb to listen by the server")
flag.StringVar(&AppConfig.PortNb, "p", AppConfig.PortNb, "AppConfig.PortNb to listen by the server")
flag.StringVar(&AppConfig.PortNb, "port", AppConfig.PortNb, "Port number to listen by the server")
flag.StringVar(&AppConfig.PortNb, "p", AppConfig.PortNb, "Port number to listen by the server")
flag.StringVar(&AppConfig.Name, "name", AppConfig.Name, "Name of the service")
flag.StringVar(&AppConfig.Name, "n", AppConfig.Name, "Name of the service")
flag.BoolVar(&AppConfig.Debug, "d", AppConfig.Debug, "enable debugging level for logging")
flag.BoolVar(&AppConfig.Version, "V", AppConfig.Version, "print version")

// extend custom flags
initFlags()

flag.Parse()
AppConfig.Args = flag.Args()

log.Debug().Str("name", AppConfig.Name).Any("environment", os.Environ()).Msg("Got environment")

Expand All @@ -85,10 +90,7 @@ func init() {
log.Fatal().Err(err).Str("name", stringy.New(AppConfig.Name).SnakeCase("?", "").ToUpper()).Msg("Couldn't read environment settings")
}

// extend custom flags
initExt()

flag.Parse()
flag.Parse()

// print app version
if AppConfig.Version {
Expand All @@ -103,4 +105,7 @@ func init() {

log.Debug().Str("name", AppConfig.Name).Any("config", AppConfig).Msg("Got configuration")
AppInfo.Name = AppConfig.Name

// extend initialisation
initExt()
}
4 changes: 4 additions & 0 deletions templates/core/configSvc.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ type ConfigExt struct {
}

// define custom cli flags, ...
func initFlags() {
}

// evaluate initialisation settings
func initExt() {
}
24 changes: 12 additions & 12 deletions templates/main.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Don't edit this file, as it is generated by dredger
// TODO: use wire for dependencies

package main

import (
Expand Down Expand Up @@ -27,10 +29,6 @@ import (
var embeddedFS embed.FS

func main() {
// ToDo: Add basic command line options as _-n_/_-name_ and _-d_/_-debug_ using flags

log.Info().Str("port", core.AppConfig.PortNb).Msg("Starting service")

// init Opentelemetry
if core.AppConfig.Tracing {
tp, err := tracing.InitTracer()
Expand All @@ -44,21 +42,24 @@ func main() {
}()
}

{{- if .AddDatabase}}
db.InitDB()
defer db.CloseDB()
{{- end}}

mainSvc()

log.Info().Str("port", core.AppConfig.PortNb).Msg("Starting web service")

// start echo server
e := echo.New()
e.Use(echoMiddleware.CORS())
e.Use(echoMiddleware.Logger())
// e.Use(otelecho.Middleware(core.AppConfig.Sid))
e.Use(middleware.PolicyHandler) // Policy Enforcement Point to check access

{{- if .AddDatabase}}
db.InitDB()
defer db.CloseDB()
{{- end}}

rest.NewHandler(e)


{{- if .AddFrontend }}

// serve doc
Expand All @@ -74,9 +75,8 @@ func main() {
e.StaticFS("/js/", echo.MustSubFS(web.Js, "js"))
e.StaticFS("/images/", echo.MustSubFS(web.Images, "images"))
e.StaticFS("/", echo.MustSubFS(web.Public, "public"))
{{end}}


{{end}}

if core.AppConfig.CertPem != "" && core.AppConfig.KeyPem != "" {
e.Logger.Fatal(e.StartTLS(":" + core.AppConfig.PortNb, []byte(core.AppConfig.CertPem), []byte(core.AppConfig.KeyPem)))
Expand Down
7 changes: 7 additions & 0 deletions templates/mainSvc.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Edit this file, as it is a specific for your service
package main

func mainSvc() {
// Add your own evaluation of the flags for additional commands
// Add your own service running concurrent with the echo web service
}
4 changes: 3 additions & 1 deletion templates/rest/getContent.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func GetContent(c echo.Context) error {
ctx, span := tracing.Tracer.Start(ctx, "logMessage")
defer span.End()

log.Info().Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Str("path", "/content.html").Msg("GetContent")
traceId := span.SpanContext().TraceID().String()
spanId := span.SpanContext().SpanID().String()
log.Info().Str("traceId", traceId).Str("spanId", spanId).Str("path", "/content.html").Msg("GetContent")

// session, err := getSession(c)
// if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion templates/rest/getIndex.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func GetIndex(c echo.Context) error {
ctx, span := tracing.Tracer.Start(ctx, "logMessage")
defer span.End()

log.Info().Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Str("path", "/index.html").Msg("GetIndex")
traceId := span.SpanContext().TraceID().String()
spanId := span.SpanContext().SpanID().String()
log.Info().Str("traceId", traceId).Str("spanId", spanId).Str("path", "/index.html").Msg("GetIndex")

// session, err := getSession(c)
// if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion templates/rest/getRoot.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func GetRoot(c echo.Context) error {
ctx, span := tracing.Tracer.Start(ctx, "logMessage")
defer span.End()

log.Info().Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Str("path", "/index.html").Msg("GetIndex")
traceId := span.SpanContext().TraceID().String()
spanId := span.SpanContext().SpanID().String()
log.Info().Str("traceId", traceId).Str("spanId", spanId).Str("path", "/index.html").Msg("GetIndex")

// session, err := getSession(c)
// if err != nil {
Expand Down
14 changes: 8 additions & 6 deletions templates/rest/handlerFunc.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func {{ .OperationID }}(c echo.Context) error {
ctx, span := tracing.Tracer.Start(ctx, "logMessage")
defer span.End()

log.Info().Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Str("path", "/").Msg("{{ .OperationID }}")
traceId := span.SpanContext().TraceID().String()
spanId := span.SpanContext().SpanID().String()
log.Info().Str("traceId", traceId).Str("spanId", spanId).Str("path", "/").Msg("{{ .OperationID }}")

// session, err := getSession(c)
// if err != nil {
Expand All @@ -40,7 +42,7 @@ func {{ .OperationID }}(c echo.Context) error {
contentType := c.Request().Header.Get("Content-Type")
body, err := io.ReadAll(c.Request().Body)
if err != nil {
log.Error().Err(err).Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Msg("Can't read body")
log.Error().Err(err).Str("traceId", traceId).Str("spanId", spanId).Msg("Can't read body")
return c.String(http.StatusBadRequest, "Can't read body")
}

Expand All @@ -52,23 +54,23 @@ func {{ .OperationID }}(c echo.Context) error {
if contentType == "application/json" {
err := json.Unmarshal(body, &content)
if err != nil {
log.Error().Err(err).Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Msg("Could not unmarshall JSON input")
log.Error().Err(err).Str("traceId", traceId).Str("spanId", spanId).Msg("Could not unmarshall JSON input")
return c.String(http.StatusBadRequest, "Could not unmarshall JSON input")
}
} else if contentType == "application/yaml" {
err := yaml.Unmarshal(body, &content)
if err != nil {
log.Error().Err(err).Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Msg("Could not unmarshall YAML input")
log.Error().Err(err).Str("traceId", traceId).Str("spanId", spanId).Msg("Could not unmarshall YAML input")
return c.String(http.StatusBadRequest, "Could not unmarshall YAML input")
}
} else if contentType == "application/xml" {
err := xml.Unmarshal(body, &content)
if err != nil {
log.Error().Err(err).Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Msg("Could not unmarshall XML input")
log.Error().Err(err).Str("traceId", traceId).Str("spanId", spanId).Msg("Could not unmarshall XML input")
return c.String(http.StatusBadRequest, "Could not unmarshall XML input")
}
} else {
log.Error().Str("content type", contentType).Str("traceId", span.SpanContext().TraceID().String()).Str("spanId", span.SpanContext().SpanID().String()).Msg("Wrong content type")
log.Error().Str("content type", contentType).Str("traceId", traceId).Str("spanId", spanId).Msg("Wrong content type")
return c.String(http.StatusUnsupportedMediaType, "Wrong content type")
}

Expand Down
Loading

0 comments on commit f15e206

Please sign in to comment.