internal

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiSpecification

type ApiSpecification struct {
	// RedirectorUriScheme refers to where you intend to host your publicly available redirects when
	// a user enters the shorten URL into their browser.
	// This API does not handle the redirects as it's conforming to the architectural
	// constraints of REST while allowing flexibility for any client that chooses to
	// use this API
	RedirectorUriScheme string `required:"true" split_words:"true"`

	// RedirectorUriHost refers to where you intend to host your publicly available redirects when
	// a user enters the shorten URL into their browser.
	// This API does not handle the redirects as it's conforming to the architectural
	// constraints of REST while allowing flexibility for any client that chooses to
	// use this API
	RedirectorUriHost string `required:"true" split_words:"true"`

	DbUrl            string `required:"true" split_words:"true"`
	ListenAddress    string `default:":8080" split_words:"true"`
	AlphaNumericBase string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"`
}

ApiSpecification defines the env vars required to run the API.

func NewApiSpecification

func NewApiSpecification() (*ApiSpecification, error)

NewApiSpecification provides a convenient constructor for mapping the env vars needed for the API to ApiSpecification.

type Postgres

type Postgres struct {
	Conn *pgxpool.Pool
}

Postgres embeds the postgres connection and allows the storage.Storage to be satisfied

func NewPostgres

func NewPostgres(connString string) (*Postgres, error)

NewPostgres connects to a postgres instance using the provided connection string and then constructs a fresh Postgres The underlying Postgres.Conn field is a pool so can be reused and is concurrent safe.

func (*Postgres) Load

func (p *Postgres) Load(id int) (string, error)

Load will give back the original url that was used to be shortened

func (*Postgres) Ping

func (p *Postgres) Ping(ctx context.Context) (bool, error)

Ping is just using pgx's ping method off of pgxpool.Pool but with the addition of a boolean returned as well as an error message

func (*Postgres) SaveOrKey

func (p *Postgres) SaveOrKey(ctx context.Context, url string) (int, error)

SaveOrKey will look to see if the url is already been shortened, otherwise it'll create a new record in the DB

type Server

type Server struct {
	Config *shortener.Config
	Logger *zap.SugaredLogger
	// contains filtered or unexported fields
}

Server acts as our HTTP server along the dependencies required to operate the server. Via Server, we can register the routing for all our HTTP handlers.

func (*Server) DocsHandler

func (e *Server) DocsHandler(w http.ResponseWriter, _ *http.Request)

func (*Server) HealthCheckHandler

func (e *Server) HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler basically just does a simple check to establish if the database connection is available

func (*Server) ShortenHandler

func (e *Server) ShortenHandler(w http.ResponseWriter, r *http.Request)

ShortenHandler takes the URL provided by the client, passing it to the shortener, and returns the result as JSON. For POST methods it'll try to shorten the URL and return it, and for GET methods it'll retrieve the non-shorten URL based on the provided shorten URL in the request param "?url="

func (*Server) Shutdown

func (e *Server) Shutdown(timeout time.Duration) error

Shutdown can be utilised to gracefully shut down the http server in Server as well as giving the option to provide a timeout, so we aren't waiting too long on any long-running operations

func (*Server) Start

func (e *Server) Start(listenAddress string) error

Start registers the routes and the listening address and then starts up the http server. It's useful to call this within a go routine, so you can listen out for any os signals and gracefully shut down on any signal that causes the program to stop abruptly via Shutdown

Directories

Path Synopsis
Package bijective implements a basic way to get obtain a slice of integers from a seed that can be inverted back to the original seed
Package bijective implements a basic way to get obtain a slice of integers from a seed that can be inverted back to the original seed

Jump to

Keyboard shortcuts

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