srvr

package
v0.0.0-...-ae54cf2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 17 Imported by: 0

README

srvr GoDoc

This package contains all of the code for the web server that wraps the simulator

Routes

GET /

Route for the website.

GET /api/simulation

Return the list of simulations on the server.

POST /api/simulation

Create a new simulation.

PUT /api/simulation/notes

Updates notes recorded about this list of simulations.

GET /api/simulation/{sim_id}

Return a specific simulation. Contains the network options for the network if set.

PUT /api/simulation/{sim_id}

Updates details about the specified simulation.

DELETE /api/simulation/{sim_id}

Deletes the specified simulation

POST /api/simulation/{sim_id}/generate

Generates a hierarchical network to be simulated in an existing simulation. There should be no existing steps within the simulation otherwise this request will fail. Returns the created first step that contains the generated network and the initial color results for the generated network.

POST /api/simulation/{sim_id}/parse

Parses a network from a byte array to be simulated in an existing simulation. There should be no existing steps within the simulation otherwise this request will fail. Returns the created first step that contains the generated network and the initial color results for the generated network.

POST /api/simulation/{sim_id}/run

Runs the simulation for a specified number of steps, each step runs a specified number of iterations.

GET /api/simulation/{sim_id}/step

Returns the list of steps in this simulation. This returns the actual content of the steps as opposed to the list of step paths that is returned in GET /api/simulation/{sim_id}

GET /api/simulation/{sim_id}/results

Returns the concatenated set of results for all the steps in this simulation.

GET /api/simulation/{sim_id}/step/{step_id}

Returns the specified step which contains the results for that step and the state of the network at the end of that step.

PUT /api/simulation/{sim_id}/step/{step_id}

Updates the results and network state in the specified step

DELETE /api/simulation/{sim_id}/step/{step_id}

Deletes the specified step

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateRouter

func CreateRouter(fm FileManager) *mango.Router

CreateRouter registers the route handlers. This function allows the route handlers to be tested with the mango.Browser

func ListenAndServe

func ListenAndServe(rootpath string, webpath string, webfs fs.FS, port string)

ListenAndServe launches the web server rootpath is the root directory where data served by this server is persisted webpath is the root directory of the static website served by this server webfs is the embedded copy of the static website that will be served if the webpath is an empty string port is the port to listen on

Types

type FileDetails

type FileDetails struct {
	Rootpath string
	Filepath string
	Lock     sync.RWMutex
	DirLock  *sync.RWMutex
}

FileDetails contains information about a file that will be managed by the FileUpdater

func (*FileDetails) Create

func (fd *FileDetails) Create(obj Persistable) error

Create creates a file and saves the supplied object into the file identified by path It returns the FileUpdater that should be used to access the file

func (*FileDetails) Delete

func (fd *FileDetails) Delete() error

Delete the file on the file system

func (*FileDetails) Path

func (fd *FileDetails) Path() string

Path to the file on the file system

func (*FileDetails) Read

func (fd *FileDetails) Read(obj Persistable) error

Read the file and unmarshal it into the supplied object. If the file hasn't changed since it was last read this function will do nothing. If the file is re-read then the Timestamp on the supplied object is updated

func (*FileDetails) Update

func (fd *FileDetails) Update(obj Persistable) error

Update the contents of the file with the supplied persistable object. If the write to the file is successful, then the Timestamp of the persistable is updated too.

type FileManager

type FileManager interface {
	Get(path string) FileUpdater
}

FileManager is a repository for all instances of FileUpdater

func NewFileManager

func NewFileManager(rootpath string) FileManager

NewFileManager returns a new instance of FileManager

type FileUpdater

type FileUpdater interface {
	Create(obj Persistable) error
	Read(obj Persistable) error
	Update(obj Persistable) error
	Delete() error
	Path() string
}

FileUpdater manages a file and provides Read and Update methods that allow the rest of the package to read and update a file. FileUpdater contains a lock to prevent multiple goroutines from trying to write at the same time and also prevents Reads from occuring whilst an update is being performed. The Update function checks the last modified time of the file before it updates it, if it has been modified since the last copy was read, then Update will return an error, and the Update method will attempt to re-read the contents of the file into the object supplied.

type ListHandlerState

type ListHandlerState struct {
	FileManager FileManager
	EncodeFunc  func(ListHolder, string) (interface{}, error)
}

ListHandlerState holds state information for a ListHandler

func (*ListHandlerState) AddItem

func (lh *ListHandlerState) AddItem(itemToAdd ListItem, listHolder ListHolder, c *mango.Context, listname string) error

AddItem adds the passed item to the specified list on the passed listholder

func (*ListHandlerState) AddItemWithContextBind

func (lh *ListHandlerState) AddItemWithContextBind(itemToAdd ListItem, listHolder ListHolder, c *mango.Context, listname string)

AddItemWithContextBind binds the itemToAdd to the object passed in the mango context and adds it to the specified list on the passed listholder

func (*ListHandlerState) DeleteItem

func (lh *ListHandlerState) DeleteItem(itemToDelete ListItem, listHolder ListHolder, c *mango.Context, listname string)

DeleteItem removes an item from the specified list on the passed listholder

func (*ListHandlerState) GetList

func (lh *ListHandlerState) GetList(listHolder ListHolder, c *mango.Context, listname string)

GetList returns the list of items

type ListHolder

type ListHolder interface {
	Persistable
	GetItems(listname string) []string
	UpdateItems(listname string, items []string)
}

ListHolder is an interface that any persistable that contains a list must implement

type ListItem

type ListItem interface {
	Persistable
	RelPath() string
}

ListItem is an interface that any persistable that can be added to a list must implement

type ParseBody

type ParseBody struct {
	sim.ParseOptions
	Payload []byte
}

ParseBody is the payload struct for uploading a network in a text file to be parsed together with the options that specify how to parse the text file

type Persistable

type Persistable interface {
	Timestamp() time.Time
	UpdateTimestamp(t time.Time)
	Filepath() string
}

Persistable must be implemented by any obj that requires to be saved in a file using FileUpdater

type PersistableHandlerState

type PersistableHandlerState struct {
	FileManager FileManager
}

PersistableHandlerState holds state information for a PersistableHandlerState

func (*PersistableHandlerState) GetObject

func (ph *PersistableHandlerState) GetObject(obj Persistable, c *mango.Context)

GetObject returns the object

func (*PersistableHandlerState) UpdateObject

func (ph *PersistableHandlerState) UpdateObject(obj Persistable, savedObj Updateable, c *mango.Context) error

UpdateObject updates the saved copy of the passed object

func (*PersistableHandlerState) UpdateObjectWithContextBind

func (ph *PersistableHandlerState) UpdateObjectWithContextBind(obj Persistable, savedObj Updateable, c *mango.Context)

UpdateObjectWithContextBind updates the saved copy of the passed object

type RunSpec

type RunSpec struct {
	Steps      int `json:"steps"`
	Iterations int `json:"iterations"`
}

RunSpec specifies the number of simulation steps to run, and the number of iterations that should be performed within each step

type SimHandler

type SimHandler interface {
	mango.Registerer
	Get(c *mango.Context)
	Put(c *mango.Context)
	GetStepsOrResults(c *mango.Context)
	GetResults(c *mango.Context)
	RunGenerateParseNetwork(c *mango.Context)
	PostRun(siminfo *SimInfo, c *mango.Context)
	GenerateNetwork(siminfo *SimInfo, c *mango.Context)
	DeleteStep(c *mango.Context)
}

SimHandler provides Read/Update methods for simulations on the simulation list

func NewSimHandler

func NewSimHandler(fm FileManager) SimHandler

NewSimHandler returns a new instance of SimHandler

type SimHandlerState

type SimHandlerState struct {
	ListHandlerState
	PersistableHandlerState
}

SimHandlerState holds state data for the SimHandler

func (*SimHandlerState) DeleteStep

func (sh *SimHandlerState) DeleteStep(c *mango.Context)

DeleteStep removes a simulation from the list of simulations

func (*SimHandlerState) EncodeStepList

func (sh *SimHandlerState) EncodeStepList(listHolder ListHolder, listname string) (interface{}, error)

func (*SimHandlerState) GenerateNetwork

func (sh *SimHandlerState) GenerateNetwork(siminfo *SimInfo, c *mango.Context)

GenerateNetwork generates a hierarchical network to be simulated.

func (*SimHandlerState) Get

func (sh *SimHandlerState) Get(c *mango.Context)

Get returns an existing simulation

func (*SimHandlerState) GetResults

func (sh *SimHandlerState) GetResults(c *mango.Context)

GetResults returns a concatenated set of results from all the steps in this simulation in JSON format

func (*SimHandlerState) GetResultsCsv

func (sh *SimHandlerState) GetResultsCsv(c *mango.Context)

GetResultsCsv returns a concatenated set of results from all the steps in this simulation in text/csv format

func (*SimHandlerState) GetStepsOrResults

func (sh *SimHandlerState) GetStepsOrResults(c *mango.Context)

GetStepsOrResults gets the list of steps in this simulation

func (*SimHandlerState) ParseNetwork

func (sh *SimHandlerState) ParseNetwork(siminfo *SimInfo, c *mango.Context)

ParseNetwork parses a network from a text file uploaded in the body of the post. The network is modified according to the options already stored in the simulation and then set as the starting point for the simulation. This will throw if a the simulation already has steps.

func (*SimHandlerState) PostRun

func (sh *SimHandlerState) PostRun(siminfo *SimInfo, c *mango.Context)

PostRun adds a new step to the list of simulations

func (*SimHandlerState) Put

func (sh *SimHandlerState) Put(c *mango.Context)

Put updates an existing simulation

func (*SimHandlerState) Register

func (sh *SimHandlerState) Register(r *mango.Router)

Register the routes for this routehandler

func (*SimHandlerState) RunGenerateParseNetwork

func (sh *SimHandlerState) RunGenerateParseNetwork(c *mango.Context)

RunGenerateParseNetwork handles three possible routes: /simulation/{id}/run Runs the simulation for the specified number of steps and iterations. /simulation/{id}/generate Generates a network to simulate, this will throw if the simulation already has steps. /simulation/{id}/parse Parses a network specified in a text file and sets it as the network to simulate. This will throw if the simulation already has steps.

type SimInfo

type SimInfo struct {
	TimestampHolder
	ID          string             `json:"id"`
	Name        string             `json:"name"`
	Description string             `json:"description"`
	Steps       []string           `json:"steps"`
	Options     sim.NetworkOptions `json:"options"`
}

SimInfo contains all relevant information about a simulation

func CreateSimInfo

func CreateSimInfo() *SimInfo

CreateSimInfo creates a new SimInfo object with a new ID

func NewSimInfo

func NewSimInfo(id string) *SimInfo

NewSimInfo returns a SimInfo object for the passed ID

func (*SimInfo) CopyValues

func (si *SimInfo) CopyValues(obj Persistable) error

CopyValues copies the values from the passed SimStep object to this object. Returns an error if the values could not be copied

func (*SimInfo) Filepath

func (si *SimInfo) Filepath() string

Filepath returns the Filepath used by this item

func (*SimInfo) GetItems

func (si *SimInfo) GetItems(listname string) []string

GetItems returns the items in the specified list

func (*SimInfo) RelPath

func (si *SimInfo) RelPath() string

RelPath returns the relative API path for this item

func (*SimInfo) UpdateItems

func (si *SimInfo) UpdateItems(listname string, items []string)

UpdateItems updates the items in the specified list

type SimList

type SimList struct {
	TimestampHolder
	Items []string `json:"simulations"`
	Notes string   `json:"notes"`
}

SimList is the list of simulations in the root directory

func NewSimList

func NewSimList() *SimList

NewSimList returns a SimList object that will be persisted

func (*SimList) CopyValues

func (sl *SimList) CopyValues(obj Persistable) error

CopyValues copies the values from the passed SimStep object to this object. Returns an error if the values could not be copied

func (*SimList) Filepath

func (sl *SimList) Filepath() string

Filepath returns the Filepath used by this item

func (*SimList) GetItems

func (sl *SimList) GetItems(listname string) []string

GetItems returns the items in the specified list

func (*SimList) UpdateItems

func (sl *SimList) UpdateItems(listname string, items []string)

UpdateItems updates the items in the specified list

type SimListExt

type SimListExt struct {
	TimestampHolder
	Items []*SimInfo `json:"simulations"`
	Notes string     `json:"notes"`
}

SimListExt is the struct that embeds SimInfo objects into the SimList

type SimListHandler

type SimListHandler interface {
	mango.Registerer
	GetSimulations(c *mango.Context)
	AddSimulation(c *mango.Context)
	UpdateNotes(c *mango.Context)
	DeleteSimulation(c *mango.Context)
}

SimListHandler provides Create/Delete methods for simulations on the simulation list

func NewSimListHandler

func NewSimListHandler(fm FileManager) SimListHandler

NewSimListHandler returns a new instance of SimListHandler

type SimListHandlerState

type SimListHandlerState struct {
	ListHandlerState
	PersistableHandlerState
}

SimListHandlerState holds state data for the SimListHandler

func (*SimListHandlerState) AddSimulation

func (sh *SimListHandlerState) AddSimulation(c *mango.Context)

AddSimulation adds a new simulation to the list of simulations

func (*SimListHandlerState) DeleteSimulation

func (sh *SimListHandlerState) DeleteSimulation(c *mango.Context)

DeleteSimulation removes a simulation from the list of simulations

func (*SimListHandlerState) EncodeFunc

func (sh *SimListHandlerState) EncodeFunc(listHolder ListHolder, listname string) (interface{}, error)

func (*SimListHandlerState) GetSimulations

func (sh *SimListHandlerState) GetSimulations(c *mango.Context)

GetSimulations gets the list of simulations

func (*SimListHandlerState) Register

func (sh *SimListHandlerState) Register(r *mango.Router)

Register the routes for this routehandler

func (*SimListHandlerState) UpdateNotes

func (sh *SimListHandlerState) UpdateNotes(c *mango.Context)

UpdateNotes updates the notes on the sim list

type SimStep

type SimStep struct {
	TimestampHolder
	Network  sim.RelationshipMgr `json:"network"`
	Results  sim.Results         `json:"results"`
	ID       string              `json:"id"`
	ParentID string              `json:"parent"`
}

SimStep holds the results of each simulation step

func CreateSimStep

func CreateSimStep(parentID string) *SimStep

CreateSimStep creates a new SimStep object with a new ID

func NewSimStep

func NewSimStep(id string, parentID string) *SimStep

NewSimStep returns a SimStep object for the passed ID that will be persisted in directory root

func NewSimStepFromRelPath

func NewSimStepFromRelPath(relPath string) *SimStep

NewSimStepFromRelPath returns a SimStep object extracting IDs from the relative path in the passed string

func (*SimStep) CopyValues

func (ss *SimStep) CopyValues(obj Persistable) error

CopyValues copies the values from the passed SimStep object to this object. Returns an error if the values could not be copied

func (*SimStep) Filepath

func (ss *SimStep) Filepath() string

Filepath returns the Filepath used by this item

func (*SimStep) RelPath

func (ss *SimStep) RelPath() string

RelPath returns the relative API path for this item

func (*SimStep) UnmarshalJSON

func (ss *SimStep) UnmarshalJSON(b []byte) error

UnmarshalJSON implements unmarshaling to make sure network is properly unmarshalled into sim.Network

type StaticRouter

type StaticRouter struct {
	Router *mango.Router
	// contains filtered or unexported fields
}

StaticRouter is a derived version of mango.Router that also serves an embedded copy of the orgnetsim UI

func (*StaticRouter) ServeHTTP

func (sr *StaticRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP Decides based on the route path whether to use the API router or an embedded file server that serves the static files of the embedded website

type StepHandler

type StepHandler interface {
	mango.Registerer
	Get(c *mango.Context)
	Put(c *mango.Context)
}

StepHandler provides Read/Update methods for steps of a simulation

func NewStepHandler

func NewStepHandler(fm FileManager) StepHandler

NewStepHandler returns a new instance of StepHandler

type StepHandlerState

type StepHandlerState struct {
	PersistableHandlerState
}

StepHandlerState holds state data for the StepHandler

func (*StepHandlerState) Get

func (sh *StepHandlerState) Get(c *mango.Context)

Get returns an existing step within a simulation

func (*StepHandlerState) Put

func (sh *StepHandlerState) Put(c *mango.Context)

Put updates an existing step within a simulation

func (*StepHandlerState) Register

func (sh *StepHandlerState) Register(r *mango.Router)

Register the routes for this routehandler

type TimestampHolder

type TimestampHolder struct {
	Stamp time.Time `json:"-"`
}

TimestampHolder holds the current timestamp on a persistable object

func (*TimestampHolder) Timestamp

func (ts *TimestampHolder) Timestamp() time.Time

Timestamp of the persistable object

func (*TimestampHolder) UpdateTimestamp

func (ts *TimestampHolder) UpdateTimestamp(t time.Time)

UpdateTimestamp of the persistable object

type Updateable

type Updateable interface {
	Persistable
	CopyValues(objToCopy Persistable) error
}

Updateable must be implemented by any persistable object that can be updated

type UpdaterRepo

type UpdaterRepo struct {
	Rootpath string
	Repo     map[string]FileUpdater
	Lock     sync.Mutex
	DirLock  sync.RWMutex
}

UpdaterRepo contains all FileUpdaters in use in this instance

func (*UpdaterRepo) Get

func (ur *UpdaterRepo) Get(path string) FileUpdater

Get a FileUpdater for the specifed path

Jump to

Keyboard shortcuts

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