Skip to content

Commit

Permalink
Merge pull request #73 from mutablelogic/v4
Browse files Browse the repository at this point in the history
Lots of changes
  • Loading branch information
djthorpe committed Jul 27, 2024
2 parents 24e3614 + 7825f01 commit 81875c6
Show file tree
Hide file tree
Showing 80 changed files with 7,849 additions and 258 deletions.
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Paths to tools needed in dependencies
GO := $(shell which go)
DOCKER := $(shell which docker)
NPM := $(shell which npm)

# Build flags
BUILD_MODULE := $(shell cat go.mod | head -1 | cut -d ' ' -f 2)
Expand All @@ -21,15 +22,18 @@ DOCKER_REGISTRY ?= ghcr.io/mutablelogic
BUILD_DIR := "build"
CMD_DIR := $(wildcard cmd/*)
PLUGIN_DIR := $(wildcard plugin/*)
NPM_DIR := $(wildcard npm/*)
BUILD_TAG := ${DOCKER_REGISTRY}/go-server-${OS}-${ARCH}:${VERSION}

# Targets
all: clean cmds plugins
all: clean plugins cmds npm

cmds: $(CMD_DIR)

plugins: $(PLUGIN_DIR)

npm: $(NPM_DIR)

docker: docker-dep
@echo build docker image: ${BUILD_TAG} for ${OS}/${ARCH}
@${DOCKER} build \
Expand Down Expand Up @@ -60,8 +64,16 @@ $(PLUGIN_DIR): go-dep mkdir
@echo Build plugin $(notdir $@)
@${GO} build -buildmode=plugin ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@).plugin ./$@

$(NPM_DIR): npm-dep mkdir
@echo Build npm $(notdir $@)
@cd $@ && npm install && npm run build
@${GO} build -buildmode=plugin ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@).npm.plugin ./$@

FORCE:

npm-dep:
@test -f "${NPM}" && test -x "${NPM}" || (echo "Missing nom binary" && exit 1)

go-dep:
@test -f "${GO}" && test -x "${GO}" || (echo "Missing go binary" && exit 1)

Expand Down
2 changes: 1 addition & 1 deletion cmd/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
static, err := static.Config{FS: filesys, Dir: true}.New()
static, err := static.Config{FS: filesys, DirListing: true}.New()
if err != nil {
log.Fatal(err)
}
Expand Down
22 changes: 17 additions & 5 deletions cmd/nginx-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// Packages
server "github.com/mutablelogic/go-server"
routerFrontend "github.com/mutablelogic/go-server/npm/router"
ctx "github.com/mutablelogic/go-server/pkg/context"
auth "github.com/mutablelogic/go-server/pkg/handler/auth"
certmanager "github.com/mutablelogic/go-server/pkg/handler/certmanager"
Expand Down Expand Up @@ -119,9 +120,13 @@ func main() {
tasks = append(tasks, certmanager)
}

// Location of the FCGI unix socket - this should be the same
// as that listed in the nginx configuration
socket := filepath.Join(n.(nginx.Nginx).DataPath(), "nginx/go-server.sock")
// Router frontend
routerFrontend, err := routerFrontend.Config{}.New()
if err != nil {
log.Fatal("routerFrontend: ", err)
} else {
tasks = append(tasks, routerFrontend)
}

// Router
// TODO: Promote middleware to the root of the configuration to reduce
Expand Down Expand Up @@ -157,10 +162,13 @@ func main() {
tasks = append(tasks, r)
}

// Add router
// Add router and frontend
// The API is served from http[s]://[any]/api/router and the frontend is served from http[s]://static/router
// see the nginx default configuration to understand how the routing occurs in the proxy
r.(router.Router).AddServiceEndpoints("router", r.(server.ServiceEndpoints), logger.(server.Middleware), auth.(server.Middleware))
r.(router.Router).AddServiceEndpoints("static/router", routerFrontend.(server.ServiceEndpoints), logger.(server.Middleware))

// LDAP
// LDAP gets enabled if a password is set
if *ldap_password != "" {
ldap, err := ldap.Config{
URL: "ldap://admin@cm1.local/",
Expand All @@ -175,6 +183,10 @@ func main() {
}
}

// Location of the FCGI unix socket - this should be the same
// as that listed in the nginx configuration
socket := filepath.Join(n.(nginx.Nginx).DataPath(), "nginx/go-server.sock")

// HTTP Server
httpserver, err := httpserver.Config{
Listen: socket,
Expand Down
72 changes: 30 additions & 42 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

// Packages

"github.com/mutablelogic/go-server/pkg/provider"
)

Expand Down Expand Up @@ -46,7 +47,8 @@ func main() {

// Create configurations
var result error
for _, plugin := range []string{"logger", "httpserver", "router", "nginx-handler", "auth-handler", "tokenjar-handler"} {
for _, plugin := range []string{"logger", "httpserver", "router", "router-frontend", "nginx-handler", "auth-handler", "tokenjar-handler"} {
// Create a new configuration for the plugin
if _, err := provider.New(plugin); err != nil {
result = errors.Join(result, err)
}
Expand All @@ -56,48 +58,34 @@ func main() {
os.Exit(1)
}

// TODO: Set parameters from a JSON file
// Create configuration manually
// SET does not actually set it:
// - checks the field exists
// - updates the dependencies between fields
// - marks that the value should be set later
// - there is a binding stage later which actually sets the value
// - this also ensures we set things in the right order
// GET will:
// - check the field exists
// - return a reference to the value if not applied yet
provider.Set("logger.flags", []string{"default", "prefix"})

provider.Set("nginx.binary", "/usr/local/bin/nginx")
provider.Set("nginx.data", "/var/run/nginx")
provider.Set("nginx.group", "www-data")

provider.Set("httpserver.listen", "run/go-server.sock")
provider.Set("httpserver.group", "www-data")
provider.Set("httpserver.router", provider.Get("router"))

provider.Set("auth.tokenjar", provider.Get("tokenjar"))
provider.Set("auth.tokenbytes", 16)
provider.Set("auth.bearer", true)

provider.Set("tokenjar.data", "run")
provider.Set("tokenjar.writeinterval", "30s")
}

/*
{
"logger": {
"flags": ["default", "prefix"]
},
"nginx": {
"binary": "/usr/local/bin/nginx",
"data": "/var/run/nginx",
"group": "www-data",
},
httpserver": {
"listen": "run/go-server.sock",
"group": "www-data",
"router": "${ router }",
},
"router": {
"services": {
"nginx": {
"service": "${ nginx }",
"middleware": ["logger", "auth"]
},
"auth": {
"service": "${ auth }",
"middleware": ["logger", "auth"]
},
"router": {
"service": "${ router }",
"middleware": ["logger", "auth"]
},
},
"auth": {
"tokenjar": "${ tokenjar }",
"tokenbytes": 16,
"bearer": true,
},
"tokenjar": {
"data": "run",
"writeinterval": "30s",
},
}
*/
*/
2 changes: 1 addition & 1 deletion etc/json/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# JSON configuration files
# JSON configuration & parser test files

Examples of JSON configuration files for the run command. This is not yet
implemented, but will be in the future.
35 changes: 20 additions & 15 deletions etc/json/nginx-proxy.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
{
"logger": {
"var": {
"nginx-binary": "/usr/local/bin/nginx",
"nginx-data": "/var/run/nginx",
"nginx-group": "nginx"
},
"logger.main": {
"flags": [
"default",
"prefix"
]
},
"nginx": {
"binary": "/usr/local/bin/nginx",
"data": "/var/run/nginx",
"group": "nginx"
"nginx-handler.main": {
"binary": "${ var.nginx-binary }",
"data": "${ var.nginx-data }",
"group": "${ var.nginx-group }"
},
"tokenjar": {
"tokenjar-handler.main": {
"data": "run",
"writeinterval": "30s"
},
"auth": {
"tokenjar": "${ tokenjar }",
"auth-handler.main": {
"tokenjar": "${ tokenjar-handler.main }",
"tokenbytes": 16,
"bearer": true
},
"router": {
},
"router.main": {
"services": {
"nginx": {
"service": "${ nginx }",
"service": "${ nginx-handler.main }",
"middleware": [
"logger",
"auth"
]
},
"auth": {
"service": "${ auth }",
"service": "${ auth-handler.main }",
"middleware": [
"logger",
"auth"
]
},
"router": {
"service": "${ router }",
"service": "${ router.main }",
"middleware": [
"logger",
"auth"
Expand All @@ -47,6 +52,6 @@
"httpserver": {
"listen": "run/go-server.sock",
"group": "nginx",
"router": "${ router }"
"router": "${ router.main }"
}
}
}
71 changes: 71 additions & 0 deletions etc/json/parser-test-002.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"arrays": {
"bool_array": [
true,
false
],
"string_array": [
"default",
"prefix"
],
"number_array": [
1,
2
],
"null_array": [
null,
null
]
},
"maps": {
"bool_map": {
"true": true,
"false": false
},
"string_map": {
"default": "default",
"prefix": "prefix"
},
"number_map": {
"one": 1,
"two": 2
},
"null_map": {
"null1": null,
"null2": null
}
},
"fields": {
"bool": true,
"string": "string",
"number": 1,
"null": null
},
"map_of_array": {
"bool_array": [
true,
false
],
"string_array": [
"default",
"prefix"
],
"number_array": [
1,
2
],
"null_array": [
null,
null
]
},
"empty_map": {},
"empty_map_array": {
"array1": [],
"array2": []
},
"empty_map_map": {
"map1": {},
"map2": {}
}
}
8 changes: 8 additions & 0 deletions etc/json/parser-test-003.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugin": {
"string": "string",
"number": 1,
"null": null,
"bool": true
}
}
Loading

0 comments on commit 81875c6

Please sign in to comment.