Skip to content

Commit

Permalink
addapted documentation and example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Mancke committed Dec 6, 2016
1 parent 96b56c3 commit 5e1aaff
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
15 changes: 7 additions & 8 deletions composition/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
# lib-ui-service: composition

This library contains code for the composition of HTML pages out of multiple HTML page containing fragments.
This library contains code for the composition of HTML pages out of multiple HTML pages, containing fragments.

### Key Concept
Every Service delivers a functional HTML user interface in form of complete HTML pages in the way, that a service can be developed and tested
Every service delivers a functional HTML user interface in form of complete HTML pages. This way a service is self contained and can be developed and tested
by it's own. A UI-Service can request multiple pages from different services and compose them to one HTML page. To support this, the HTML pages
from the services contain a special HTML vocabular.

### Composition Process
The composition is done in the following steps:

1. The UI-Service has a `CompositionHandler` in it's handler chain, which answers these which need composition.
1. An UI-Service has a `CompositionHandler` in it's handler chain, which answers these which need composition.
2. The `CompositionHandler` has a callback from the UI-Service. This callback gets a `http.Request` object as argument and returns a List of FetchResult.
3. For each request this callback is triggered. So the UI-Service can add a `ContentFetcher` for this request and adds FetchDefinitions for Page using `ContentFetcher.AddFetchJob()`.
4. The ContentFetchter loads the Pages and recursively it's dependencies in parallel. For the actual loading and parsing, it uses the `HtmlContentParser`.
5. When all `Content` objects are loaded, the `CompositionHandler` merges them together, using `ContentMerge`.

### Merging
The it self is very simple:
The merging it self is very simple:

- The MetaJSON is calculated by adding all fields of the loaded MetaJSON to one global map.
- All Head fragments are concatenated within the `<head>`.
- The Default Body Fragment of the first Content object is executed and may recursively include other Fragments Content objects.
- For rendering of the body part, the default fragment of the page with the name `layout` is rendered first. This rendering may recursively include other fragments.
- All Tail fragments are concatenated at the end of the `<body>`.

### Execution Order
**Attention**: The execution order of the Content Objects is determined by the order in which they are returned from the `ContentFetcher`.
Currently this is only deterministic within the FetchDefinitions added by `ContentFetcher.AddFetchJob()`. The recursive dependencies are loaded from them are in a random order.
This may cause not deterministic behaviour, if the contain equal named fragments or provide the same MetaJSON attributes.
This may cause not deterministic behaviour, if they contain equal named fragments or provide the same MetaJSON attributes.

### Caching
At a later point, the ContentFetcher may provide Caching of FetchDefinitions.

Caching is proveded at the level of framents, if a cache from caching package is configured.


## HTML Composition Vocabulary
Expand Down
23 changes: 19 additions & 4 deletions composition_example/example_ui_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"os"
)

var host = "127.0.0.1:8080"

func main() {
panic(http.ListenAndServe(":8080", handler()))
panic(http.ListenAndServe(host, handler()))
}

func handler() http.Handler {
Expand All @@ -26,9 +28,22 @@ func compositionHandler() http.Handler {
}

fetcher := composition.NewContentFetcher(defaultMetaJSON)
fetcher.AddFetchJob(composition.NewFetchDefinition("§[request.base_url]§/static/styles.html"))
fetcher.AddFetchJob(composition.NewFetchDefinition("§[request.base_url]§/static/layout.html"))
fetcher.AddFetchJob(composition.NewFetchDefinition("§[request.base_url]§/static/lorem.html"))
baseUrl := "http://" + r.Host

fetcher.AddFetchJob(&composition.FetchDefinition{
URL: baseUrl + "/static/layout.html",
Name: "layout",
})

fetcher.AddFetchJob(&composition.FetchDefinition{
URL: baseUrl + "/static/lorem.html",
Name: "content",
})

fetcher.AddFetchJob(&composition.FetchDefinition{
URL: baseUrl + "/static/styles.html",
})

return fetcher
}
return composition.NewCompositionHandler(contentFetcherFactory)
Expand Down
14 changes: 6 additions & 8 deletions composition_example/expected_test_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
The Subheader
</div>


<p></p>
<ul>
<li><a href="http://127.0.0.1:8080?text=Something">Set Text: Something</a></li>
<li><a href="http://127.0.0.1:8080?text=Else">Set Text: Else</a></li>
</ul>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

<p></p>
<ul>
<li><a href="http://127.0.0.1:8080?text=Something">Set Text: Something</a></li>
<li><a href="http://127.0.0.1:8080?text=Else">Set Text: Else</a></li>
</ul>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

<hr>
<small>The footer</small>
Expand Down
6 changes: 3 additions & 3 deletions composition_example/static/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<body>
<h1 uic-remove>This is the basic layout</h1>
<uic-include src="§[request.base_url]§/static/basic.html#header"/>
<uic-include src="#subheader"/>
<uic-include src="#content"/>
<uic-include src="#footer"/>
<uic-include src="§[request.base_url]§/static/basic.html#subheader"/>
§[> content
<uic-include src="§[request.base_url]§/static/basic.html#footer"/>
</body>
</html>
14 changes: 6 additions & 8 deletions composition_example/static/lorem.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
<head>
</head>
<body>
<uic-fragment name="content">
<p>§[request.params.text]§</p>
<ul>
<li><a href="§[request.base_url]§?text=Something">Set Text: Something</a></li>
<li><a href="§[request.base_url]§?text=Else">Set Text: Else</a></li>
</ul>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</uic-fragment>
<p>§[request.params.text]§</p>
<ul>
<li><a href="§[request.base_url]§?text=Something">Set Text: Something</a></li>
<li><a href="§[request.base_url]§?text=Else">Set Text: Else</a></li>
</ul>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</body>
</html>
2 changes: 1 addition & 1 deletion composition_example/static/styles.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<link rel="stylesheet" href="/static/stylesheet.css">
</head>
<body>
<h1 uic-remove>This file only defines some styles and generic javascript for the head.</h1>
<h1>This file only defines some styles and generic javascript for the head.</h1>
<uic-tail>
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script>
Expand Down

0 comments on commit 5e1aaff

Please sign in to comment.