serve

package
v0.0.0-...-71c6276 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

README

serve package

Static Web

serve.Static is a helper for running static web containers. Main purpose is to provide mechanism to expose container environment variables to the front-end application.

Example
main.go

If you create the following program,

package main

import (
	"embed"
	"gitlab.com/pgmtc-lib/commons/serve"
	"gitlab.com/pgmtc-lib/commons/svc"
	"log"
	"net/http"
)

//go:embed dist
var content embed.FS

func main() {
  var server := serve.Static(
    // allows front end routing - redirects 404 to /index.html
    serve.WithSPA,
    // listener
    serve.WithAddr(":8080"),
    // server content from var content, strip path dist
    serve.WithContent(content, "dist"),
    // expose only these env variables
    serve.WithExposedEnvVariables("ENV_VAR1", "ENV_VAR2"),
    // GZIP content
    serve.WithCompression,
    // You can intercept request before it is processed, return true to stop
    serve.WithIntercept(func(w http.ResponseWriter, r *http.Request) bool {
      log.Println(r.URL.Path)
      return false
    }),
  ) 
  // run ...
  if err := server(); err != nil {
    log.Fatal(err.Error())
  }
  // ...
}

Then once you build the container, front end application (which was in dist directory) can access container's env variables using the following relative paths:

json map (GET /env)
{
  "ENV_VAR1":"env. variable 1",
  "ENV_VAR2":"env. variable 2",
}

You can get this using AJAX request.

javascript map object (GET /env.js)

which is assigned to the top level window object (get /env.js).

window.env = new Map()
window.env.set("ENV_VAR1", "env. variable 1")
window.env.set("ENV_VAR2", "env. variable 2")

If you then reference this in the index.html, you will be able to access variables in your front end application. For example:

<script src=/env.js></script>
...
<script>
function sometimesLaterOn() {
   console.log("env variable 1 = ", window.env.get("ENV_VAR1)
}
</script>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Static

func Static(opts ...Option) svc.Runner

Static creates static server which can expose env variables in /env handler. Returns svc.Runner

	var server = serve.Static(
	  WithAddr("localhost:7777"),
	  WithContent(content, "test-html"),
	  WithExposedEnvVariables("HOME", "SHELL"))
 err := server()

func StaticHandler

func StaticHandler(opts ...Option) http.Handler

StaticHandler creates static server which can expose env variables in /env. Returns http.handler

func WithCompression

func WithCompression(s *staticServer)

WithCompression instructs server to gzip files

func WithSPA

func WithSPA(s *staticServer)

WithSPA switches on SPA mode, which redirect 404 to index.html

serve.Static(serve.WithSPA)

Types

type Option

type Option func(s *staticServer)

Option is an option for static server

func WithAddr

func WithAddr(addr string) Option

WithAddr is an option which sets the custom listener address

func WithContent

func WithContent(content fs.FS, subPath string) Option

WithContent is an option which sets content and it's subdir. Can be used with embed

//go:embed public-html
var content embed.FS
...
serve.Static(serve.WithContent(content, "public-html"))

func WithExposedEnvVariables

func WithExposedEnvVariables(envVariable ...string) Option

WithExposedEnvVariables is an option which tells the static server to expose environment variable(s)

serve.Static(serve.WithExposedVariables("HOME", "SHELL", "API_ADDR")

func WithIntercept

func WithIntercept(intercept func(w http.ResponseWriter, r *http.Request) bool) Option

WithIntercept allows intercept flow with custom function

serve.Static(serve.WithIntercept(myHandlerFn))

Jump to

Keyboard shortcuts

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