api

package
v0.0.0-...-6b15219 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package api is a package that can create and cleanup api server binaries. Its cousin is the build/static package, which contains considerably more logic.

The point of this package is primarily to create an `api/main.go` output in the given directory that statically links the games and storage type configured via config.json, and then build that binary using `go build`.

The directory parameter gives the build directory; the build command will create an `api` sub-folder within that, and static.Build() will create a static directory. A directory of "" is legal and is effectively ".".

There's nothing magic about this package; it's legal to create your own server binary by hand. This package just automates that for you so when you add a game to your server you only have to worry about adding it in your config.json and everything else happens automatically.

For a config json that has a defaultstoragetype of bolt and lists the games `github.com/jkomoros/boardgame/examples/checkers`, `github.com/jkomoros/boardgame/examples/memory`, and `github.com/jkomoros/boardgame/examples/pig` it would output (with the package doc comment omitted):

package main

import (
    "github.com/jkomoros/boardgame/examples/checkers"
    "github.com/jkomoros/boardgame/examples/memory"
    "github.com/jkomoros/boardgame/examples/pig"
    "github.com/jkomoros/boardgame/server/api"
    "github.com/jkomoros/boardgame/storage/bolt"
)

func main() {

    storage := api.NewServerStorageManager(bolt.NewStorageManager(".database"))
    defer storage.Close()
    api.NewServer(storage,
        checkers.NewDelegate(),
        memory.NewDelegate(),
        pig.NewDelegate(),
    ).Start()
}

Typically it is not used directly, but via the `boardgame-util build api `,`boardgame-util cleanup api`, and `boardgame-util serve` commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(directory string, pkgs []*gamepkg.Pkg, storage StorageType, options *Options) (string, error)

Build is the primary method in this package. It generates the code for a server with the following imported games and given storage type in a folder called api/ within the given directory, builds it, and returns the path to the compiled binary. The bulk of the logic to generate the code is in Code().

To clean up the binary, call Cleanup and pass the same directory.

func Clean

func Clean(directory string) error

Clean removes the api/ directory (code and binary) that was generated within directory by Build.

func Code

func Code(pkgs []*gamepkg.Pkg, storage StorageType, options *Options) ([]byte, error)

Code returns the code for the `api/main.go`of a server with the given type. Options may be nil for default options.

func ValidStorageTypeStrings

func ValidStorageTypeStrings() []string

ValidStorageTypeStrings returns an array of strings that are the normal (i.e. not invalid) strings that would return useful values if passed to StorageTypeFromString.

Types

type Options

type Options struct {
	//If true, installs an overrider in the generated binary that enables
	//offline dev mode.
	OverrideOfflineDevMode bool

	//Will be passed to the storage's Constructor() method as the
	//optionalLiteralArgs.
	StorageLiteralArgs string
}

Options is a struct to pass extra options to Code() and Build(). The defaults are all the zero values.

type StorageType

type StorageType int

StorageType denotes one of the storage managers this package knows how to generate code for.

const (
	//StorageInvalid is the invalid default
	StorageInvalid StorageType = iota
	//StorageMemory denotes the memory storage layer
	StorageMemory
	//StorageBolt denotes the bolt storage layer
	StorageBolt
	//StorageMysql denotes the mysql storage layer
	StorageMysql
	//StorageFilesystem denotes the filesystem storage layer
	StorageFilesystem
)

func StorageTypeFromString

func StorageTypeFromString(in string) StorageType

StorageTypeFromString returns the right storage type for the given string. "" returns StorageDefault, and any unknown types return StorageInvalid.

func (StorageType) Constructor

func (s StorageType) Constructor(optionalLiteralArgs string) string

Constructor is a string representing a default constructor for this storage type, e.g. `bolt.NewStorageManager(".database")`. optionalLiteralArgs will be passed literally within the `()` of the storage constructor, so valid strings are "\".database\"" etc. If not provided, will fall back on reasonable defaults for that type.

func (StorageType) Import

func (s StorageType) Import() string

Import is the string denting the import path for this storage type, e.g. "github.com/jkomoros/boardgame/storage/mysql"

func (StorageType) String

func (s StorageType) String() string

Jump to

Keyboard shortcuts

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