As Library
Apart from using Connexions as a standalone application, you can also use it as a library without
running any server instance.
Installation¶
go get github.com/cubahno/connexions
OpenAPI Parser¶
Load and parse OpenAPI specification file.
You can choose which provider to use:
libopenapi
supports OpenAPI 3.1 and can handle circular references.
kin-openapi
has validation functions.
Swagger is supported out of the box.
Document implements Document
interface.
package main
import (
"fmt"
"github.com/cubahno/connexions"
"github.com/cubahno/connexions/config"
)
func main() {
docFactory := connexions.NewDocumentFromFileFactory(config.LibOpenAPIProvider)
doc, _ := docFactory("resources/petstore.yaml")
fmt.Printf("Loaded document version %s, with %d resources\n", doc.GetVersion(), len(doc.GetResources()))
}
Schema Replacements¶
package main
import (
"fmt"
"github.com/cubahno/connexions"
"github.com/cubahno/connexions/config"
"github.com/cubahno/connexions/openapi"
"github.com/cubahno/connexions/replacers"
)
func main() {
cfg := config.NewDefaultConfig("")
schema := &openapi.Schema{
Type: "object",
Properties: map[string]*openapi.Schema{
"id": {
Type: "string",
Format: "uuid",
},
"name": {
Type: "string",
},
"age": {
Type: "integer",
Minimum: 20,
Maximum: 30,
},
},
}
contexts := []map[string]any{
{"person": map[string]any{"name": "Jane", "age": 33}},
{
"id": []string{"111", "222"},
"name": "Jane",
},
}
replacer := replacers.CreateValueReplacer(cfg, replacers.Replacers, contexts)
res := connexions.GenerateContentFromSchema(schema, replacer, nil)
fmt.Printf("%+v\n", res)
// will print either:
// `mmap[age:30 id:111 name:Jane]`
// or
// `mmap[age:30 id:222 name:Jane]`
}
Fake Functions¶
We use fakes from jaswdr/faker library.
Fake functions are gathered in one map with dotted key names.
All map values implement FakeFunc
interface.
package main
import (
"fmt"
"github.com/cubahno/connexions/contexts"
)
func main() {
fakeMap := contexts.GetFakes()
uuid := fakeMap["uuid.v4"]().Get()
tag := fakeMap["gamer.tag"]().Get()
fmt.Printf("uuid: %s, tag: %v\n", uuid, tag)
}
File Operations¶
package main
import "github.com/cubahno/connexions"
func main() {
// will create complete path if it does not exist
_ = connexions.SaveFile("/path/a/b/c/test.txt", []byte("hello world"))
_ = connexions.CopyFile("/path/a/b/c/test.txt", "/path/a/b/c/test2.txt")
_ = connexions.CopyDirectory("/path/a/b/c", "/path/a/b/c2")
}
Running Application Server¶
package main
import (
"github.com/cubahno/connexions/api"
"github.com/cubahno/connexions/config"
"net/http"
)
func main() {
cfg := config.NewDefaultConfig("")
cfg.App.Port = 8888
app := api.NewApp(cfg)
// add as many blueprints as needed.
_ = app.AddBluePrint(func(router *api.Router) error {
h := &ApiHandler{api.NewBaseHandler()}
router.HandleFunc("/api/v1/route-1", h.myRoute1)
router.HandleFunc("/api/v1/route-2", h.myRoute2)
return nil
})
app.Run()
}
type ApiHandler struct {
*api.BaseHandler
}
func (h *ApiHandler) myRoute1(w http.ResponseWriter, r *http.Request) {
h.JSONResponse(w).WithStatusCode(http.StatusOK).Send(&api.SimpleResponse{
Message: "hello world",
Success: true,
})
}
func (h *ApiHandler) myRoute2(w http.ResponseWriter, r *http.Request) {
h.Response(w).WithHeader("content-type", "text/plain").Send([]byte("hello world"))
}