Skip to content

Commit

Permalink
nlayared web application support added
Browse files Browse the repository at this point in the history
  • Loading branch information
enesarf committed Sep 27, 2022
1 parent 356af16 commit b2c7721
Show file tree
Hide file tree
Showing 72 changed files with 1,212 additions and 22 deletions.
42 changes: 42 additions & 0 deletions .cd/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
variables:
DOCKER_HOST: tcp://docker:2375/

stages:
- build
- test
- package
- prepare-test-artifacts
- deploy-to-test-env
- pact-provider-verify
- prepare-prod-artifacts
- deploy-to-prod-env

build:
stage: build
image: golang:1.17.0-alpine3.14
before_script:
- go mod tidy -go=1.16 && go mod tidy -go=1.17
script:
- go build -o bin/main main.go

test:
stage: test
image: golang:1.17
variables:
CI : $CI
before_script:
- go mod tidy -go=1.16 && go mod tidy -go=1.17
script:
- go test

package:
stage: package
image: docker:18-git
services:
- name: docker:18-dind
variables:
DOCKER_REGISTRY_NAME: $MY_CI_REGISTRY_NAME:$CI_PIPELINE_ID
script:
- docker login -u $MY_CI_DOCKER_USER --password $MY_CI_DOCKER_PASS
- docker build -t $DOCKER_REGISTRY_NAME $CI_PROJECT_DIR
- docker push $DOCKER_REGISTRY_NAME
3 changes: 3 additions & 0 deletions .cd/deployment-artifacts/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
apiVersion: apps/v1
kind: Deployment
3 changes: 3 additions & 0 deletions .cd/deployment-artifacts/pv-claim.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
apiVersion: v1
kind: PersistentVolumeClaim
3 changes: 3 additions & 0 deletions .cd/deployment-artifacts/pv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
3 changes: 3 additions & 0 deletions .cd/deployment-artifacts/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
apiVersion: v1
kind: Service
3 changes: 3 additions & 0 deletions .dev/dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
technology:
config_name: value
3 changes: 3 additions & 0 deletions .dev/local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
technology:
config_name: value
3 changes: 3 additions & 0 deletions .dev/prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
technology:
config_name: value
3 changes: 3 additions & 0 deletions .dev/qa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
technology:
config_name: value
5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {

}
8 changes: 4 additions & 4 deletions goarch/cobra/nlw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cobra

import (
nlayered_backend "github.com/eneskzlcn/goarch/goarch/nlayered-backend"
nlayered_web "github.com/eneskzlcn/goarch/goarch/nlayered-web"
"github.com/spf13/cobra"
)

Expand All @@ -21,12 +21,12 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
RunE: func(cmd *cobra.Command, args []string) error {
nlb := nlayered_backend.New(".")
return nlb.Create()
nlw := nlayered_web.New(".")
return nlw.Create()
},
}

func init() {
rootCmd.AddCommand(nlbCmd)
rootCmd.AddCommand(nlwCmd)

}
8 changes: 0 additions & 8 deletions goarch/cobra/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,5 @@ func Execute() {
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.goarch.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
7 changes: 5 additions & 2 deletions goarch/nlayered-web/nlayered_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ type NLayeredWeb struct {

func New(basePath string) *NLayeredWeb {
arch := arch.New(basePath)
nlw := NLayeredWeb{arch: arch}
nlw := &NLayeredWeb{arch: arch}
nlw.initializeArchitecture()
return &nlw
return nlw
}
func (nlw *NLayeredWeb) Create() error {
return nlw.arch.Create()
}
func (nlw *NLayeredWeb) initializeArchitecture() {
nlw.addInitializedCommonRootDirectories()
Expand Down
44 changes: 37 additions & 7 deletions goarch/nlayered-web/template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nlayered_web

import (
_ "embed"
"github.com/eneskzlcn/goarch/goarch/common"
nlayered_common "github.com/eneskzlcn/goarch/goarch/common/nlayered-common"
"github.com/eneskzlcn/goarch/goarch/directory"
Expand All @@ -21,6 +22,7 @@ var coreDirectory = directory.Directory{
"mail": common.MailDirectory,
},
}

var internalDirectory = directory.Directory{
SubDirs: directory.Directories{
"client": common.ClientDirectory,
Expand All @@ -33,27 +35,55 @@ var internalDirectory = directory.Directory{
},
}

//go:embed templates/web/template/include/README.md
var webTemplateIncludeMarkdownFileContent string

//go:embed templates/web/template/include/header.gohtml
var webTemplateIncludeHeaderFileContent string

//go:embed templates/web/template/include/layout.gohtml
var webTemplateIncludeLayoutFileContent string

//go:embed templates/web/template/home.gohtml
var webTemplateHomeFileContent string

var templateDirectory = directory.Directory{
SubDirs: directory.Directories{
"include": directory.Directory{
Files: file.Files{
"layout": file.NewGoHtmlFile(""),
"header": file.NewGoHtmlFile(""),
"layout": file.NewGoHtmlFile(webTemplateIncludeLayoutFileContent),
"header": file.NewGoHtmlFile(webTemplateIncludeHeaderFileContent),
"README": file.NewMarkdownFile(webTemplateIncludeMarkdownFileContent),
},
},
},
Files: file.Files{
"home": file.NewGoHtmlFile(""),
"home": file.NewGoHtmlFile(webTemplateHomeFileContent),
},
}

//MARK: web directory initialization.

//go:embed templates/web/template_go.arch
var webTemplateGoFileContent string

//go:embed templates/web/handler_go.arch
var webHandlerGoFileContent string

//go:embed templates/web/home_go.arch
var webHomeGoFileContent string

//go:embed templates/web/web_go.arch
var webGoFileContent string

var webDirectory = directory.Directory{
SubDirs: directory.Directories{
"template": templateDirectory,
},
Files: file.Files{
//TODO: content will be taken by embed,
"template": file.NewGoFile(""),
"handler": file.NewGoFile(""),
"home": file.NewGoFile(""),
"template": file.NewGoFile(webTemplateGoFileContent),
"handler": file.NewGoFile(webHandlerGoFileContent),
"home": file.NewGoFile(webHomeGoFileContent),
"web": file.NewGoFile(webGoFileContent),
},
}
1 change: 0 additions & 1 deletion goarch/nlayered-web/templates/web/handler.go

This file was deleted.

77 changes: 77 additions & 0 deletions goarch/nlayered-web/templates/web/handler_go.arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package web

import (
logger "<path-to-core>/logger"
middleware "<path-to-core>/middleware"
router "<path-to-core>/router"
"context"
"encoding/gob"
"errors"
"html/template"
"net/http"
"net/url"
)

type Service interface {
GetUsers(ctx context.Context, limit int)
}
type Renderer interface {
Render(w http.ResponseWriter, tmpl *template.Template, data any, statusCode int)
}

type Handler struct {
handler http.Handler
logger logger.Logger
renderer Renderer
service Service
templates PageTemplates
urlParamExtractor func(ctx context.Context, key string) string
}

func NewHandler(logger logger.Logger, service Service, renderer Renderer) *Handler {
if logger == nil {
return nil
}
if session == nil || service == nil || renderer == nil {
logger.Error("invalid arguments"))
return nil
}
handler := Handler{logger: logger, service: service, renderer: renderer}

handler.init()

return &handler
}
func (h *Handler) init() {
muxRouter := router.NewMuxRouterAdapter()
h.urlParamExtractor = muxRouter.ExtractURLParam
h.handler = muxRouter
h.RegisterHandlers(muxRouter)

h.applyMiddlewares()

gob.Register(url.Values{})

templates := make(PageTemplates, 0)
templates["home"] = ParseTemplate("home.gohtml")
h.templates = templates
}
func (h *Handler) applyMiddlewares() {
//apply method overriding middleware
h.handler = middleware.OverrideFormMethods(h.handler)
}

func (h *Handler) RenderPage(page string, w http.ResponseWriter, data any, statusCode int) {
h.renderer.Render(w, h.templates[page], data, statusCode)
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.handler.ServeHTTP(w, req)
}

func (h *Handler) RegisterHandlers(muxRouter router.Router) {
muxRouter.Handle("/", router.MethodHandlers{
http.MethodGet: h.ShowHome,
})

}
17 changes: 17 additions & 0 deletions goarch/nlayered-web/templates/web/home_go.arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package web

import (
"net/http"
)

type homeData struct {
Header string
}

func (h *Handler) ShowHome(w http.ResponseWriter, r *http.Request) {
h.RenderHome(w, homeData{Header: "Welcome"}, http.StatusFound)
}

func (h *Handler) RenderHome(w http.ResponseWriter, data homeData, status int) {
h.RenderPage("home", w, data, status)
}
3 changes: 3 additions & 0 deletions goarch/nlayered-web/templates/web/template/home.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<h1>{{.Header}}</h1>
</div>
9 changes: 9 additions & 0 deletions goarch/nlayered-web/templates/web/template/include/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This directory contains common ui elements
like reusable components and layout elements which
are common for every page.

Suppose that you want to render home.gohtml page
then actually the html content of home.gohtml
taken by template engine and get put into layout.gohtml
file which expects a template "content". Every page
you want to parse and render, the same thing continually happens.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<nav>

</nav>
14 changes: 14 additions & 0 deletions goarch/nlayered-web/templates/web/template/include/layout.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Softdare</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
{{template "header.gohtml" .}}
{{template "content" .}}
</body>

</html>
20 changes: 20 additions & 0 deletions goarch/nlayered-web/templates/web/template_go.arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package web

import (
coreTemplate "<path-to-core>/html/template"
"embed"
"html/template"
)

//go:embed template/include/*.gohtml template/*.gohtml

var templateFS embed.FS

var templateFuncs = template.FuncMap{
"linkify": coreTemplate.Linkify,
}

func ParseTemplate(name string) *template.Template {
return coreTemplate.Parse(name, templateFuncs, templateFS,
"template/include/*.gohtml", "template/")
}
5 changes: 5 additions & 0 deletions goarch/nlayered-web/templates/web/web_go.arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package web

import "html/template"

type PageTemplates map[string]*template.Template
1 change: 1 addition & 0 deletions internal/client/httpclient/httpclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package httpclient
1 change: 1 addition & 0 deletions internal/client/httpclient/httpclient_test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package httpclient_test
1 change: 1 addition & 0 deletions internal/client/rabbitclient/rabbitclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rabbitclient
1 change: 1 addition & 0 deletions internal/client/rabbitclient/rabbitclient_test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rabbitclient_test
Loading

0 comments on commit b2c7721

Please sign in to comment.