server

package
v0.0.0-...-0ae050f Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2016 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = cli.Command{
	Category: "Server",
	Name:     "server",
	Usage:    "Run the Inki server daemon",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:   "config, c",
			Usage:  "The configuration file to be used by this daemon",
			EnvVar: "INKI_CONFIG",
			Value:  "/etc/inki/server.yml",
		},
		cli.IntFlag{
			Name:   "port, P",
			Usage:  "The port on which the Inki server should listen",
			EnvVar: "PORT",
			Value:  3000,
		},
	},
	Before: func(c *cli.Context) error {
		if c.IsSet("config") {
			err := LoadConfig(c.String("config"))
			if err != nil {
				log.WithError(err).Errorf("Failed to read configuration file '%s'", c.String("config, c"))
				return err
			}
		} else {
			log.Warn("No configuration file provided, using empty defaults")
		}

		return nil
	},
	Action: func(c *cli.Context) error {
		port := c.Int("port")
		log.WithField("port", port).Info("Starting server")

		mux := http.NewServeMux()
		mux.Handle("/api/", http.StripPrefix("/api", Router()))
		mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(404)
			w.Write([]byte(`{"code": 404, "error": "Not Found", "message": "The method you attempted to make use of could not be found on our system."}`))
		})

		return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), cors.New(cors.Options{
			AllowCredentials: true,
			AllowedOrigins:   []string{"*"},
			AllowedHeaders:   []string{"Authorization", "Content-Type"},
			AllowedMethods:   []string{"GET", "POST", "PUT", "DELETE"},
			Debug:            false,
		}).Handler(mux))
	},
}

Functions

func AddKey

func AddKey(key *crypto.Key)

func GetAllKeys

func GetAllKeys() []crypto.Key

func GetKeyBy

func GetKeyBy(pred KeyPredicate) *crypto.Key

func GetKeysBy

func GetKeysBy(pred KeyPredicate) []crypto.Key

func HasKey

func HasKey(key *crypto.Key) bool

func LoadConfig

func LoadConfig(file string) error

func RemoveKey

func RemoveKey(key *crypto.Key)

func RemoveKeyBy

func RemoveKeyBy(pred KeyPredicate)

func Router

func Router() *mux.Router

Router returns the registered router for the API

Types

type Config

type Config struct {
	Port  int          `yaml:"port"`
	Users []ConfigUser `yaml:"users"`
}

func GetConfig

func GetConfig() *Config

func (*Config) GetUser

func (c *Config) GetUser(name string) *ConfigUser

type ConfigUser

type ConfigUser struct {
	Name    string `yaml:"name"`
	KeyRing string `yaml:"keyring"`
}

func (*ConfigUser) GetKeyRing

func (u *ConfigUser) GetKeyRing() (openpgp.KeyRing, error)

type KeyPredicate

type KeyPredicate func(k *crypto.Key) bool

func FingerprintEquals

func FingerprintEquals(fingerprint string) KeyPredicate

func KeyEquals

func KeyEquals(key *crypto.Key) KeyPredicate

func KeyExpired

func KeyExpired() KeyPredicate

func KeyValid

func KeyValid() KeyPredicate

func UserEquals

func UserEquals(user string) KeyPredicate

func (KeyPredicate) And

func (KeyPredicate) Or

type StringFormatter

type StringFormatter struct{}

func (*StringFormatter) Write

func (f *StringFormatter) Write(data interface{}, into io.Writer) error

Jump to

Keyboard shortcuts

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