Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
- Goboiler now outputs templating system supported by golang embed >= Go 1.16
- Multiple Optimizations
  • Loading branch information
shammishailaj committed Jan 24, 2023
1 parent f070077 commit b66eb89
Show file tree
Hide file tree
Showing 21 changed files with 806 additions and 295 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.92
1.0.0
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ func createStructure(app *application) {
fmt.Sprintf("%s%s", app.Path, "README.md"): "README.md.tmpl",
fmt.Sprintf("%s%s", app.Path, "VERSION"): "VERSION.tmpl",
fmt.Sprintf("%s%s", app.Path, ".gitignore"): "gitignore.tmpl",
fmt.Sprintf("%s%s.go", app.Path, app.Name): fmt.Sprintf("%s.go", app.Name),
},
Directories: make([]*dir, 0),
}

sep := string(os.PathSeparator)

s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s%s%s", app.Path, "cmd", sep, app),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "cmd", sep, app, sep, fmt.Sprintf("%s.go", app)): "app.go.tmpl",
},
})
//s.Directories = append(s.Directories, &dir{
// Name: fmt.Sprintf("%s%s%s%s", app.Path, "cmd", sep, app),
// Files: map[string]string{
// fmt.Sprintf("%s%s%s%s%s%s", app.Path, "cmd", sep, app, sep, fmt.Sprintf("%s.go", app)): "app.go.tmpl",
// },
//})

s.Directories = append(s.Directories, &dir{
Name: fmt.Sprintf("%s%s", app.Path, "configs"),
Expand Down Expand Up @@ -138,14 +139,16 @@ func createStructure(app *application) {
Name: fmt.Sprintf("%s%s%s%s", app.Path, "pkg", sep, "utils"),
Files: map[string]string{
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "array.go"): "utils_array.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "aws_v2.go"): "utils_aws_v2.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "constants.go"): "utils_constants.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "couchbase_config.go"): "utils_couchbase_config.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "couchbasedb.go"): "utils_couchbasedb.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "filesystem.go"): "utils_filesystem.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "http_request.go"): "utils_http_request.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "rdbms.go"): "utils_rdbms.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "rdbms_utils.go"): "utils_rdbms_utils.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "reflection.go"): "utils_reflection.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "ses.go"): "utils_ses.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "ses_old.go"): "utils_ses_old.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "system.go"): "utils_system.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "template.go"): "utils_template.go.tmpl",
fmt.Sprintf("%s%s%s%s%s%s", app.Path, "pkg", sep, "utils", sep, "time.go"): "utils_time.go.tmpl",
Expand Down Expand Up @@ -206,7 +209,7 @@ func main() {
version bool
)

const VERSION = "0.0.92"
const VERSION = "1.0.0"

flag.BoolVar(&version, "version", false, "Prints version and exits")
flag.StringVar(&pathToApp, "path", "", "Specify absolute path to app")
Expand Down
12 changes: 11 additions & 1 deletion templates/Makefile.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# https://unix.stackexchange.com/a/470502
ifndef os
override os = linux
endif

# https://unix.stackexchange.com/a/470502
ifndef arch
override arch = amd64
endif

build:
-docker rm {{.Name}}
-docker rmi -f {{.Name}}:latest
Expand All @@ -10,7 +20,7 @@ run:
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 CGO_ENABLED=0 GOOS=$(os) GOARCH=$(arch) docker compose -f ./deployments/docker-compose.yml up --remove-orphans {{.Name}}-dev

cli:
sh ./deployments/build_cli.sh GOOS=$(os) GOARCH=$(arch)
sh ./deployments/build_cli.sh $(os) $(arch)

test:
docker compose -f ./deployments/docker-compose.yml up tests
Expand Down
19 changes: 5 additions & 14 deletions templates/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,16 @@ limitations under the License.
package main

import (
"embed"
"{{.Name}}/internal/app/{{.Name}}/cmd"
)

var (
BuildDate string
GitBranch string
GitCommit string
GitState string
GitSummary string
Version string
//go:embed all:web/*
webDir embed.FS
)

func main() {
cmd.BuildDate = BuildDate
cmd.GitBranch = GitBranch
cmd.GitCommit = GitCommit
cmd.GitState = GitState
cmd.GitSummary = GitSummary
cmd.Version = Version
cmd.Execute()
cmd.WebDir = webDir
cmd.Execute()
}

93 changes: 75 additions & 18 deletions templates/build_cli.sh.tmpl
Original file line number Diff line number Diff line change
@@ -1,22 +1,79 @@
#!/bin/sh
printf "Building inside a docker container...\n"
#!/usr/bin/bash
printf "Building...\n"
OS=$1
ARCH=$2
# See https://docs.docker.com/compose/compose-file/
# See https://docs.docker.com/develop/develop-images/multistage-build/#before-multi-stage-builds
docker rmi -f {{.Name}}_cli:latest
GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose -f ./deployments/docker-compose.yml build {{.Name}}-cli
printf "Extracting from the built docker container image\n"
printf "Creating container...\n"
docker container create --name extractor {{.Name}}_cli:latest
printf "Copying file to local filesystem...\n"

if [ ! -d ./bin ]

CURRENT_FILE_PATH=$(realpath "$0")
CURRENT_DIR_PATH=$(dirname "${CURRENT_FILE_PATH}")
PROJECT_DIR_PATH=$(realpath "${CURRENT_DIR_PATH}"/..)
BINDIR_PATH=$(realpath "${PROJECT_DIR_PATH}"/bin)
if [ ! -d "${BINDIR_PATH}" ]
then
printf "Executables Directory not found. Creating...\n"
BINDIR_CREATE_CMD="mkdir ${BINDIR_PATH}"
printf "Creating executables directory using command\n%s\n" "${BINDIR_CREATE_CMD}"
$BINDIR_CREATE_CMD
fi

GIT_BRANCH=$(git symbolic-ref -q --short HEAD)
GIT_COMMIT=$(git rev-parse --short HEAD)
GIT_STATUS_PORCELAIN=$(git status --porcelain | wc -l)
GIT_STATE="clean"
if [ "$GIT_STATUS_PORCELAIN" -gt 0 ]
then
echo "Executables Directory not found. Creating..."
mkdir ./bin
GIT_STATE="dirty"
fi
docker container cp extractor:/bin/{{.Name}} ./bin/{{.Name}}-"$OS"-"$ARCH"
printf "Removing container...\n"
docker container rm -f extractor
docker rmi -f {{.Name}}_cli:latest
GIT_SUMMARY=$(git describe --tags --dirty --always)
BUILD_DATE=$(date --rfc-3339="ns")
VERSION=$(cat VERSION)

LDFLAGS_STRING="-extldflags '-static'"
cat > $PROJECT_DIR_PATH/pkg/schemas/semver.go <<EOF
package schemas
import (
"encoding/json"
"fmt"
)
type SemanticVersion struct {
GitBranch string \`json:"git_branch"\`
GitState string \`json:"git_state"\`
GitSummary string \`json:"git_summary"\`
BuildDate string \`json:"build_date"\`
Version string \`json:"version"\`
GitCommit string \`json:"git_version"\`
}
func (s *SemanticVersion) String() string {
return fmt.Sprintf("Version: %s\nBuilt On: %s\nBuilt From: %s (%s)\nGit State: %s\nGit Summary: %s\n", s.Version, s.BuildDate, s.GitCommit, s.GitBranch, s.GitState, s.GitSummary)
}
func (s *SemanticVersion) JSON() ([]byte, error) {
return json.Marshal(s)
}
var SemVer = &SemanticVersion{
GitBranch: "${GIT_BRANCH}",
GitState: "${GIT_STATE}",
GitSummary: "${GIT_SUMMARY}",
BuildDate: "${BUILD_DATE}",
Version: "${VERSION}",
GitCommit: "${GIT_COMMIT}",
}
EOF

export CGO_ENABLED=0
export GOOS=${OS}
export GOARCH=${ARCH}

BUILD_CMD=(go build -work -mod vendor -a -installsuffix cgo -ldflags="${LDFLAGS_STRING}" -o "${BINDIR_PATH}"/goboiler-"${OS}"-"${ARCH}")

printf "Building using command\nSHELLEXEC> %s" "${BUILD_CMD[*]}"
#for i in "${BUILD_CMD[@]}"
#do
# echo "${i} "
#done
printf "\n"

"${BUILD_CMD[@]}"
16 changes: 7 additions & 9 deletions templates/home.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package handlers

import (
"{{.Name}}/pkg/schemas"
"{{.Name}}/pkg/utils"
log "{{.LoggerPackage}}"
"github.com/spf13/viper"
"{{.Name}}/internal/app/{{.Name}}/server/middleware"
"{{.Name}}/pkg/schemas"
"net/http"
"strings"
)

type Home struct {
l *log.Logger
l *middleware.Log
}

func NewHome(l *log.Logger) *Home{
func NewHome(l *middleware.Log) *Home {
return &Home{
l: l,
}
Expand All @@ -27,11 +26,10 @@ func (h *Home) Handler(w http.ResponseWriter, r *http.Request) {

// https://stackoverflow.com/a/40382340
hp := schemas.HomePage{
AppName: strings.Title(strings.ToLower(viper.Get("app_name").(string))),
AppName: strings.ToTitle(strings.ToLower(viper.Get("app_name").(string))),
AppDesc: viper.Get("app_desc").(string),
}

h.l.Infof("hp = %v", hp)
u := utils.NewUtils(h.l)
u.RenderTemplate(w, "", "home", hp)
h.l.Utils.Log.Infof("hp = %v", hp)
h.l.Utils.RenderTemplate(w, "pages", "home", hp)
}
75 changes: 67 additions & 8 deletions templates/list.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package routes

import (
"{{.Name}}/internal/app/{{.Name}}/server/handlers"
"{{.Name}}/internal/app/{{.Name}}/server/middleware"
"fmt"
"{{.RouterPackage}}"
{{if eq .Router "chi" }}
"github.com/go-chi/httprate"
{{end}}
"{{.LoggerPackage}}"
"github.com/spf13/viper"
"{{.Name}}/internal/app/{{.Name}}/server/handlers"
"{{.Name}}/internal/app/{{.Name}}/server/middleware"
"io/fs"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
Expand All @@ -21,7 +22,7 @@ import (
"time"
)

func GetRoutesList(logger *logrus.Logger, router *chi.Mux, lm *middleware.Log) *Routes {
func GetRoutesList(router *chi.Mux, lm *middleware.Log) *Routes {
router.Use(lm.Handler)
{{if eq .Router "chi" }}
// Enable httprate request limiter of 100 requests per minute.
Expand All @@ -35,7 +36,40 @@ func GetRoutesList(logger *logrus.Logger, router *chi.Mux, lm *middleware.Log) *
router.Use(httprate.LimitByIP(100, 1*time.Minute))
{{end}}

staticFilesDir := http.Dir("./web/template/" + viper.GetString("theme") + "/static/")
defaultThemeName := "default"
themeName := viper.GetString("theme")
if themeName == "" {
themeName = defaultThemeName
}
staticFilesDirPath := fmt.Sprintf("web/template/%s/static", themeName)
lm.Utils.Log.Infof("Theme: %s, Theme Static Files Path: %s", themeName, staticFilesDirPath)

staticFilesDir, staticFilesDirErr := lm.WebDir.ReadDir(staticFilesDirPath)
if staticFilesDirErr != nil {
lm.Utils.Log.Fatalf("Error reading static files directory \"%s\" for theme: \"%s\" from embedded filesystem: \"%s\"", staticFilesDirPath, themeName, staticFilesDirErr.Error())
}

for k, staticFilesDirEntry := range staticFilesDir {
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: IsDir(): %t", k, staticFilesDirEntry.IsDir())
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Name(): %s", k, staticFilesDirEntry.Name())
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Type(): %d", k, staticFilesDirEntry.Type())
staticFilesDirEntryInfo, staticFilesDirEntryInfoErr := staticFilesDirEntry.Info()
if staticFilesDirEntryInfoErr != nil {
lm.Utils.Log.Errorf("Error getting staticFiles.DirEntry.Info %s", staticFilesDirEntryInfoErr.Error())
break
} else {
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Info.IsDir(): %t", k, staticFilesDirEntryInfo.IsDir())
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Info.Name(): %s", k, staticFilesDirEntryInfo.Name())
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Info.Size(): %d", k, staticFilesDirEntryInfo.Size())
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Info.Mode(): %d", k, staticFilesDirEntryInfo.Mode())
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Info.ModTime().String(): %s", k, staticFilesDirEntryInfo.ModTime().String())
infoSys := true
if staticFilesDirEntryInfo.Sys() == nil {
infoSys = false
}
lm.Utils.Log.Infof("StaticFilesDirEntry #%d: Info.ModTime().Sys(): %t", k, infoSys)
}
}

m := minify.New()
m.AddFunc("text/css", css.Minify)
Expand All @@ -45,16 +79,41 @@ func GetRoutesList(logger *logrus.Logger, router *chi.Mux, lm *middleware.Log) *
m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(staticFilesDir)))
staticFilesDirFS, staticFilesDirFSErr := fs.Sub(lm.WebDir, staticFilesDirPath)
if staticFilesDirFSErr != nil {
lm.Utils.Log.Errorf("Error getting a sub-directory as a filesystem from webDir: %s", staticFilesDirFSErr.Error())
}

domready, domreadyErr := staticFilesDirFS.Open("domready.js")
if domreadyErr != nil {
lm.Utils.Log.Errorf("Error reading file domready.js: %s", domreadyErr.Error())
}

domreadyStat, domreadyStatErr := domready.Stat()
if domreadyStatErr != nil {
lm.Utils.Log.Fatalf("Error stating file dmready.js: %s", domreadyStatErr.Error())
}
domreadySize := domreadyStat.Size()
domreadyData := make([]byte, domreadySize)

domreadyBytesRead, domreadyBytesReadErr := domready.Read(domreadyData)
if domreadyBytesReadErr != nil {
lm.Utils.Log.Fatalf("Error reading data for file domready.js: %s", domreadyBytesReadErr.Error())
}
lm.Utils.Log.Infof("total bytes read for domready.js: %d", domreadyBytesRead)
lm.Utils.Log.Infof("Data for domready.js=================\n%s\n===================end data=====================\n", domreadyData)

router.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFilesDirFS))))

return &Routes{
Routes: []*Route{
{
Pattern: "/",
Method: "GET",
Handler: handlers.NewHome(logger).Handler,
Handler: handlers.NewHome(lm).Handler,
},
},
Router: router,
Log: logger,
Log: lm,
}
}

0 comments on commit b66eb89

Please sign in to comment.