Skip to content

Commit

Permalink
Automated commit by GitLab CI/CD - Personal Bot
Browse files Browse the repository at this point in the history
  • Loading branch information
poniatowski-bot committed Nov 6, 2023
1 parent 97924c8 commit 1e02ac1
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 22 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [1.1.1] - 29-10-2023
## [1.1.2] - 2-11-2023
### Added
- A profiler, with an env variable check, to output how long a task has taken for a number of tasks/functions.

- Added documentation for existing functions/methods/structs
- Dockerfile inclusion
58 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#build stage
FROM golang:alpine AS builder
ARG VERSION
RUN apk add --no-cache git
WORKDIR /go/src/app
COPY . .
RUN go get -d -v ./...
RUN go build -o /go/bin/app -v ./...

#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /go/bin/app /app
COPY --from=builder /go/src/app/VERSION /VERSION
ENTRYPOINT ["/app"]
LABEL Name=zehd Version="$(cat VERSION)"
EXPOSE 80

# syntax=docker/dockerfile:1

################################################################################################################################
# FROM golang:alpine AS builder
# LABEL MAINTAINER="Adam Poniatowski <adaml.poniatowski@gmail.com>"
# LABEL MICROSERVICE="frontend"


# # # Download and install the latest release of dep
# ADD https://github.com/golang/dep/releases/download/v0.5.4/dep-linux-amd64 /usr/bin/dep
# RUN chmod +x /usr/bin/dep

# # install git, openssh-client and configure (if it resides in a private repo)
# # openssh-client and the git config line is optional
# RUN apk add git openssh-client
# RUN git config --global url."ssh://git@git.poniatowski.dev/adam/*".insteadOf https://git.poniatowski.dev/adam/*

# # As this is a private repo, copy a key that has been generated and public key added to github
# RUN mkdir /root/.ssh/
# ADD ~/.ssh/id_rsa /root/.ssh/id_rsa
# RUN chmod 600 /root/.ssh/id_rsa
# RUN ssh-keyscan git.poniatowski.dev > /root/.ssh/known_hosts

# # Copy the code from the host and compile it
# WORKDIR $GOPATH/src/git.poniatowski.dev/adam/zehd-frontend
# RUN git clone git@git.poniatowski.dev:adam/zehd-frontend.git .
# RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /frontend-bin .
# RUN tar -zcf /package.tar.gz ./templates

# # # Copy the binary to new container and run
# FROM alpine
# RUN mkdir -p ~/frontend/templates
# WORKDIR ~
# COPY --from=builder /package.tar.gz .
# COPY --from=builder /frontend-bin .
# RUN mkdir -p /var/frontend/templates
# RUN tar -zxf package.tar.gz -C /var/frontend
# RUN rm package.tar.gz
# RUN setcap 'cap_net_bind_service,cap_sys_time,cap_sys_resource,cap_sys_nice+ep' ./frontend-bin
# ENTRYPOINT ["./frontend-bin"]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1
1.1.2
11 changes: 6 additions & 5 deletions internal/backendconnector/backendConnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
"log"
"net/http"
"os"
"zehd-frontend/internal/logging"
"sync"
"time"

"zehd-frontend/internal/logging"
)

// DatabaseExistsInfo Struct for checking if the DB exists
type DatabaseExistsInfo struct {
Frontend string `json:"frontend"`
Connection string `json:"connection"`
Tables string `json:"tables"`
}

// RequestData Struct for checking and collecting user requests via headers
type RequestData struct {
FrontendName string `json:"frontendName"`
TimeDate int64 `json:"timeDate"`
Expand All @@ -36,7 +37,7 @@ type RequestData struct {
CFIPCountry string `json:"CF-IPCountry"`
}

// DatabaseInit - Initialize the database with tables, if the check returns false
// DatabaseInit Initialize the database with tables, if the check returns false
func DatabaseInit() error {
defer logging.TrackTime("db-init", time.Now())
var databaseInfo DatabaseExistsInfo
Expand Down Expand Up @@ -68,7 +69,7 @@ func DatabaseInit() error {
return nil
}

// DatabaseExists - Check if database exists and has existing tables
// DatabaseExists Check if database exists and has existing tables
func (dbInfo *DatabaseExistsInfo) DatabaseExists() {
defer logging.TrackTime("db-exists", time.Now())
backendURL := os.Getenv("BACKEND")
Expand Down Expand Up @@ -119,7 +120,7 @@ func (dbInfo *DatabaseExistsInfo) DatabaseCreate() {
}()
}

// DBConnector Function to insert request data into the database
// DBConnector Function to insert request data into the database
func (rD *RequestData) DBConnector(waitGroup *sync.WaitGroup) {
defer logging.TrackTime("db-connector", time.Now())
waitGroup.Add(1)
Expand Down
12 changes: 7 additions & 5 deletions internal/caching/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"time"

"zehd-frontend/internal"
"zehd-frontend/internal/logging"
"strings"
"time"
)

// Pages Struct for caching templates and routes
type Pages struct {
RouteMap map[string]*template.Template
}

// CachePages Method that walks the specified or default directories and caches the templates
func (pages *Pages) CachePages() error {
defer logging.TrackTime("cacher", time.Now())
errchdir := os.Chdir(internal.TemplatesDir + internal.TemplateType)
Expand Down Expand Up @@ -49,7 +50,7 @@ func (pages *Pages) CachePages() error {
filetype = "invalid"
}
name := strings.TrimSuffix(croppedtemplatepath, filepath.Ext(path))
tmpl, err := templatebuilder(croppedtemplatepath, filetype)
tmpl, err := templateBuilder(croppedtemplatepath, filetype)
if err != nil {
return fmt.Errorf("failed to build template for file %q: %v", path, err)
}
Expand All @@ -63,7 +64,8 @@ func (pages *Pages) CachePages() error {
return nil
}

func templatebuilder(page, filetype string) (*template.Template, error) {
// templateBuilder Private function for building templates, which is called by CachePages
func templateBuilder(page, filetype string) (*template.Template, error) {
defer logging.TrackTime("template-builder", time.Now())
if filetype == "invalid" {
return nil, errors.New("invalid filetype: " + page)
Expand Down
6 changes: 4 additions & 2 deletions internal/caching/cachingHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"html/template"
"os"
"zehd-frontend/internal/logging"
"time"

"github.com/gomarkdown/markdown"
"github.com/russross/blackfriday/v2"

"zehd-frontend/internal/logging"
)

// pageBuilder Private helper function that builds HTML/goHTML pages and returns the templates
func pageBuilder(templatePath, layoutPath string) (*template.Template, error) {
defer logging.TrackTime("page-builder", time.Now())
templates := template.New("")
Expand All @@ -30,6 +30,7 @@ func pageBuilder(templatePath, layoutPath string) (*template.Template, error) {
return templates, nil
}

// convertOrgToTemplate Private helper function that builds org-mode pages and returns the templates
func convertOrgToTemplate(orgPath, layoutPath string) (*template.Template, error) {
defer logging.TrackTime("org-converter", time.Now())
templates := template.New("")
Expand Down Expand Up @@ -73,6 +74,7 @@ func convertOrgToTemplate(orgPath, layoutPath string) (*template.Template, error
return templates, nil
}

// convertMarkdownToTemplate Private helper function that builds markdown pages and returns the templates
func convertMarkdownToTemplate(markdownPath, layoutPath string) (*template.Template, error) {
defer logging.TrackTime("md-converter", time.Now())
templates := template.New("")
Expand Down
7 changes: 3 additions & 4 deletions internal/datacapturing/dataCapturing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"net"
"net/http"
"os"
"zehd-frontend/internal/backendconnector"
"zehd-frontend/internal/logging"
"strconv"
"strings"
"sync"
"time"

timeSmithy "github.com/aws/smithy-go/time"

"zehd-frontend/internal/backendconnector"
"zehd-frontend/internal/logging"
)

// CollectData - Collects all data from http request, parses it and sends it to the database
// CollectData Collects all data from http request, parses it and sends it to the database
func CollectData(r *http.Request) {
defer logging.TrackTime("data-collector", time.Now())
var rD backendconnector.RequestData
Expand Down
6 changes: 6 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// get the environment var for the port you'd prefer to use via ENV in k8s/dockerfile,
// or alternatively it will default to 80, if no port was specified

// EnvPort get the port from environment variables or return the default (80)
func EnvPort() string {
port := os.Getenv("PORT")
if len(port) == 0 {
Expand All @@ -16,6 +17,7 @@ func EnvPort() string {
return ":" + port
}

// EnvHostname get the hostname from environment variables or return the default (the hostname of the server)
func EnvHostname() string {
hostname, err := os.Hostname()
if err != nil {
Expand All @@ -26,6 +28,7 @@ func EnvHostname() string {
}
}

// EnvTemplateDir get the templates directory from environment variables or return the default (/var/frontend/templates/)
func EnvTemplateDir() string {
templateDir := os.Getenv("TEMPLATEDIRECTORY")
if len(templateDir) == 0 {
Expand All @@ -34,6 +37,7 @@ func EnvTemplateDir() string {
return templateDir
}

// EnvTemplateType get the templates type from environment variables or return the default (.gohtml)
func EnvTemplateType() string {
templateType := os.Getenv("TEMPLATETYPE")
if len(templateType) == 0 {
Expand All @@ -42,6 +46,7 @@ func EnvTemplateType() string {
return templateType
}

// EnvCacheRefresh get the cache refresh from environment variables or return the default (60)
func EnvCacheRefresh() string {
timer := os.Getenv("REFRESHCACHE")
if len(timer) == 0 {
Expand All @@ -50,6 +55,7 @@ func EnvCacheRefresh() string {
return timer
}

// EnvProfiler get the profiler switch from environment variables or return the default (false)
func EnvProfiler() string {
profiler := os.Getenv("PROFILER")
if len(profiler) == 0 {
Expand Down
5 changes: 3 additions & 2 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"log"
"os"
"path/filepath"
"zehd-frontend/internal/env"
"strconv"
"time"

"zehd-frontend/internal/env"
)

// LogIt Boilerplate funtion that calls Logger, to write/prints logs
func LogIt(logFunction string, logOutput string, message string) {
errCloseLogger := Logger(logFunction, logOutput, message)
if errCloseLogger != nil {
log.Println(errCloseLogger)
}
}

// Logger This function is called by Logit and prints/writes logs
func Logger(logFunction string, logOutput string, message string) error {
currentDate := time.Now().Format("2006-01-02 15:04:05")
pathString := os.Getenv("HOME") + "/log/"
Expand Down

0 comments on commit 1e02ac1

Please sign in to comment.