GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com β€” GitHub - gowebly/helpers: πŸ™‹ A most useful helpers for build the best Go web applications with the gowebly CLI (and not so).

BranchesTags
Open more actions menu

Latest commit

 

History

19 Commits
19 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
getenv.go
getenv.go
 
 
getenv_test.go
getenv_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
parse_templates.go
parse_templates.go
 
 
parse_templates_test.go
parse_templates_test.go
 
 
static_file_server_handler.go
static_file_server_handler.go
 
 

Repository files navigation

Helpers for the Gowebly CLI (and not so)

Go version Go report Code coverage License

A most useful helpers for build the best Go web applications with Gowebly CLI.

πŸ’‘ Note: You can use these helpers in other projects as well.

πŸ“– List of helpers

gowebly.Getenv

Helper to get the given environment variable. If key is not found, sets a fallback value.

import (
    gowebly "github.com/gowebly/helpers"
)

// Get a value of the environment variable 'BACKEND_PORT'
// or sets it to a fallback value '5000'.
gowebly.Getenv("BACKEND_PORT", "5000")

πŸ’‘ Note: This is a more advanced version of the built-in os.Getenv function.

gowebly.ParseTemplates

Helper to parse list of the given templates to the HTTP handler.

import (
    "log/slog"

    gowebly "github.com/gowebly/helpers"
)

func handler(w http.ResponseWriter, r *http.Request) {
    // Define paths to the user templates.
    indexPage := filepath.Join("templates", "pages", "index.html")
    indexLoginForm := filepath.Join("templates", "components", "index-login-form.html")

    // Parse user templates, using gowebly helper, or return error.
    tmpl, err := gowebly.ParseTemplates(indexPage, indexLoginForm)
    if err != nil {
        w.WriteHeader(http.StatusBadRequest)
        slog.Error(err.Error(), "method", r.Method, "status", http.StatusBadRequest, "path", r.URL.Path)
        return
    }

    // Execute (render) all templates or return error.
    if err := tmpl.Execute(w, nil); err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        slog.Error(err.Error(), "method", r.Method, "status", http.StatusInternalServerError, "path", r.URL.Path)
        return
    }
}

πŸ’‘ Note: The main layout template (templates/main.html) is already included.

gowebly.ParseTemplatesWithCustomMainLayout

Helper to parse a list of the given templates with a custom main layout to the HTTP handler. Useful to use at times when you want to override file name of the default templates/main.html layout template.

import (
    "log/slog"

    gowebly "github.com/gowebly/helpers"
)

func handler(w http.ResponseWriter, r *http.Request) {
    // Define path to the main layout template.
    customMainLayout := filepath.Join("templates", "my-custom-main.html")

    // Define paths to the user templates.
    indexPage := filepath.Join("templates", "pages", "index.html")
    indexLoginForm := filepath.Join("templates", "components", "index-login-form.html")

    // Parse user templates or return error.
    tmpl, err := gowebly.ParseTemplatesWithCustomMainLayout(customMainLayout, indexPage, indexLoginForm)
    if err != nil {
        w.WriteHeader(http.StatusBadRequest)
        slog.Error(err.Error(), "method", r.Method, "status", http.StatusBadRequest, "path", r.URL.Path)
        return
    }

    // Execute (render) all templates or return error.
    if err := tmpl.Execute(w, nil); err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        slog.Error(err.Error(), "method", r.Method, "status", http.StatusInternalServerError, "path", r.URL.Path)
        return
    }
}

gowebly.StaticFileServerHandler

Helpers to create a custom handler for serve embed ./static folder.

import (
    "embed"
    "net/http"

    gowebly "github.com/gowebly/helpers"
)

//go:embed static/*
var static embed.FS

// Create the gowebly helper for serve embed static folder.
staticFileServer := gowebly.StaticFileServerHandler(http.FS(static))

// Handle static files (with a custom handler).
http.Handle("/static/", staticFileServer)

⚠️ License

The Gowebly Helpers is free and open-source software licensed under the Apache 2.0 License, created and supported by Vic Shóstak with 🩡 for people and robots.

About

πŸ™‹ A most useful helpers for build the best Go web applications with the gowebly CLI (and not so).

gowebly.org

Topics

gogolanggolang-librarygolang-packagegowebly

Resources

Readme
Apache-2.0 license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

10 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402520 β€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800Γ—600. No cookies required.