Skip to content

Commit

Permalink
Merge pull request #26 from fraunhoferfokus/development
Browse files Browse the repository at this point in the history
* replaced symlink creation
  • Loading branch information
JGottschick authored May 13, 2024
2 parents 8761055 + 4db3a66 commit cbc0310
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ download-style:
curl -o templates/web/js/hyperscript.js -L https://unpkg.com/hyperscript.org@latest
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

curl -o templates/web/css/pico.colors.min.css -L https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.colors.min.css
certificate:
go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost`

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ For typical tasks you can use the [just](https://just.systems/man/en/) recipes:

# Examples

You can find a few OpenAPI 3 Specification file examples <a href="./examples">here</a>.
You can find a few OpenAPI 3 Specification file examples [here](./examples). There is also a minimal [OpenAPI.yaml](./examples/OpenAPI.yaml.min-example) file as starting point for your service.

# Contributions

Expand Down
2 changes: 1 addition & 1 deletion core/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.15
0.2.16
87 changes: 87 additions & 0 deletions examples/OpenAPI.yaml.min-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
openapi: 3.1.0
info:
title: My-Service
description: My service just do exist, so it mainly implements the mmost important API endpoints livez and readyz required by Kubernetes
version: 0.0.1
servers:
- url: http://localhost:{port}
description: The main component container of the pod
variables:
port:
default: "8080"

paths:
/:
get:
summary: Returns the index content of my service
operationId: Root
responses:
"200":
description: This entrypoint could be a HTML page or data in different formats.
content:
text/html:
schema:
type: string
application/json:
schema:
type: string
application/yaml:
schema:
type: string
/livez:
get:
summary: deliver the state of the service
tags:
- builtin
responses:
"200":
description: service is alive
content:
text/plain:
schema:
type: string
/readyz:
get:
summary: deliver the state of the service
tags:
- builtin
responses:
"200":
description: service is ready to service requests
content:
text/plain:
schema:
type: string
"503":
description: service is not ready to service requests, e.g. waiting to finish its initialisation or waiting for other services to become ready
content:
text/plain:
schema:
type: string
/infoz:
get:
summary: relevant meta information about the service
tags:
- builtin
responses:
"200":
description: deliver all properties like service name and version
content:
application/json:
schema:
type: string
/robots.txt:
get:
summary: return robots.txt to restrict web crawlers
tags:
- builtin
responses:
"200":
description: deliver robots.txt
content:
application/json:
schema:
type: string

components:
schemas:
15 changes: 9 additions & 6 deletions generator/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package generator

import (
extCmd "dredger/cmd"
"errors"
"os"
"path/filepath"
"runtime"

"github.com/rs/zerolog/log"
)

func generateConfigFiles(serverConf ServerConfig) {
Expand Down Expand Up @@ -37,10 +37,13 @@ func generateConfigFiles(serverConf ServerConfig) {
templateFile = "templates/core/version"
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
createFileFromTemplate(filePath, templateFile, serverConf)
if runtime.GOOS == "windows" {
extCmd.RunCommand("mklink /h "+fileName+" "+filePath, config.Path)
} else {
extCmd.RunCommand("ln -s "+filePath+" "+fileName, config.Path)
if err = os.Symlink(filePath, fileName); err != nil {
log.Error().Err(err).Str("source", filePath).Str("target", fileName).Msg("Could not create symbolic Link")
}
// if runtime.GOOS == "windows" {
// extCmd.RunCommand("mklink /h "+fileName+" "+filePath, config.Path)
// } else {
// extCmd.RunCommand("ln -s "+filePath+" "+fileName, config.Path)
// }
}
}
109 changes: 99 additions & 10 deletions generator/entities.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package generator

import (
"fmt"
"math"
"path/filepath"
"strings"
"unicode"

"github.com/getkin/kin-openapi/openapi3"
"github.com/gobeam/stringy"
)

var IMPORT_UUID bool
Expand Down Expand Up @@ -42,7 +43,7 @@ type ImportsConfig struct {

func GenerateTypes(spec *openapi3.T, pConf ProjectConfig) {
if spec != nil && spec.Components != nil {
schemaDefs := generateStructDefs(&spec.Components.Schemas)
schemaDefs := generateTypeDefs(&spec.Components.Schemas)
imports := generateImports()
var conf ModelCOnfig
conf.Imports = imports
Expand All @@ -58,11 +59,102 @@ func GenerateTypes(spec *openapi3.T, pConf ProjectConfig) {
}
}

func generateStructDefs(schemas *openapi3.Schemas) map[string][]TypeDefinition {
func generateTypeDefs(schemas *openapi3.Schemas) map[string][]TypeDefinition {
schemaDefs := make(map[string][]TypeDefinition, len(*schemas))

for schemaName, ref := range *schemas {
schemaDefs[schemaName] = generateTypeDefs(&ref.Value.Properties)
fmt.Printf("%s: %#v\n\n", schemaName, ref.Value.Type)
var goType string
if ref.Value.Type.Includes("number") {
switch ref.Value.Format {
case "float":
goType = "float32"
case "double":
goType = "float64"
default:
goType = "float"
}
schemaDefs[schemaName] = []TypeDefinition{{
schemaName,
goType,
ref.Value.MinLength,
uintOrMax(ref.Value.MaxLength),
ref.Value.Pattern,
floatOrMin(ref.Value.Min),
floatOrMax(ref.Value.Max),
stringy.New(schemaName).LcFirst(),
[]TypeDefinition{},
}}
} else if ref.Value.Type.Includes("integer") {
goType = "int"
if ref.Value.Format != "" {
goType = ref.Value.Format
}
schemaDefs[schemaName] = []TypeDefinition{{
schemaName,
goType,
ref.Value.MinLength,
uintOrMax(ref.Value.MaxLength),
ref.Value.Pattern,
floatOrMin(ref.Value.Min),
floatOrMax(ref.Value.Max),
stringy.New(schemaName).LcFirst(),
[]TypeDefinition{},
}}
} else if ref.Value.Type.Includes("boolean") {
goType = "bool"
schemaDefs[schemaName] = []TypeDefinition{{
schemaName,
goType,
ref.Value.MinLength,
uintOrMax(ref.Value.MaxLength),
ref.Value.Pattern,
floatOrMin(ref.Value.Min),
floatOrMax(ref.Value.Max),
stringy.New(schemaName).LcFirst(),
[]TypeDefinition{},
}}
} else if ref.Value.Type.Includes("string") {
switch ref.Value.Format {
case "binary":
goType = "[]byte"
case "date":
IMPORT_TIME = true
goType = "time.Time"
case "uuid":
IMPORT_UUID = true
goType = "uuid.UUID"
default:
goType = "string"
}
schemaDefs[schemaName] = []TypeDefinition{{
schemaName,
goType,
ref.Value.MinLength,
uintOrMax(ref.Value.MaxLength),
ref.Value.Pattern,
floatOrMin(ref.Value.Min),
floatOrMax(ref.Value.Max),
stringy.New(schemaName).LcFirst(),
[]TypeDefinition{},
}}
} else if ref.Value.Type.Includes("array") {
items, _ := toGoType(ref.Value.Items)
goType = "[]" + items
schemaDefs[schemaName] = []TypeDefinition{{
schemaName,
goType,
ref.Value.MinLength,
uintOrMax(ref.Value.MaxLength),
ref.Value.Pattern,
floatOrMin(ref.Value.Min),
floatOrMax(ref.Value.Max),
stringy.New(schemaName).LcFirst(),
[]TypeDefinition{},
}}
} else if ref.Value.Type.Includes("object") {
schemaDefs[schemaName] = generatePropertyDefs(&ref.Value.Properties)
}
}
return schemaDefs
}
Expand All @@ -88,19 +180,16 @@ func floatOrMax(x *float64) float64 {
return math.MaxFloat64
}

func generateTypeDefs(properties *openapi3.Schemas) []TypeDefinition {
func generatePropertyDefs(properties *openapi3.Schemas) []TypeDefinition {
typeDefs := make([]TypeDefinition, len(*properties))
i := 0
for name, property := range *properties {
goType, nested := toGoType(property)
var nestedGoTypes []TypeDefinition
if nested {
nestedGoTypes = generateTypeDefs(&property.Value.Properties)
nestedGoTypes = generatePropertyDefs(&property.Value.Properties)
}

// first letter to lower case
marshalName := []rune(name)
marshalName[0] = unicode.ToLower(marshalName[0])
propertyDef := TypeDefinition{
name,
goType,
Expand All @@ -109,7 +198,7 @@ func generateTypeDefs(properties *openapi3.Schemas) []TypeDefinition {
property.Value.Pattern,
floatOrMin(property.Value.Min),
floatOrMax(property.Value.Max),
string(marshalName),
stringy.New(name).LcFirst(),
nestedGoTypes,
}
typeDefs[i], i = propertyDef, i+1
Expand Down
1 change: 1 addition & 0 deletions generator/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func generateFrontend(_ *openapi3.T, conf GeneratorConfig) {
fs.CopyWebFile("web/css", stylesheetPath, "bootstrap-icons.min.css", true)
fs.CopyWebFile("web/css", stylesheetPath, "bootstrap.min.css", true)
fs.CopyWebFile("web/css", stylesheetPath, "pico.min.css", true)
fs.CopyWebFile("web/css", stylesheetPath, "pico.colors.min.css", true)
fs.CopyWebFile("web/css", stylesheetPath, "simple.min.css", true)

// files in web directory
Expand Down
4 changes: 4 additions & 0 deletions templates/web/css/pico.colors.min.css

Large diffs are not rendered by default.

0 comments on commit cbc0310

Please sign in to comment.