actions

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeHTML = iota
	ContentTypeJSON
)
View Source
const (
	DevelopmentEnvironment Environment = "development"
	TestEnvironment                    = "test"
	ProductionEnvironment              = "production"
)
View Source
const CookieLifetime = 2 * time.Hour

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func NewApp

func NewApp(conf Config) (*App, error)

func (*App) AuthCallback

func (app *App) AuthCallback(w http.ResponseWriter, r *http.Request)

func (*App) HomeGET

func (app *App) HomeGET(w http.ResponseWriter, r *http.Request)

HomeGET is a default handler to serve up a home page.

func (*App) LogoutGET

func (app *App) LogoutGET(w http.ResponseWriter, r *http.Request)

func (*App) RequireLogin

func (app *App) RequireLogin(next http.Handler) http.Handler

RequireLogin checks whether or not a user is logged in with an unexpired session cookie if the user is not logged in, then the frontend should redirect to /auth/google

func (*App) Start

func (app *App) Start()

Start begins listening for connections and serving clients in the background.

func (*App) Stop

func (app *App) Stop(ctx context.Context) error

Stop gracefully shuts down the server and closes the database. Set a timeout on the provided context to force shutdown after a certain amount of time.

func (*App) UserDELETE

func (app *App) UserDELETE(w http.ResponseWriter, r *http.Request)

UserDELETE handles DELETE /users/{id}

func (*App) UserEditGET

func (app *App) UserEditGET(w http.ResponseWriter, r *http.Request)

UserEditGET handles GET /users/{id}/edit

func (*App) UserGET

func (app *App) UserGET(w http.ResponseWriter, r *http.Request)

UserGET handles GET /users/{id}

func (*App) UserNewGET

func (app *App) UserNewGET(w http.ResponseWriter, r *http.Request)

UserNewGET handles GET /users/new

func (*App) UserPOST

func (app *App) UserPOST(w http.ResponseWriter, r *http.Request)

UserPOST handles POST /users

func (*App) UserPUT

func (app *App) UserPUT(w http.ResponseWriter, r *http.Request)

UserPUT handles PUT /users/{id}

func (*App) UsersGET

func (app *App) UsersGET(w http.ResponseWriter, r *http.Request)

UsersGET handles GET /users

type Config

type Config struct {
	ServerAddr    string        `envconfig:"SERVER_ADDR"`
	SessionSecret string        `envconfig:"SESSION_SECRET"`
	DeployEnv     Environment   `envconfig:"DEPLOY_ENV"`
	EnforceAuth   bool          `envconfig:"ENFORCE_AUTH"`
	FrontendHost  string        `envconfig:"FRONTEND_HOST"`
	DBConfig      models.Config `envconfig:"DB"`

	GoogleOAuthKey    string `envconfig:"GOOGLE_OAUTH_KEY"`
	GoogleOAuthSecret string `envconfig:"GOOGLE_OAUTH_SECRET"`
}

type ContentType

type ContentType int

func GetContentType

func GetContentType(r *http.Request) ContentType

GetContentType figures out which of the above supported content/media types we should use with our request, by checking first the "Content-Type" header, or falling back to the "Accept" header. Use it like so:

switch GetContentType(r) {
case ContentTypeHTML:
	// Render the response as HTML data
case ContentTypeJSON:
	// Render JSON
}

type Environment

type Environment string

func (Environment) IsProduction

func (e Environment) IsProduction() bool

type HTMLParams

type HTMLParams struct {
	// HTTP Status, defaults to http.StatusOK (200).
	Status int
	// Name of the template to render, e.g. "users/new" for templates/users/new.html.
	Template string
	// Data to pass to the template, often a map of key/values.
	Data any
	// Options for the renderer.
	HTMLOptions []render.HTMLOptions
	// A page title used by the layout.
	Title string
}

HTMLParams provides all the HTML function needs to render an HTML template.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer wraps the unrolled/render package in order to provide a few goodies (error rendering, session saving).

func NewRenderer

func NewRenderer(isProduction bool) (*Renderer, error)

func (*Renderer) Asset

func (r *Renderer) Asset(assetPath string) (string, error)

Asset returns the path to an asset by looking it up in the asset manifest. Webpack processes each asset (to minify CSS, for example) and generates the manifest to tell us where to find it. This function is available in templates as the "asset" function.

For example consider public/assets/application.js which gets processed by Webpack into public/dist/assets/application.[fingerpring].js. The manifest will have something like:

{
  "assets/application.js": "assets/application.39de836e61570e45cf00.js"
}

In a template we would put a script tag like:

<script src="{{ asset "assets/application.js" }}"></script>

Which would render as:

<script src="/assets/application.39de836e61570e45cf00.js"></script>

This ensures assets are cacheable and have unique URLs.

func (*Renderer) Data

func (r *Renderer) Data(w http.ResponseWriter, req *http.Request, status int, v []byte) error

Data writes out the raw bytes as binary data.

func (*Renderer) Error

func (r *Renderer) Error(w http.ResponseWriter, req *http.Request, status int, err error)

Error figures out how to render the given error message appropriate to the expected content type, while showing full error messages in dev but not in production.

func (*Renderer) HTML

func (r *Renderer) HTML(w http.ResponseWriter, req *http.Request, params HTMLParams) error

HTML builds up the response from the specified parameters.

func (*Renderer) HTMLError

func (r *Renderer) HTMLError(w http.ResponseWriter, req *http.Request, status int, err error)

HTMLError sends the user an HTML error page, hiding error details in production.

func (*Renderer) JSON

func (r *Renderer) JSON(w http.ResponseWriter, req *http.Request, status int, v interface{}) error

JSON marshals the given interface object and writes the JSON response.

func (*Renderer) JSONError

func (r *Renderer) JSONError(w http.ResponseWriter, req *http.Request, status int, err error)

HTMLError sends the user a JSON error payload, hiding error details in production.

func (*Renderer) JSONP

func (r *Renderer) JSONP(w http.ResponseWriter, req *http.Request, status int, callback string, v interface{}) error

JSONP marshals the given interface object and writes the JSON response.

func (*Renderer) Redirect

func (r *Renderer) Redirect(w http.ResponseWriter, req *http.Request, url string, status int)

func (*Renderer) Text

func (r *Renderer) Text(w http.ResponseWriter, req *http.Request, status int, v string) error

Text writes out a string as plain text.

func (*Renderer) XML

func (r *Renderer) XML(w http.ResponseWriter, req *http.Request, status int, v interface{}) error

XML marshals the given interface object and writes the XML response.

Jump to

Keyboard shortcuts

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