ascanvas

package module
v0.0.0-...-d041062 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2021 License: MIT Imports: 8 Imported by: 0

README

ASCII Canvas

A simple web server to perform drawing operations on a text canvas.

This is intended for educational purposes.

Build Requirements

The project was built using go 1.17.

In case of code modification, the following projects are used for code generation and as such, must be installed.

They are executed automatically on go generate.

Name Link Generates
swag https://github.com/swaggo/swag Swagger documentation for http endpoints
mockery https://github.com/vektra/mockery Mock implementations for testing

Building

It is recommended to use make to build the project

Command Description
make build Build the binary into the build subfolder
make clean Clean up previous builds

Running

Build the binary first and then execute:

cd build
./ascanvas

You need to have the build folder as writable and port 1337 opened.

You may customize the database and listen address by creating a file ascanvas.json in the same directory as the binary (build folder).

A sample of this file is included as ascanvas.json.dist

Using

The server can be accessed via web interface:

URL Description
http://127.0.0.1:1337/swagger View API endpoints and perform requests using Swagger UI
http://127.0.0.1:1337/ View listing of canvas items and access live update UI

License

This project is provided under the MIT license. A copy of the license found in this repository.

Documentation

Index

Constants

View Source
const ObserveALL = "all"

ObserveALL is a special keyword to observe all events

Variables

View Source
var (
	// ErrInvalidInput is for validation errors
	ErrInvalidInput = errors.New("invalid input")

	// ErrNotFound is when we cannot find something
	ErrNotFound = errors.New("item not found")

	// ErrOutOfBounds is when a cooridnate is out of bounds
	ErrOutOfBounds = errors.New("out of bounds")
)
View Source
var IndexHTML []byte

Functions

func AsyncBroadcast

func AsyncBroadcast(ctx context.Context, b CanvasBroadcaster, l *zap.Logger, event CanvasEvent)

AsyncBroadcast uses a go routine

func SyncBroadcast

func SyncBroadcast(ctx context.Context, b CanvasBroadcaster, l *zap.Logger, event CanvasEvent)

SyncBroadcast is blocking - useful for testing

func TransformFloodfill

func TransformFloodfill(canvas *Canvas, args TransformFloodfillArgs) error

func TransformRectangle

func TransformRectangle(canvas *Canvas, args TransformRectangleArgs) error

func UUIDGenerator

func UUIDGenerator() (string, error)

UUIDGenerator is a uuid v4 generator

Types

type BroadcastFunc

type BroadcastFunc func(ctx context.Context, b CanvasBroadcaster, l *zap.Logger, event CanvasEvent)

BroadcastFunc denotes functions used for wrapping broadcasting features

type Canvas

type Canvas struct {
	Id      string `json:"id"`
	Name    string `json:"name"`
	Content string `json:"content"`
	Width   int    `json:"width"`
	Height  int    `json:"height"`
}

Canvas is an ascii art drawing

func (Canvas) AsGrid

func (c Canvas) AsGrid() [][]string

func (Canvas) AsLogFields

func (c Canvas) AsLogFields() []zap.Field

AsLogFields is a helper for logging

func (Canvas) Contains

func (c Canvas) Contains(coords Coordinates) bool

func (*Canvas) FromGrid

func (c *Canvas) FromGrid(grid [][]string)

func (Canvas) String

func (c Canvas) String() string

type CanvasBroadcaster

type CanvasBroadcaster interface {
	Observe(ctx context.Context, id string) (StopObserveFunc, <-chan CanvasEvent, error)
	Broadcast(ctx context.Context, event CanvasEvent) error
}

CanvasBroadcaster allows changes to be published or observed on a specific canvas

type CanvasEvent

type CanvasEvent struct {
	Name   CanvasEventName
	Canvas Canvas
}

CanvasEvent emitted by a CanvasBroadcaster

type CanvasEventName

type CanvasEventName string

CanvasEventName is the type of event emitted by CanvasEvent

const (
	// CanvasEventCreated is emitted when a new Canvas has been created
	CanvasEventCreated CanvasEventName = "CREATED"

	// CanvasEventUpdated is emitted when an existing Canvas has been updated
	CanvasEventUpdated CanvasEventName = "UPDATED"

	// CanvasEventDeleted is emitted when an existing Canvas has been deleted
	CanvasEventDeleted CanvasEventName = "DELETED"
)

type CanvasRepository

type CanvasRepository interface {
	Create(ctx context.Context, canvas Canvas) error
	Update(ctx context.Context, canvas Canvas) error
	Get(ctx context.Context, id string) (*Canvas, error)
	List(ctx context.Context) ([]Canvas, error)
	Delete(ctx context.Context, id string) error
}

CanvasRepository is for canvas persistence

type CanvasService

type CanvasService struct {
	Repo        CanvasRepository
	BroadCaster CanvasBroadcaster
	Logger      *zap.Logger
	GenerateID  UUIDGeneratorFunc
	Broadcast   BroadcastFunc
}

CanvasService provides applicative features

func (CanvasService) ApplyFloodfill

func (s CanvasService) ApplyFloodfill(ctx context.Context, id string, args TransformFloodfillArgs) (*Canvas, error)

func (CanvasService) ApplyRectangle

func (s CanvasService) ApplyRectangle(ctx context.Context, id string, args TransformRectangleArgs) (*Canvas, error)

ApplyRectangle loads a Canvas and uses TransformRectangle on it

func (CanvasService) Create

func (s CanvasService) Create(ctx context.Context, args CreateArgs) (*Canvas, error)

Create a new Canvas

func (CanvasService) Delete

func (s CanvasService) Delete(ctx context.Context, id string) error

Delete a specific Canvas item

func (CanvasService) Get

func (s CanvasService) Get(ctx context.Context, id string) (*Canvas, error)

Get one specific Canvas item

func (CanvasService) List

func (s CanvasService) List(ctx context.Context) ([]Canvas, error)

List all available Canvas items

func (CanvasService) Observe

func (s CanvasService) Observe(ctx context.Context, id string) (StopObserveFunc, <-chan CanvasEvent, error)

type Coordinates

type Coordinates struct {
	X int `json:"x"`
	Y int `json:"y"`
}

Coordinates on a cartesian plane

func (Coordinates) String

func (c Coordinates) String() string

type CreateArgs

type CreateArgs struct {
	Name   string `json:"name"`
	Fill   string `json:"fill"`
	Width  int    `json:"width"`
	Height int    `json:"height"`
}

func (CreateArgs) AsLogFields

func (a CreateArgs) AsLogFields() []zap.Field

AsLogFields is a helper for logging

func (CreateArgs) Validate

func (a CreateArgs) Validate() error

type StopObserveFunc

type StopObserveFunc func()

StopObserveFunc can be used to stop listening after calling Observe

type TransformFloodfillArgs

type TransformFloodfillArgs struct {
	Start Coordinates `json:"start"`
	Fill  string      `json:"fill"`
}

func (TransformFloodfillArgs) Validate

func (a TransformFloodfillArgs) Validate() error

type TransformRectangleArgs

type TransformRectangleArgs struct {
	TopLeft Coordinates `json:"top_left"`
	Width   int         `json:"width"`
	Height  int         `json:"height"`
	Fill    string      `json:"fill"`
	Outline string      `json:"outline"`
}

func (TransformRectangleArgs) Validate

func (a TransformRectangleArgs) Validate() error

type UUIDGeneratorFunc

type UUIDGeneratorFunc func() (string, error)

UUIDGeneratorFunc denotes functions that can provide uuids

func StaticUUIDGenerator

func StaticUUIDGenerator(id string, err error) UUIDGeneratorFunc

StaticUUIDGenerator generates predefined uuid and err; useful for testing

Directories

Path Synopsis
broadcaster
cmd
docs
ascanvas
Package ascanvas GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
Package ascanvas GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
repo
web

Jump to

Keyboard shortcuts

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