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

Feature/auto edge httpfile #780

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
78 changes: 78 additions & 0 deletions engine/resources/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,84 @@ func (obj *HTTPFileRes) Cleanup() error {
return nil
}

// HTTPFileResAutoEdges holds the state of the auto edge generator.
type HTTPFileResAutoEdges struct {
data []engine.ResUID
pointer int
found bool
}

// Next returns the next automatic edge.
func (obj *HTTPFileResAutoEdges) Next() []engine.ResUID {
if obj.found {
return nil
}
if len(obj.data) == 0 {
return nil
}
value := obj.data[obj.pointer]
obj.pointer++
return []engine.ResUID{value}
}

// Test gets results of the earlier Next() call & returns if we should continue.
func (obj *HTTPFileResAutoEdges) Test(input []bool) bool {
if len(obj.data) <= obj.pointer {
return false
}
if obj.found {
return false
}
if len(input) != 1 {
panic("Expecting a single value!")
}
if input[0] {
obj.found = true
return false
}
return true
}

// AutoEdges returns the automatic edges that this resource has based on its data.
func (obj *HTTPFileRes) AutoEdges() (engine.AutoEdge, error) {
var data []engine.ResUID

// No automatic edges when using the Data field directly.
if obj.Data != "" {
return &HTTPFileResAutoEdges{
data: data,
pointer: 0,
found: false,
}, nil
}

// Skip edge creation if no path specified.
if obj.Path == "" {
return &HTTPFileResAutoEdges{
data: data,
pointer: 0,
found: false,
}, nil
}

// The file must exist before we can serve it.
var reversed = true
data = append(data, &FileUID{
BaseUID: engine.BaseUID{
Name: obj.Name(),
Kind: "file",
Reversed: &reversed,
},
path: obj.Path,
})

return &HTTPFileResAutoEdges{
data: data,
pointer: 0,
found: false,
}, nil
}

// Watch is the primary listener for this resource and it outputs events. This
// particular one does absolutely nothing but block until we've received a done
// signal.
Expand Down
46 changes: 46 additions & 0 deletions examples/lang/http-edge0.mcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import "fmt"

# This creates a set of test files with different configurations.
file "/tmp/mgmt-test1.txt" {
content => "Hello from Test 1!",
state => $const.res.file.state.exists,
}

file "/tmp/mgmt-test2.txt" {
content => "Hello from Test 2!",
state => $const.res.file.state.exists,
}

# Create a test directory and its contents.
file "/tmp/mgmt-test-dir/" {
state => $const.res.file.state.exists,
}

file "/tmp/mgmt-test-dir/index.txt" {
content => "Hello from Directory!",
state => $const.res.file.state.exists,
}

# Create an HTTP server to serve these files.
http:server ":8080" {
address => ":8080",
}

# Serve multiple test files from the same server.
http:file "/test1" {
path => "/tmp/mgmt-test1.txt",
}

http:file "/test2" {
path => "/tmp/mgmt-test2.txt",
}

# Configure directory serving.
http:file "/dir/" {
path => "/tmp/mgmt-test-dir/",
}

# Serve a specific file from within the directory.
http:file "/dir/index" {
path => "/tmp/mgmt-test-dir/index.txt",
}
Loading