gowebly

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 6 Imported by: 7

README

gowebly helpers

Go version Go report Code coverage License

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

📖 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.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

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Getenv

func Getenv(key, fallback string) string

Getenv gets the given environment variable. This is a more advanced version of the built-in os.Getenv function.

If key is not found, Getenv sets it to a fallback value.

Example:

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")

func ParseTemplates

func ParseTemplates(names ...string) (*template.Template, error)

ParseTemplates parses list of the given templates to the HTTP handler.

Already included 'templates/main.html' layout template from your project path.

Example:

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 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
	}
}

func StaticFileServerHandler

func StaticFileServerHandler(fs http.FileSystem) http.Handler

StaticFileServerHandler handles a custom handler for serve embed static folder.

Example:

import (
	"embed"
	"net/http"

	gowebly "github.com/gowebly/helpers"
)

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

// Handle static files (with a custom handler).
http.Handle("/static/", gowebly.StaticFileServerHandler(http.FS(static)))

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL