Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated nginx module fix #38 #39

Merged
merged 20 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .github/workflows/make-image-v2.yaml
100644 → 100755
Empty file.
21 changes: 13 additions & 8 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

const (
flagAddress = "address"
flagAddress = "addr"
flagPlugins = "plugins"
flagVersion = "version"
)
Expand All @@ -37,12 +37,6 @@ func main() {
}
}

// Check for version flag
if flagset.Lookup(flagVersion).Value.(flag.Getter).Get().(bool) {
version.PrintVersion(flagset.Output())
os.Exit(0)
}

// Get builtin plugin prototypes
protos, err := BuiltinPlugins()
if err != nil {
Expand All @@ -58,6 +52,15 @@ func main() {
}
}

// Check for version flag
if flagset.Lookup(flagVersion).Value.(flag.Getter).Get().(bool) {
version.PrintVersion(flagset.Output())
for key := range protos {
fmt.Fprintf(flagset.Output(), " Plugin: %q\n", key)
}
os.Exit(0)
}

// Read resources using JSON
var result error
var resources []config.Resource
Expand Down Expand Up @@ -121,7 +124,9 @@ func main() {
}()

// Print out information
fmt.Fprintf(os.Stderr, "%s (%s)\n\n", version.GitSource, version.GitTag)
if version.GitSource != "" && version.GitTag != "" {
fmt.Fprintf(os.Stderr, "%s (%s)\n\n", version.GitSource, version.GitTag)
}
for _, key := range provider.Keys() {
provider.Printf(ctx, "task: %s: %s\n", key, provider.Get(key))
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/server/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
// Package imports
dnsregister "github.com/mutablelogic/go-server/pkg/dnsregister"

log "github.com/mutablelogic/go-server/pkg/log"
task "github.com/mutablelogic/go-server/pkg/task"

Expand All @@ -13,7 +13,6 @@ import (
// BuiltInPlugins is a list of plugins which are compiled into the binary
var BuiltInPlugins = []Plugin{
log.Plugin{},
dnsregister.Plugin{},
}

// BuiltinPlugins returns the list of plugins which are compiled into the binary
Expand Down
4 changes: 2 additions & 2 deletions etc/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ FROM --platform=${PLATFORM}/${ARCH} library/nginx:${VERSION}
COPY --from=builder /usr/src/app/build/* /usr/local/bin/
COPY etc/docker/server-entrypoint.sh .
COPY etc/json/ /var/lib/go-server/json/
COPY etc/nginx/ /var/lib/nginx/

# Entrypoint when running the server
ENTRYPOINT [ "/server-entrypoint.sh" ]
CMD [ "/usr/local/bin/server", "-plugins=/usr/local/bin/*.plugin", "/var/lib/go-server/*.json" ]
#CMD ["/usr/local/bin/server","/var/lib/go-server/json/*.json"]
CMD [ "/usr/local/bin/server", "-plugins=/usr/local/bin/*.plugin", "/var/lib/go-server/json/*.json" ]
7 changes: 7 additions & 0 deletions etc/json/nginx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "nginx",
"label": "main",
"path": "/usr/sbin/nginx",
"prefix": "/var/lib/nginx/html",
"config": "/var/lib/nginx/nginx.conf"
}
10 changes: 10 additions & 0 deletions etc/nginx/enabled/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
root /var/lib/nginx/html;
index index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
}
8 changes: 8 additions & 0 deletions etc/nginx/html/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>nginx: not found</title>
</head>
<body>
<h1>Page not found</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions etc/nginx/html/50x.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>nginx: server error</title>
</head>
<body>
<h1>A server error has occurred</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions etc/nginx/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>nginx</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
89 changes: 89 additions & 0 deletions etc/nginx/mime.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;

text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;

image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg svgz;
image/webp webp;

application/font-woff woff;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;

application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;

application/vnd.openxmlformats-officedocument.wordprocessingml.document docx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx;

audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;

video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
18 changes: 18 additions & 0 deletions etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
user nginx;
worker_processes auto;
error_log /alloc/logs/nginx-error.log notice;
pid /run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /var/lib/nginx/mime.types;
default_type application/octet-stream;
access_log /alloc/logs/nginx-access.log combined;
sendfile on;
keepalive_timeout 65;
gzip on;
include /var/lib/nginx/enabled/*.conf;
}
6 changes: 6 additions & 0 deletions etc/test/json/nginx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "nginx",
"label": "main",
"path": "nginx",
"config": "etc/test/nginx/nginx.conf"
}
23 changes: 23 additions & 0 deletions etc/test/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
worker_processes auto;
error_log stderr notice;

events {
worker_connections 1024;
}

http {
default_type application/octet-stream;
access_log none;
sendfile on;
keepalive_timeout 65;
gzip on;

server {
listen 8080 default_server;
listen [::]:8080 default_server;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ go 1.18
require (
github.com/djthorpe/go-errors v1.0.2
github.com/hashicorp/go-multierror v1.1.1
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20221026004748-78e5e7837ae6
)

require github.com/hashicorp/errwrap v1.0.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/djthorpe/go-errors v1.0.2 h1:kZuNLhb6Yo1iNHaenGa9s5CpRbOG6KxbUtrME4LrAkk=
github.com/djthorpe/go-errors v1.0.2/go.mod h1:HtfrZnMd6HsX75Mtbv9Qcnn0BqOrrFArvCaj3RMnZhY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20221026004748-78e5e7837ae6 h1:mC6uOkPi9SUk8A59jZvw7//rlyc+MlELtQUCyOUSKZQ=
golang.org/x/exp v0.0.0-20221026004748-78e5e7837ae6/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12 changes: 8 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func LoadForPattern(filesys fs.FS, pattern string) ([]Resource, error) {
r := Resource{Path: path}
if data, err := fs.ReadFile(filesys, path); err != nil {
return err
} else if err := unmarshal(data, &r); err != nil {
} else if err := unmarshal(path, data, &r); err != nil {
return fmt.Errorf("%s: %w", filepath.Base(path), err)
} else if name := strings.TrimSpace(r.Name()); name == "" {
return ErrBadParameter.Withf("%q: Resource has no name", d.Name())
Expand Down Expand Up @@ -108,7 +108,7 @@ func LoadForResources(filesys fs.FS, resources []Resource, protos task.Plugins)
result = multierror.Append(result, ErrInternalAppError.Withf("LoadForResources: %q", name))
} else if data, err := fs.ReadFile(filesys, path); err != nil {
result = multierror.Append(result, err)
} else if err := unmarshal(data, &plugin); err != nil {
} else if err := unmarshal(path, data, &plugin); err != nil {
result = multierror.Append(result, fmt.Errorf("%s: %w", filepath.Base(path), err))
} else if label := strings.TrimSpace(plugin.Label()); label == "" {
result = multierror.Append(result, ErrBadParameter.Withf("%v: Resource has no label", filepath.Base(path)))
Expand All @@ -133,8 +133,12 @@ func LoadForResources(filesys fs.FS, resources []Resource, protos task.Plugins)

// unmarshal decodes data into a data structure. TODO: need to support
// formats other than JSON
func unmarshal(data []byte, r any) error {
return json.Unmarshal(data, r)
func unmarshal(path string, data []byte, r any) error {
if err := json.Unmarshal(data, r); err != nil {
return err
}
fmt.Println("TODO: Resolve paths", path)
return nil
}

// newPluginInstance returns a new plugin instance given a prototype
Expand Down
15 changes: 15 additions & 0 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
contextParams
contextAdmin
contextAddress
contextPath
)

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -61,6 +62,11 @@ func WithAddress(ctx context.Context, addr string) context.Context {
return context.WithValue(ctx, contextAddress, addr)
}

// Return a context with the given path string
func WithPath(ctx context.Context, path string) context.Context {
return context.WithValue(ctx, contextPath, path)
}

///////////////////////////////////////////////////////////////////////////////
// RETURN VALUES FROM CONTEXT

Expand Down Expand Up @@ -94,6 +100,12 @@ func Address(ctx context.Context) string {
return contextString(ctx, contextAddress)
}

// Return the path parameter from the context, or zero value if
// not defined
func Path(ctx context.Context) string {
return contextString(ctx, contextPath)
}

// Return the parameters from a HTTP request, or nil if
// not defined
func ReqParams(req *http.Request) []string {
Expand Down Expand Up @@ -146,6 +158,9 @@ func DumpContext(ctx context.Context, w io.Writer) {
if value, ok := ctx.Value(contextAddress).(string); ok {
fmt.Fprintf(w, " address=%q", value)
}
if value, ok := ctx.Value(contextPath).(string); ok {
fmt.Fprintf(w, " path=%q", value)
}
fmt.Fprintf(w, ">")
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/expr/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
The `expr` package implements a generic expression parser and evaluator,
and interpolation for strings.
*/
package expr
Loading