Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hasangenc0 committed Jul 19, 2019
0 parents commit 95bb06c
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 0 deletions.
Empty file added .bin/.keep
Empty file.
31 changes: 31 additions & 0 deletions .circleci/config.yml
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"
40 changes: 40 additions & 0 deletions .gitignore
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/
11 changes: 11 additions & 0 deletions Dockerfile
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"]
68 changes: 68 additions & 0 deletions examples/main.go
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
}

}
114 changes: 114 additions & 0 deletions microfrontends.go
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)
}
12 changes: 12 additions & 0 deletions microfrontends_test.go
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() {

})
}
21 changes: 21 additions & 0 deletions pkg/client/client.go
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
}
7 changes: 7 additions & 0 deletions pkg/collector/collect.go
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
}
22 changes: 22 additions & 0 deletions pkg/collector/collect_test.go
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)
})
}
22 changes: 22 additions & 0 deletions pkg/collector/service.go
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
}
5 changes: 5 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package server

func setHeader() {

}
6 changes: 6 additions & 0 deletions readme.md
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

0 comments on commit 95bb06c

Please sign in to comment.