-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 95bb06c
Showing
13 changed files
with
359 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
version: 2.0 | ||
|
||
jobs: | ||
# Base test configuration for Go library tests Each distinct version should | ||
# inherit this base, and override (at least) the container image used. | ||
"test": &test | ||
docker: | ||
- image: circleci/golang:latest | ||
working_directory: /go/src/github.com/hasangenc0/microfrontends | ||
steps: &steps | ||
# Our build steps: we checkout the repo, fetch our deps, lint, and finally | ||
# run "go test" on the package. | ||
- checkout | ||
# Logs the version in our build logs, for posterity | ||
- run: go version | ||
- run: | ||
name: "Fetch dependencies" | ||
command: > | ||
go get -t -v ./... | ||
- run: go test -v -race ./... | ||
|
||
"1.12": | ||
<<: *test | ||
docker: | ||
- image: circleci/golang:1.12 | ||
|
||
workflows: | ||
version: 2 | ||
build: | ||
jobs: | ||
- "1.12" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### Go ### | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Binary Output folder | ||
.bin/* | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
### Go Patch ### | ||
/vendor/ | ||
/Godeps/ | ||
|
||
### VisualStudioCode ### | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
### VisualStudioCode Patch ### | ||
# Ignore all local history of files | ||
.history | ||
|
||
# Git keep | ||
!.keep | ||
|
||
# GoLand Ide | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:1.12.0-alpine3.9 | ||
|
||
RUN mkdir /microfrontends | ||
|
||
ADD . /microfrontends | ||
|
||
WORKDIR /microfrontends | ||
|
||
RUN go build -o .bin/main | ||
|
||
CMD ["/microfrontends/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gorilla/mux" | ||
"github.com/hasangenc0/microfrontends" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
gateways := []microfrontends.Gateway{ | ||
{ | ||
Name: "header", | ||
Content: "<div><h1>Header</h1></div>", | ||
}, | ||
{ | ||
Name: "content", | ||
Content: "<div><h1>Content</h1></div>", | ||
}, | ||
{ | ||
Name: "footer", | ||
Content: "<div><h1>Footer</h1></div>", | ||
}, | ||
} | ||
|
||
page := microfrontends.Page{ | ||
Name: "App", | ||
Content: ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Microfrontends Example</title> | ||
</head> | ||
<body> | ||
<chunk name="header"></chunk> | ||
<chunk name="content"></chunk> | ||
<chunk name="footer"></chunk> | ||
</body> | ||
</html> | ||
`, | ||
} | ||
|
||
app := microfrontends.App{ | ||
gateways, | ||
page, | ||
} | ||
|
||
microfrontends.Make(w, app); | ||
} | ||
|
||
func main() { | ||
port := ":4446" | ||
|
||
r := mux.NewRouter() | ||
r.HandleFunc("/", handler) | ||
|
||
fmt.Println("Listening...") | ||
|
||
err := http.ListenAndServe(port, r) | ||
|
||
if err != nil { | ||
log.Fatal("Listen and serve: ", err) | ||
return | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package microfrontends | ||
|
||
import ( | ||
"github.com/hasangenc0/microfrontends/pkg/client" | ||
"github.com/hasangenc0/microfrontends/pkg/collector" | ||
"html/template" | ||
"net/http" | ||
"runtime" | ||
"sync" | ||
) | ||
|
||
type Gateway = collector.Gateway | ||
type Page = collector.Page | ||
type App = collector.App | ||
|
||
func setHeaders(w http.ResponseWriter) { | ||
w.Header().Set("Transfer-Encoding", "chunked") | ||
//w.Header().Set("X-Content-Type-Options", "nosniff") | ||
} | ||
|
||
func initialize(w http.ResponseWriter, page Page) { | ||
flusher, ok := w.(http.Flusher) | ||
|
||
if !ok { | ||
panic("expected http.ResponseWriter to be an http.Flusher") | ||
} | ||
|
||
tmpl, err := template.New(page.Name).Parse(page.Content) | ||
|
||
if err != nil { | ||
panic("An Error occured when parsing html") | ||
return | ||
} | ||
|
||
err = tmpl.Execute(w, "") | ||
|
||
if err != nil { | ||
panic("Error in Template.Execute") | ||
} | ||
|
||
flusher.Flush() | ||
} | ||
|
||
func sendChunk(w http.ResponseWriter, gateway Gateway, wg *sync.WaitGroup, ch chan http.Flusher) { | ||
var flusher, ok = w.(http.Flusher) | ||
if !ok { | ||
panic("expected http.ResponseWriter to be an http.Flusher") | ||
} | ||
|
||
chunk := client.GetView(gateway.Name, gateway.Content) | ||
|
||
tmpl, err := template.New(gateway.Name).Parse(chunk) | ||
|
||
if err != nil { | ||
panic("An Error occured when parsing html") | ||
} | ||
|
||
err = tmpl.Execute(w, "") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
//flusher.Flush() | ||
ch <- flusher | ||
wg.Done() | ||
} | ||
|
||
func finish(w http.ResponseWriter) { | ||
flusher, ok := w.(http.Flusher) | ||
|
||
if !ok { | ||
panic("expected http.ResponseWriter to be an http.Flusher") | ||
} | ||
|
||
_, err := w.Write([]byte("")) | ||
|
||
if err != nil { | ||
panic("expected http.ResponseWriter to be an http.Flusher") | ||
|
||
} | ||
|
||
flusher.Flush() | ||
|
||
} | ||
|
||
func Make(w http.ResponseWriter, app App) { | ||
setHeaders(w) | ||
|
||
var wg sync.WaitGroup | ||
|
||
initialize(w, app.Page) | ||
|
||
runtime.GOMAXPROCS(4) | ||
|
||
var flusher = make(chan http.Flusher) | ||
|
||
for _, gateway := range app.Gateway { | ||
wg.Add(1) | ||
go sendChunk(w, gateway, &wg, flusher) | ||
} | ||
|
||
for range app.Gateway { | ||
flusher, ok := <-flusher | ||
if !ok { | ||
panic("expected http.ResponseWriter to be an http.Flusher") | ||
} | ||
flusher.Flush() | ||
} | ||
|
||
wg.Wait() | ||
|
||
finish(w) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package microfrontends | ||
|
||
import ( | ||
"testing" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestMicrofronteds(t *testing.T) { | ||
Convey("Make should return ", t, func() { | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package client | ||
|
||
import "fmt" | ||
|
||
func getJs(view string, name string) string { | ||
js := ` | ||
<script> | ||
var chunk = document.querySelector('[name="%s"]'); | ||
chunk.innerHTML = '%s'; | ||
</script> | ||
` | ||
|
||
return fmt.Sprintf(js, view, name) | ||
|
||
} | ||
|
||
func GetView(view string, name string) string { | ||
js := getJs(view, name) | ||
|
||
return js | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package collector | ||
|
||
// Collect collects endpoints | ||
func Collect(services []Service) int { | ||
|
||
return 61 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package collector | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestCollect(t *testing.T) { | ||
// Only pass t into top-level Convey calls | ||
Convey("Collect should return 61", t, func() { | ||
services := []Service{ | ||
{ | ||
host: "asdasd", | ||
port: "4460", | ||
}, | ||
} | ||
|
||
host := Collect(services) | ||
So(host, ShouldEqual, 61) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package collector | ||
|
||
// Service is the model for microservice endpoints | ||
type Service struct { | ||
host string | ||
port string | ||
} | ||
|
||
type Gateway struct { | ||
Name string | ||
Content string | ||
} | ||
|
||
type Page struct { | ||
Content string | ||
Name string | ||
} | ||
|
||
type App struct { | ||
Gateway []Gateway | ||
Page Page | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package server | ||
|
||
func setHeader() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Microfronteds | ||
|
||
This project is under development | ||
|
||
## Usage | ||
Look at the [examples](./examples) folder |