<commit_before>/*
Package safego wraps "go" call with additional recover feature to keep
your goroutine away from panic.

example:

origin code:

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		go func() {
			panic("OMG!")
		}()
	})
	http.ListenAndServe(":8080", nil)

even the panic occur inside a new goroutine, it will bring the whole server down!

with safego, you can replace with:

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		sofego.Go(func() {
			panic("OMG!")
		})
	})
	http.ListenAndServe(":8080", nil)

 */
// Author: ZHU HAIHUA
// Since: 2016-03-22 15:57
package safego

import (
	"fmt"
	"github.com/kimiazhu/golib/stack"
	"os"
)

type Handler func(err interface{})

var DefaultHandler = func(err interface{}) {
	fmt.Fprintf(os.Stderr, "recovered: %s\n%s", err, stack.CallStack(3))
}

// Go run the f with a goroutine and keep it away from panic.
// it will use DefaultHandler if argument handler is nil
func Go(f func(), handler... Handler) {
	handle := DefaultHandler
	switch len(handler) {
	case 1:
		handle = handler[0]
	}

	go func() {
		defer func() {
			if r := recover(); r != nil {
				handle(r)
			}
		}()

		f()
	}()
}<commit_msg>safego support function call with args<commit_after>/*
Package safego wraps "go" call with additional recover feature to keep
your goroutine away from panic.

example:

origin code:

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		go func() {
			panic("OMG!")
		}()
	})
	http.ListenAndServe(":8080", nil)

even the panic occur inside a new goroutine, it will bring the whole server down!

with safego, you can replace with:

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		sofego.Go(func() {
			panic("OMG!")
		})
	})
	http.ListenAndServe(":8080", nil)

 */
// Author: ZHU HAIHUA
// Since: 2016-03-22 15:57
package safego

import (
	"fmt"
	"github.com/kimiazhu/golib/stack"
	"os"
)

type Handler func(err interface{})

var DefaultHandler = func(err interface{}) {
	fmt.Fprintf(os.Stderr, "recovered: %s\n%s", err, stack.CallStack(3))
}

// Go run the f with a goroutine and keep it away from panic.
// it will use DefaultHandler if argument handler is nil
func Go(f func(), handler... Handler) {
	handle := DefaultHandler
	switch len(handler) {
	case 1:
		handle = handler[0]
	}

	go func() {
		defer func() {
			if r := recover(); r != nil {
				handle(r)
			}
		}()

		f()
	}()
}

// GoWithHandler will run function f(args... interface{}) in a go routines.
// And handler the panic if occur with given handler.
func GoWithHandler(f func(args... interface{}), handler Handler, args... interface{}) {
	go func() {
		defer func() {
			if r := recover(); r != nil {
				handler(r)
			}
		}()

		f(args...)
	}()
}<|endoftext|>


<commit_before>package convolution

import (
	"errors"
	"math"
)

type Kernel interface {
	Weights() []float64
	Normalized() ([]float64, float64)
}

type kernel []float64

func (k kernel) Weights() []float64 {
	return k
}

func NewKernel(data []float64) (k Kernel, err error) {
	size := int(math.Sqrt(float64(len(data))))
	if size%2 == 0 || size*size != len(data) {
		err = errors.New("Kernel has to be an odd square")
		return
	}

	k = kernel(data)

	return
}

func (k kernel) Normalized() ([]float64, float64) {
	return NormalizeData(k)
}

func NormalizeData(data []float64) ([]float64, float64) {
	var sum float64
	for _, d := range data {
		sum += d
	}

	var div, offset float64
	if sum > 0 {
		div = sum
		offset = 0
	} else if sum < 0 {
		div = -sum
		offset = 1
	} else {
		div = 1
		offset = 0.5
	}

	if div != 1 {
		for i := range data {
			data[i] = data[i] / div
		}

	}

	return data, offset
}
<commit_msg>Add a hv kernel<commit_after>package convolution

import (
	"errors"
	"math"
)

type Kernel interface {
	Weights() []float64
	Normalized() ([]float64, float64)
}

type HVKernel interface {
	HWeights() []float64
	VWeights() []float64
}

type kernel []float64
type hvkernel struct {
	h []float64
	v []float64
}

func NewKernel(data []float64) (k Kernel, err error) {
	size := int(math.Sqrt(float64(len(data))))
	if size%2 == 0 || size*size != len(data) {
		err = errors.New("Kernel has to be an odd square")
		return
	}

	k = kernel(data)

	return
}

func (k kernel) Weights() []float64 {
	return k
}

func (k kernel) Normalized() ([]float64, float64) {
	return NormalizeData(k)
}

func NewHVKernel(h, v []float64) (k HVKernel, err error) {
	if len(h)%2 == 0 {
		err = errors.New("Horizontal kernel has to be odd")
		return
	}
	if len(v)%2 == 0 {
		err = errors.New("Vertical kernel has to be odd")
		return
	}
	k = hvkernel{h, v}

	return
}

func (k hvkernel) HWeights() []float64 {
	return k.h
}

func (k hvkernel) VWeights() []float64 {
	return k.v
}

func NormalizeData(data []float64) ([]float64, float64) {
	var sum float64
	for _, d := range data {
		sum += d
	}

	var div, offset float64
	if sum > 0 {
		div = sum
		offset = 0
	} else if sum < 0 {
		div = -sum
		offset = 1
	} else {
		div = 1
		offset = 0.5
	}

	if div != 1 {
		for i := range data {
			data[i] = data[i] / div
		}

	}

	return data, offset
}
<|endoftext|>


<commit_before>package server

import (
	"errors"
	"fmt"
	"io"
	"log"
	"net"
	"strings"
	"sync"

	"github.com/Akagi201/redface/resp"
)

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(args []string) (interface{}, error)

// Server A Server defines parameters for running an Redis API Server.
// The zero value for Server is a valid configuration.
type Server struct {
	// "unix" for unix socket, "tcp" for tcp
	Proto string
	// TCP address to listen on, ":6379" if empty
	Addr string

	mu sync.RWMutex

	// map cmd: HandlerFunc
	m map[string]HandlerFunc
}

// NewServer creates a new redis server
func NewServer(proto string, host string, port int) (*Server, error) {
	srv := &Server{
		Proto: proto,
		m:     make(map[string]HandlerFunc),
	}

	if srv.Proto == "unix" {
		srv.Addr = host
	} else {
		srv.Addr = fmt.Sprintf("%s:%d", host, port)
	}

	return srv, nil
}

// NewTCPServer creates a new tcp redis server
func NewTCPServer(host string, port int) (*Server, error) {
	return NewServer("tcp", host, port)
}

// NewUnixServer creates a new unix sockets redis server
func NewUnixServer(host string, port int) (*Server, error) {
	return NewServer("unix", host, port)
}

// ListenAndServe listens on the TCP or Unix socket network address srv.Addr and then
// calls Serve to handle requests on incoming connections.
// If srv.Addr is blank, ":6379" is used.
// ListenAndServe always returns a non-nil error.
func (srv *Server) ListenAndServe() error {
	addr := srv.Addr
	if srv.Proto == "" {
		srv.Proto = "tcp"
	}
	if srv.Proto == "unix" && addr == "" {
		addr = "/tmp/redis.sock"
	} else if addr == "" {
		addr = ":6379"
	}
	ln, err := net.Listen(srv.Proto, addr)
	if err != nil {
		return err
	}
	return srv.Serve(ln)
}

// Serve accepts incoming connections on the Listener l, creating a new service goroutine for each.
// The service goroutines read requests and then call srv.Handler to reply to them.
//
// Serve always returns a non-nil error.
func (srv *Server) Serve(l net.Listener) error {
	defer l.Close()

	for {
		rw, err := l.Accept()
		if err != nil {
			return err
		}
		go srv.doServe(rw)
	}
}

var invalidCmdResp = resp.NewResp(errors.New("ERR invalid command"))

// doServe starts a new redis session, using `conn` as a transport.
// It reads commands using the redis protocol, passes them to `handler`,
// and returns the result.
func (srv *Server) doServe(conn net.Conn) {
	defer func() {
		if err := recover(); err != nil {
			log.Printf("redis: panic doServe %v", err)
		}
		conn.Close()
	}()

	rr := resp.NewRespReader(conn)

outer:
	for {
		var cmd string
		var args []string

		m := rr.Read()
		if m.IsType(resp.IOErr) {
			log.Printf("Client connection error %q", m.Err)
			conn.Close()
			return
		}

		parts, err := m.Array()
		if err != nil {
			log.Printf("Error parsing to array: %q", err)
			continue outer
		}

		for i := range parts {
			val, err := parts[i].Str()
			if err != nil {
				log.Printf("Invalid command part %#v: %s", parts[i], err)
				invalidCmdResp.WriteTo(conn)
				continue outer
			}
			if i == 0 {
				cmd = val
			} else {
				args = append(args, val)
			}
		}

		log.Printf("redis: doServe cmd: %s, args: %#v", cmd, args)
		srv.Dispatch(conn, cmd, args)
	}
}

// Dispatch takes in a client whose command has already been read off the
// socket, a list of arguments from that command (not including the command name
// itself), and handles that command
func (srv *Server) Dispatch(conn net.Conn, cmd string, args []string) {
	handlerFunc, ok := srv.m[strings.ToUpper(cmd)]
	if !ok {
		writeErrf(conn, "ERR unknown command %q", cmd)
		return
	}

	ret, err := handlerFunc(args)
	if err != nil {
		writeErrf(conn, "ERR unexpected server-side error")
		log.Printf("command %s %#v err: %s", cmd, args, err)
		return
	}

	resp.NewResp(ret).WriteTo(conn)
}

// Handle registers the handler for the given cmd.
// If a handler already exists for pattern, Handle panics.
func (srv *Server) Handle(cmd string, handlerFunc HandlerFunc) {
	srv.mu.Lock()
	defer srv.mu.Unlock()

	if cmd == "" {
		panic("redis: invalid cmd " + cmd)
	}

	if srv.m == nil {
		srv.m = make(map[string]HandlerFunc)
	}

	srv.m[strings.ToUpper(cmd)] = handlerFunc
}

func writeErrf(w io.Writer, format string, args ...interface{}) {
	err := fmt.Errorf(format, args...)
	resp.NewResp(err).WriteTo(w)
}
<commit_msg>update doc<commit_after>// Package server implements a redis server
package server

import (
	"errors"
	"fmt"
	"io"
	"log"
	"net"
	"strings"
	"sync"

	"github.com/Akagi201/redface/resp"
)

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(args []string) (interface{}, error)

// Server A Server defines parameters for running an Redis API Server.
// The zero value for Server is a valid configuration.
type Server struct {
	// "unix" for unix socket, "tcp" for tcp
	Proto string
	// TCP address to listen on, ":6379" if empty
	Addr string

	mu sync.RWMutex

	// map cmd: HandlerFunc
	m map[string]HandlerFunc
}

// NewServer creates a new redis server
func NewServer(proto string, host string, port int) (*Server, error) {
	srv := &Server{
		Proto: proto,
		m:     make(map[string]HandlerFunc),
	}

	if srv.Proto == "unix" {
		srv.Addr = host
	} else {
		srv.Addr = fmt.Sprintf("%s:%d", host, port)
	}

	return srv, nil
}

// NewTCPServer creates a new tcp redis server
func NewTCPServer(host string, port int) (*Server, error) {
	return NewServer("tcp", host, port)
}

// NewUnixServer creates a new unix sockets redis server
func NewUnixServer(host string, port int) (*Server, error) {
	return NewServer("unix", host, port)
}

// ListenAndServe listens on the TCP or Unix socket network address srv.Addr and then
// calls Serve to handle requests on incoming connections.
// If srv.Addr is blank, ":6379" is used.
// ListenAndServe always returns a non-nil error.
func (srv *Server) ListenAndServe() error {
	addr := srv.Addr
	if srv.Proto == "" {
		srv.Proto = "tcp"
	}
	if srv.Proto == "unix" && addr == "" {
		addr = "/tmp/redis.sock"
	} else if addr == "" {
		addr = ":6379"
	}
	ln, err := net.Listen(srv.Proto, addr)
	if err != nil {
		return err
	}
	return srv.Serve(ln)
}

// Serve accepts incoming connections on the Listener l, creating a new service goroutine for each.
// The service goroutines read requests and then call srv.Handler to reply to them.
//
// Serve always returns a non-nil error.
func (srv *Server) Serve(l net.Listener) error {
	defer l.Close()

	for {
		rw, err := l.Accept()
		if err != nil {
			return err
		}
		go srv.doServe(rw)
	}
}

var invalidCmdResp = resp.NewResp(errors.New("ERR invalid command"))

// doServe starts a new redis session, using `conn` as a transport.
// It reads commands using the redis protocol, passes them to `handler`,
// and returns the result.
func (srv *Server) doServe(conn net.Conn) {
	defer func() {
		if err := recover(); err != nil {
			log.Printf("redis: panic doServe %v", err)
		}
		conn.Close()
	}()

	rr := resp.NewRespReader(conn)

outer:
	for {
		var cmd string
		var args []string

		m := rr.Read()
		if m.IsType(resp.IOErr) {
			log.Printf("Client connection error %q", m.Err)
			conn.Close()
			return
		}

		parts, err := m.Array()
		if err != nil {
			log.Printf("Error parsing to array: %q", err)
			continue outer
		}

		for i := range parts {
			val, err := parts[i].Str()
			if err != nil {
				log.Printf("Invalid command part %#v: %s", parts[i], err)
				invalidCmdResp.WriteTo(conn)
				continue outer
			}
			if i == 0 {
				cmd = val
			} else {
				args = append(args, val)
			}
		}

		log.Printf("redis: doServe cmd: %s, args: %#v", cmd, args)
		srv.Dispatch(conn, cmd, args)
	}
}

// Dispatch takes in a client whose command has already been read off the
// socket, a list of arguments from that command (not including the command name
// itself), and handles that command
func (srv *Server) Dispatch(conn net.Conn, cmd string, args []string) {
	handlerFunc, ok := srv.m[strings.ToUpper(cmd)]
	if !ok {
		writeErrf(conn, "ERR unknown command %q", cmd)
		return
	}

	ret, err := handlerFunc(args)
	if err != nil {
		writeErrf(conn, "ERR unexpected server-side error")
		log.Printf("command %s %#v err: %s", cmd, args, err)
		return
	}

	resp.NewResp(ret).WriteTo(conn)
}

// Handle registers the handler for the given cmd.
// If a handler already exists for pattern, Handle panics.
func (srv *Server) Handle(cmd string, handlerFunc HandlerFunc) {
	srv.mu.Lock()
	defer srv.mu.Unlock()

	if cmd == "" {
		panic("redis: invalid cmd " + cmd)
	}

	if srv.m == nil {
		srv.m = make(map[string]HandlerFunc)
	}

	srv.m[strings.ToUpper(cmd)] = handlerFunc
}

func writeErrf(w io.Writer, format string, args ...interface{}) {
	err := fmt.Errorf(format, args...)
	resp.NewResp(err).WriteTo(w)
}
<|endoftext|>


<commit_before>package server

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"sync"
	"syscall"

	"github.com/carbocation/interpose/middleware"
	"github.com/dchest/blake2b"
	"github.com/gorilla/mux"
	"github.com/rs/cors"
	"github.com/tsileo/blobstash/api"
	"github.com/tsileo/blobstash/backend"
	"github.com/tsileo/blobstash/config"
	"github.com/tsileo/blobstash/config/pathutil"
	"github.com/tsileo/blobstash/embed"
	"github.com/tsileo/blobstash/ext/docstore"
	"github.com/tsileo/blobstash/ext/lua"
	"github.com/tsileo/blobstash/httputil"
	"github.com/tsileo/blobstash/logger"
	"github.com/tsileo/blobstash/meta"
	serverMiddleware "github.com/tsileo/blobstash/middleware"
	"github.com/tsileo/blobstash/nsdb"
	"github.com/tsileo/blobstash/router"
	"github.com/tsileo/blobstash/synctable"
	"github.com/tsileo/blobstash/vkv"
	"github.com/tsileo/blobstash/vkv/hub"
	"github.com/unrolled/secure"
	"golang.org/x/net/http2"
	log2 "gopkg.in/inconshreveable/log15.v2"
)

var Version = "0.0.0"

var DefaultConf = map[string]interface{}{
	"backends": map[string]interface{}{
		"blobs": map[string]interface{}{
			"backend-type": "blobsfile",
			"backend-args": map[string]interface{}{
				"path": "$VAR/blobs",
			},
		},
	},
	"router":    []interface{}{[]interface{}{"default", "blobs"}},
	"data_path": pathutil.VarDir(),
}

// FIXME(ts) create ext interface with io.Closer and store them in a map as server attribute.

type Server struct {
	Log         log2.Logger
	Router      *router.Router
	Backends    map[string]backend.BlobHandler
	DB          *vkv.DB
	NsDB        *nsdb.DB
	metaHandler *meta.MetaHandler

	syncer *synctable.SyncTable

	KvUpdate chan *vkv.KeyValue
	blobs    chan *router.Blob

	conf     map[string]interface{}
	resync   bool
	ready    chan struct{}
	shutdown chan struct{}
	stop     chan struct{}
	wg       sync.WaitGroup
	watchHub *hub.Hub

	port      int
	tlsConfig *TLSConfig
}

type TLSConfig struct {
	CertPath string
	KeyPath  string
	Hostname string
}

func New(conf map[string]interface{}) *Server {
	// TODO(tsileo): the conf as a struct instead of map?
	if conf == nil {
		logger.Log.Debug("No config provided, using `DefaultConf`")
		conf = DefaultConf
	}
	vardir := pathutil.VarDir()
	// if dpath, ok := conf["data_path"].(string); ok {
	// 	vardir = dpath
	// }
	os.MkdirAll(vardir, 0700)
	db, err := vkv.New(filepath.Join(vardir, "vkv.db"))
	if err != nil {
		panic(err)
	}
	nsdb, err := nsdb.New(filepath.Join(vardir, "ns.db"))
	if err != nil {
		panic(err)
	}
	server := &Server{
		Router:    router.New(conf["router"].([]interface{})),
		Backends:  map[string]backend.BlobHandler{},
		DB:        db,
		NsDB:      nsdb,
		KvUpdate:  make(chan *vkv.KeyValue),
		ready:     make(chan struct{}, 1),
		shutdown:  make(chan struct{}, 1),
		stop:      make(chan struct{}),
		blobs:     make(chan *router.Blob),
		resync:    conf["resync"].(bool),
		port:      conf["port"].(int),
		Log:       logger.Log,
		watchHub:  hub.NewHub(),
		conf:      conf,
		tlsConfig: &TLSConfig{},
	}
	backends := conf["backends"].(map[string]interface{})
	for _, b := range server.Router.ResolveBackends() {
		server.Backends[b] = config.NewFromConfig(backends[b].(map[string]interface{}))
		server.Router.Backends[b] = server.Backends[b]
	}
	server.metaHandler = meta.New(server.Router, server.DB, server.NsDB)
	return server
}

func (s *Server) processBlobs() {
	s.wg.Add(1)
	defer s.wg.Done()
	for {
		select {
		case blob := <-s.blobs:
			log.Printf("processBlobs: %+v", blob)
			backend := s.Router.Route(blob.Req)
			exists, err := backend.Exists(blob.Hash)
			if err != nil {
				panic(fmt.Errorf("processBlobs error: %v", err))
			}
			if !exists {
				if err := backend.Put(blob.Hash, blob.Blob); err != nil {
					panic(fmt.Errorf("processBlobs error: %v", err))
				}
			}
			if blob.Req.MetaBlob {
				if err := s.NsDB.ApplyMeta(blob.Hash); err != nil {
					panic(err)
				}
			}
			if blob.Req.Namespace != "" {
				nsBlobBody := meta.CreateNsBlob(blob.Hash, blob.Req.Namespace)
				nsHash := fmt.Sprintf("%x", blake2b.Sum256(nsBlobBody))
				s.blobs <- &router.Blob{
					Blob: nsBlobBody,
					Hash: nsHash,
					Req: &router.Request{
						Type:   router.Write,
						NsBlob: true,
					},
				}
				if err := s.NsDB.AddNs(blob.Hash, blob.Req.Namespace); err != nil {
					panic(err)
				}
			}
		case <-s.stop:
			return
		}
	}
}

// TillReady blocks until all blobs get scanned (and all meta blobs applied if needed)
func (s *Server) TillReady() {
	<-s.ready
}

// SetUp should be called instead of Run in embedded mode
func (s *Server) SetUp() {
	// Start meta handler: watch for kv update and create meta blob
	go s.metaHandler.WatchKvUpdate(s.wg, s.blobs, s.KvUpdate, s.watchHub)
	// Scan existing meta blob
	if s.resync {
		go func() {
			s.wg.Add(1)
			defer s.wg.Done()
			if err := s.metaHandler.Scan(); err != nil {
				panic(err)
			}
			s.ready <- struct{}{}
		}()
	} else {
		s.ready <- struct{}{}
	}
	// Start the worker for handling blob upload
	for i := 0; i < 25; i++ {
		go s.processBlobs()
	}
}

// KvStore return a kv client, written for embedded client
func (s *Server) KvStore() *embed.KvStore {
	return embed.NewKvStore(s.DB, s.KvUpdate, s.Router)
}

// BlobStore returns a blob store clienm for embedded usge
func (s *Server) BlobStore() *embed.BlobStore {
	return embed.NewBlobStore(s.blobs, s.Router)
}

// Run runs the server and block until the server is shutdown
func (s *Server) Run() {
	hawkKey, err := LoadAPIKey("hawk.key")
	if err != nil {
		panic(err)
	}
	// XXX(tsileo): Maybe handle a master key with multiple key? so key can be revoked?
	apiKey, err := LoadAPIKey("api.key")
	if err != nil {
		panic(err)
	}
	authMiddleware := middleware.BasicAuth("", apiKey)
	middlewares := &serverMiddleware.SharedMiddleware{
		Auth: authMiddleware,
	}
	authFunc := httputil.BasicAuthFunc("", apiKey)
	// Start the HTTP API
	s.SetUp()
	r := mux.NewRouter()
	// publicRoute := r.PathPrefix("/public").Subrouter()
	appRoute := r.PathPrefix("/app").Subrouter()
	ekvstore := s.KvStore()
	eblobstore := s.BlobStore()
	docstore.New(s.Log.New("ext", "docstore"), ekvstore, eblobstore).RegisterRoute(r.PathPrefix("/api/ext/docstore/v1").Subrouter(), middlewares)
	luaExt := lua.New(s.Log.New("ext", "lua"), []byte(hawkKey), authFunc, ekvstore, eblobstore)
	luaExt.RegisterRoute(r.PathPrefix("/api/ext/lua/v1").Subrouter(), middlewares)
	luaExt.RegisterAppRoute(appRoute, middlewares)
	api.New(r.PathPrefix("/api/v1").Subrouter(), middlewares, s.wg, s.DB, s.NsDB, s.KvUpdate, s.Router, s.blobs, s.watchHub)

	s.syncer = synctable.New(s.blobs, eblobstore, s.NsDB, s.Log.New("ext", "synctable"))
	s.syncer.RegisterRoute(r.PathPrefix("/api/sync/v1").Subrouter(), middlewares)

	// FIXME(tsileo): a way to make an app hook the index
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		index := `<!doctype html>

  <meta charset="utf-8">
  <title>BlobsStash</title>

This is a private BlobStash instance.

`
		w.Write([]byte(index))
	})
	r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	})
	r.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`User-agent: *
Disallow: /`))
	})

	// FIXME allowedorigins from config
	c := cors.New(cors.Options{
		AllowedOrigins: []string{"*"},
	})

	secureMiddleware := secure.New(secure.Options{
		FrameDeny:             true,
		ContentTypeNosniff:    true,
		BrowserXssFilter:      true,
		ContentSecurityPolicy: "default-src 'self'",
	})
	http.Handle("/", secureMiddleware.Handler(c.Handler(r)))
	s.Log.Info(fmt.Sprintf("server: HTTP API listening on 0.0.0.0:%d", s.port))
	runFunc := func() {
		if err := http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil); err != nil {
			panic(err)
		}
	}

	if tlsHost, ok := s.conf["tls-hostname"]; ok {
		s.tlsConfig.Hostname = tlsHost.(string)
		s.tlsConfig.CertPath = s.conf["tls-crt-path"].(string)
		s.tlsConfig.KeyPath = s.conf["tls-key-path"].(string)
		runFunc = func() {

			srv := &http.Server{
				Addr:    fmt.Sprintf(":%d", s.port),
				Handler: http.DefaultServeMux,
			}
			s.Log.Info(fmt.Sprintf("server: HTTPS API listening on 0.0.0.0:%d", s.port))
			http2.ConfigureServer(srv, &http2.Server{})
			if err := srv.ListenAndServeTLS(s.tlsConfig.CertPath, s.tlsConfig.KeyPath); err != nil {
				panic(err)
			}
		}
	}

	go runFunc()

	s.TillShutdown()
}

// TillShutdown blocks until a kill signal is catched.
func (s *Server) TillShutdown() {
	// Listen for shutdown signal
	cs := make(chan os.Signal, 1)
	signal.Notify(cs, os.Interrupt,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)
	for {
		select {
		case <-s.shutdown:
			break
		case sig := <-cs:
			s.Log.Debug("captured signal", "signal", sig)
			s.Log.Info("shutting down...")
			break
		}
		s.Close()
		os.Exit(0)
	}
}

func (s *Server) Close() {
	close(s.stop)
	s.wg.Wait()
	close(s.KvUpdate)
	s.DB.Close()
	s.NsDB.Close()
	for _, b := range s.Backends {
		b.Close()
	}
}

func LoadAPIKey(kfile string) (string, error) {
	keyPath := filepath.Join(pathutil.ConfigDir(), kfile)
	if _, err := os.Stat(keyPath); os.IsNotExist(err) {
		logger.Log.Debug("Generating new API key...")
		newKey, err := RandomKey()
		if err != nil {
			return "", err
		}
		if err := ioutil.WriteFile(keyPath, []byte(newKey), 0644); err != nil {
			return "", err
		}
		logger.Log.Debug(fmt.Sprintf("API key saved at %v", keyPath))
		return newKey, nil
	}
	logger.Log.Debug(fmt.Sprintf("Loading API key from %v", keyPath))
	data, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return "", err
	}
	return string(data), nil
}
func RandomKey() (string, error) {
	b := make([]byte, 32)
	if _, err := rand.Read(b); err != nil {
		return "", err
	}
	return base64.URLEncoding.EncodeToString(b), nil
}
<commit_msg>server: cleanup<commit_after>package server

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/signal"
	"path/filepath"
	"strconv"
	"sync"
	"syscall"

	"github.com/carbocation/interpose/middleware"
	"github.com/dchest/blake2b"
	"github.com/gorilla/mux"
	"github.com/rs/cors"
	"github.com/tsileo/blobstash/api"
	"github.com/tsileo/blobstash/backend"
	"github.com/tsileo/blobstash/config"
	"github.com/tsileo/blobstash/config/pathutil"
	"github.com/tsileo/blobstash/embed"
	"github.com/tsileo/blobstash/ext/docstore"
	"github.com/tsileo/blobstash/ext/lua"
	"github.com/tsileo/blobstash/httputil"
	"github.com/tsileo/blobstash/logger"
	"github.com/tsileo/blobstash/meta"
	serverMiddleware "github.com/tsileo/blobstash/middleware"
	"github.com/tsileo/blobstash/nsdb"
	"github.com/tsileo/blobstash/router"
	"github.com/tsileo/blobstash/synctable"
	"github.com/tsileo/blobstash/vkv"
	"github.com/tsileo/blobstash/vkv/hub"
	"github.com/unrolled/secure"
	"golang.org/x/net/http2"
	log2 "gopkg.in/inconshreveable/log15.v2"
)

var Version = "0.0.0"

var DefaultConf = map[string]interface{}{
	"backends": map[string]interface{}{
		"blobs": map[string]interface{}{
			"backend-type": "blobsfile",
			"backend-args": map[string]interface{}{
				"path": "$VAR/blobs",
			},
		},
	},
	"router":    []interface{}{[]interface{}{"default", "blobs"}},
	"data_path": pathutil.VarDir(),
}

// FIXME(ts) create ext interface with io.Closer and store them in a map as server attribute.

type Server struct {
	Log         log2.Logger
	Router      *router.Router
	Backends    map[string]backend.BlobHandler
	DB          *vkv.DB
	NsDB        *nsdb.DB
	metaHandler *meta.MetaHandler

	syncer *synctable.SyncTable

	KvUpdate chan *vkv.KeyValue
	blobs    chan *router.Blob

	conf     map[string]interface{}
	resync   bool
	ready    chan struct{}
	shutdown chan struct{}
	stop     chan struct{}
	wg       sync.WaitGroup
	watchHub *hub.Hub

	port      int
	tlsConfig *TLSConfig
}

type TLSConfig struct {
	CertPath string
	KeyPath  string
	Hostname string
}

func New(conf map[string]interface{}) *Server {
	// TODO(tsileo): the conf as a struct instead of map?
	if conf == nil {
		logger.Log.Debug("No config provided, using `DefaultConf`")
		conf = DefaultConf
	}
	vardir := pathutil.VarDir()
	// if dpath, ok := conf["data_path"].(string); ok {
	// 	vardir = dpath
	// }
	os.MkdirAll(vardir, 0700)
	db, err := vkv.New(filepath.Join(vardir, "vkv.db"))
	if err != nil {
		panic(err)
	}
	nsdb, err := nsdb.New(filepath.Join(vardir, "ns.db"))
	if err != nil {
		panic(err)
	}
	server := &Server{
		Router:    router.New(conf["router"].([]interface{})),
		Backends:  map[string]backend.BlobHandler{},
		DB:        db,
		NsDB:      nsdb,
		KvUpdate:  make(chan *vkv.KeyValue),
		ready:     make(chan struct{}, 1),
		shutdown:  make(chan struct{}, 1),
		stop:      make(chan struct{}),
		blobs:     make(chan *router.Blob),
		resync:    conf["resync"].(bool),
		port:      conf["port"].(int),
		Log:       logger.Log,
		watchHub:  hub.NewHub(),
		conf:      conf,
		tlsConfig: &TLSConfig{},
	}
	backends := conf["backends"].(map[string]interface{})
	for _, b := range server.Router.ResolveBackends() {
		server.Backends[b] = config.NewFromConfig(backends[b].(map[string]interface{}))
		server.Router.Backends[b] = server.Backends[b]
	}
	server.metaHandler = meta.New(server.Router, server.DB, server.NsDB)
	return server
}

func (s *Server) processBlobs() {
	s.wg.Add(1)
	defer s.wg.Done()
	for {
		select {
		case blob := <-s.blobs:
			log.Printf("processBlobs: %+v", blob)
			backend := s.Router.Route(blob.Req)
			exists, err := backend.Exists(blob.Hash)
			if err != nil {
				panic(fmt.Errorf("processBlobs error: %v", err))
			}
			if !exists {
				if err := backend.Put(blob.Hash, blob.Blob); err != nil {
					panic(fmt.Errorf("processBlobs error: %v", err))
				}
			}
			if blob.Req.MetaBlob {
				if err := s.NsDB.ApplyMeta(blob.Hash); err != nil {
					panic(err)
				}
			}
			if blob.Req.Namespace != "" {
				nsBlobBody := meta.CreateNsBlob(blob.Hash, blob.Req.Namespace)
				nsHash := fmt.Sprintf("%x", blake2b.Sum256(nsBlobBody))
				s.blobs <- &router.Blob{
					Blob: nsBlobBody,
					Hash: nsHash,
					Req: &router.Request{
						Type:   router.Write,
						NsBlob: true,
					},
				}
				if err := s.NsDB.AddNs(blob.Hash, blob.Req.Namespace); err != nil {
					panic(err)
				}
			}
		case <-s.stop:
			return
		}
	}
}

// TillReady blocks until all blobs get scanned (and all meta blobs applied if needed)
func (s *Server) TillReady() {
	<-s.ready
}

// SetUp should be called instead of Run in embedded mode
func (s *Server) SetUp() {
	// Start meta handler: watch for kv update and create meta blob
	go s.metaHandler.WatchKvUpdate(s.wg, s.blobs, s.KvUpdate, s.watchHub)
	// Scan existing meta blob
	if s.resync {
		go func() {
			s.wg.Add(1)
			defer s.wg.Done()
			if err := s.metaHandler.Scan(); err != nil {
				panic(err)
			}
			s.ready <- struct{}{}
		}()
	} else {
		s.ready <- struct{}{}
	}
	// Start the worker for handling blob upload
	for i := 0; i < 25; i++ {
		go s.processBlobs()
	}
}

// KvStore return a kv client, written for embedded client
func (s *Server) KvStore() *embed.KvStore {
	return embed.NewKvStore(s.DB, s.KvUpdate, s.Router)
}

// BlobStore returns a blob store clienm for embedded usge
func (s *Server) BlobStore() *embed.BlobStore {
	return embed.NewBlobStore(s.blobs, s.Router)
}

// Run runs the server and block until the server is shutdown
func (s *Server) Run() {
	hawkKey, err := LoadAPIKey("hawk.key")
	if err != nil {
		panic(err)
	}
	// XXX(tsileo): Maybe handle a master key with multiple key? so key can be revoked?
	apiKey, err := LoadAPIKey("api.key")
	if err != nil {
		panic(err)
	}
	authMiddleware := middleware.BasicAuth("", apiKey)
	middlewares := &serverMiddleware.SharedMiddleware{
		Auth: authMiddleware,
	}
	authFunc := httputil.BasicAuthFunc("", apiKey)
	// Start the HTTP API
	s.SetUp()
	r := mux.NewRouter()
	// publicRoute := r.PathPrefix("/public").Subrouter()
	appRoute := r.PathPrefix("/app").Subrouter()
	ekvstore := s.KvStore()
	eblobstore := s.BlobStore()
	docstore.New(s.Log.New("ext", "docstore"), ekvstore, eblobstore).RegisterRoute(r.PathPrefix("/api/ext/docstore/v1").Subrouter(), middlewares)
	luaExt := lua.New(s.Log.New("ext", "lua"), []byte(hawkKey), authFunc, ekvstore, eblobstore)
	luaExt.RegisterRoute(r.PathPrefix("/api/ext/lua/v1").Subrouter(), middlewares)
	luaExt.RegisterAppRoute(appRoute, middlewares)
	api.New(r.PathPrefix("/api/v1").Subrouter(), middlewares, s.wg, s.DB, s.NsDB, s.KvUpdate, s.Router, s.blobs, s.watchHub)

	s.syncer = synctable.New(s.blobs, eblobstore, s.NsDB, s.Log.New("ext", "synctable"))
	s.syncer.RegisterRoute(r.PathPrefix("/api/sync/v1").Subrouter(), middlewares)

	// FIXME(tsileo): a way to make an app hook the index
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		index := `<!doctype html>

  <meta charset="utf-8">
  <title>BlobsStash</title>

This is a private BlobStash instance.

`
		w.Write([]byte(index))
	})
	r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	})
	r.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`User-agent: *
Disallow: /`))
	})

	// FIXME allowedorigins from config
	c := cors.New(cors.Options{
		AllowedOrigins: []string{"*"},
	})
	isDevelopment, _ := strconv.ParseBool(os.Getenv("BLOBSTASH_DEV_MODE"))
	if isDevelopment {
		s.Log.Info("Server started in development mode")
	}
	secureOptions := secure.Options{
		FrameDeny:             true,
		ContentTypeNosniff:    true,
		BrowserXssFilter:      true,
		ContentSecurityPolicy: "default-src 'self'",
		IsDevelopment:         isDevelopment,
	}
	var tlsHostname string
	if tlsHost, ok := s.conf["tls-hostname"]; ok {
		tlsHostname = tlsHost.(string)
		secureOptions.AllowedHosts = []string{tlsHostname}
	}
	secureMiddleware := secure.New(secureOptions)
	http.Handle("/", secureMiddleware.Handler(c.Handler(r)))
	s.Log.Info(fmt.Sprintf("server: HTTP API listening on 0.0.0.0:%d", s.port))
	runFunc := func() {
		if err := http.ListenAndServe(fmt.Sprintf(":%d", s.port), nil); err != nil {
			panic(err)
		}
	}

	if tlsHostname != "" {
		s.tlsConfig.Hostname = tlsHostname
		s.tlsConfig.CertPath = s.conf["tls-crt-path"].(string)
		s.tlsConfig.KeyPath = s.conf["tls-key-path"].(string)
		runFunc = func() {

			srv := &http.Server{
				Addr:    fmt.Sprintf(":%d", s.port),
				Handler: http.DefaultServeMux,
			}
			s.Log.Info(fmt.Sprintf("server: HTTPS API listening on 0.0.0.0:%d", s.port))
			http2.ConfigureServer(srv, &http2.Server{})
			if err := srv.ListenAndServeTLS(s.tlsConfig.CertPath, s.tlsConfig.KeyPath); err != nil {
				panic(err)
			}
		}
	}

	go runFunc()

	s.TillShutdown()
}

// TillShutdown blocks until a kill signal is catched.
func (s *Server) TillShutdown() {
	// Listen for shutdown signal
	cs := make(chan os.Signal, 1)
	signal.Notify(cs, os.Interrupt,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)
	for {
		select {
		case <-s.shutdown:
			break
		case sig := <-cs:
			s.Log.Debug("captured signal", "signal", sig)
			s.Log.Info("shutting down...")
			break
		}
		s.Close()
		os.Exit(0)
	}
}

func (s *Server) Close() {
	close(s.stop)
	s.wg.Wait()
	close(s.KvUpdate)
	s.DB.Close()
	s.NsDB.Close()
	for _, b := range s.Backends {
		b.Close()
	}
}

func LoadAPIKey(kfile string) (string, error) {
	keyPath := filepath.Join(pathutil.ConfigDir(), kfile)
	if _, err := os.Stat(keyPath); os.IsNotExist(err) {
		logger.Log.Debug("Generating new API key...")
		newKey, err := RandomKey()
		if err != nil {
			return "", err
		}
		if err := ioutil.WriteFile(keyPath, []byte(newKey), 0644); err != nil {
			return "", err
		}
		logger.Log.Debug(fmt.Sprintf("API key saved at %v", keyPath))
		return newKey, nil
	}
	logger.Log.Debug(fmt.Sprintf("Loading API key from %v", keyPath))
	data, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return "", err
	}
	return string(data), nil
}
func RandomKey() (string, error) {
	b := make([]byte, 32)
	if _, err := rand.Read(b); err != nil {
		return "", err
	}
	return base64.URLEncoding.EncodeToString(b), nil
}
<|endoftext|>


<commit_before>package main

import "net"  //now look at this net that I just found
import "time" //time is a tool you can put on the wall or wear it on your wrist
//the past is far behind us, the future doesn't exist
import "./players"

func getPlayerOfIp(ip string) players.NetworkPlayer {
	for _, plr := range networkPlayerList {
		if plr.GetIp() == ip {
			return plr
		}
	}
	return nil
}

func handleConn1(conn net.Conn) {
	plr := getPlayerOfIp(conn.RemoteAddr().String())
	for len(plr.GetDataToBeSent()) == 0 {
		time.Sleep(time.Second / 2)
	}
	_, err := conn.Write(plr.GetDataToBeSent()[0])
	if err == nil {
		plr.RemoveASentData()
	}
}

func handleConn2(conn net.Conn) {
	//do things
}

func serverFunc(quitChan chan struct{}) {
	ln1, _ := net.Listen("tcp", ":5252") //event listeners
	ln2, _ := net.Listen("tcp", ":6565") //chat
	for {
		select {
		case <-quitChan:
			return
		default:
			conn1, _ := ln1.Accept()
			go handleConn1(conn1)
			conn2, _ := ln2.Accept()
			go handleConn2(conn2)
		}
	}
}
<commit_msg>chats<commit_after>package main

import "net"  //now look at this net that I just found
import "time" //time is a tool you can put on the wall or wear it on your wrist
//the past is far behind us, the future doesn't exist
import "./players"

func getPlayerOfIp(ip string) players.NetworkPlayer {
	for _, plr := range networkPlayerList {
		if plr.GetIp() == ip {
			return plr
		}
	}
	return nil
}

func handleConn1(conn net.Conn) {
	plr := getPlayerOfIp(conn.RemoteAddr().String())
	for len(plr.GetDataToBeSent()) == 0 {
		time.Sleep(time.Second / 2)
	}
	_, err := conn.Write(plr.GetDataToBeSent()[0])
	if err == nil {
		plr.RemoveASentData()
	}
}

func handleConn2(conn net.Conn) {
	var chat []byte
	_, err := conn.Read(chat)
	if err != nil {
		return
	}
	chat = append([]byte("CHAT"), chat...)
	plr := getPlayerOfIp(conn.RemoteAddr().String())
	for _, plr2 := range networkPlayerList {
		if plr2 != plr {
			plr2.SendData(chat)
		}
	}
}

func serverFunc(quitChan chan struct{}) {
	ln1, _ := net.Listen("tcp", ":5252") //event listeners
	ln2, _ := net.Listen("tcp", ":6565") //chat
	for {
		select {
		case <-quitChan:
			return
		default:
			conn1, _ := ln1.Accept()
			go handleConn1(conn1)
			conn2, _ := ln2.Accept()
			go handleConn2(conn2)
		}
	}
}
<|endoftext|>


<commit_before>package server

import (
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"sync"

	"github.com/blacklabeldata/kappa/auth"
	"github.com/blacklabeldata/kappa/datamodel"
	"github.com/blacklabeldata/kappa/pkg/uuid"
	"github.com/blacklabeldata/sshh"
	"github.com/hashicorp/serf/serf"
	log "github.com/mgutz/logxi/v1"
	"golang.org/x/crypto/ssh"
	tomb "gopkg.in/tomb.v2"
)

const serfSnapshot = "serf/local.snapshot"

func NewServer(c *DatabaseConfig) (server *Server, err error) {

	// Create logger
	if c.LogOutput == nil {
		c.LogOutput = log.NewConcurrentWriter(os.Stdout)
	}
	logger := log.NewLogger(c.LogOutput, "kappa")

	// Create data directory
	if err = os.MkdirAll(c.DataPath, os.ModeDir|0655); err != nil {
		logger.Warn("Could not create data directory", "err", err.Error())
		return
	}

	// Connect to database
	cwd, err := os.Getwd()
	if err != nil {
		logger.Error("Could not get working directory", "error", err.Error())
		return
	}

	file := path.Join(cwd, c.DataPath, "meta.db")
	logger.Info("Connecting to database", "file", file)
	system, err := datamodel.NewSystem(file)
	if err != nil {
		logger.Error("Could not connect to database", "error", err.Error())
		return
	}

	// Get SSH Key file
	sshKeyFile := c.SSHPrivateKeyFile
	logger.Info("Reading private key", "file", sshKeyFile)

	privateKey, err := auth.ReadPrivateKey(logger, sshKeyFile)
	if err != nil {
		return
	}

	// Get admin certificate
	adminCertFile := c.AdminCertificateFile
	logger.Info("Reading admin public key", "file", adminCertFile)

	// Read admin certificate
	cert, err := ioutil.ReadFile(adminCertFile)
	if err != nil {
		logger.Error("admin certificate could not be read", "filename", c.AdminCertificateFile)
		return
	}

	// Add admin cert to key ring
	userStore, err := system.Users()
	if err != nil {
		logger.Error("could not get user store", "error", err.Error())
		return
	}

	// Create admin account
	admin, err := userStore.Create("admin")
	if err != nil {
		logger.Error("error creating admin account", "error", err.Error())
		return
	}

	// Add admin certificate
	keyRing := admin.KeyRing()
	fingerprint, err := keyRing.AddPublicKey(cert)
	if err != nil {
		logger.Error("admin certificate could not be added", "error", err.Error())
		return
	}
	logger.Info("Added admin certificate", "fingerprint", fingerprint)

	// Read root cert
	rootPem, err := ioutil.ReadFile(c.CACertificateFile)
	if err != nil {
		logger.Error("root certificate could not be read", "filename", c.CACertificateFile)
		return
	}

	// Create certificate pool
	roots := x509.NewCertPool()
	if ok := roots.AppendCertsFromPEM(rootPem); !ok {
		logger.Error("failed to parse root certificate")
		return
	}

	// Setup SSH Server
	sshLogger := log.NewLogger(c.LogOutput, "ssh")
	pubKeyCallback, err := PublicKeyCallback(system)
	if err != nil {
		logger.Error("failed to create PublicKeyCallback", err)
		return
	}

	// Setup server config
	config := sshh.Config{
		Deadline:          c.SSHConnectionDeadline,
		Logger:            sshLogger,
		Bind:              c.SSHBindAddress,
		PrivateKey:        privateKey,
		PublicKeyCallback: pubKeyCallback,
		AuthLogCallback: func(meta ssh.ConnMetadata, method string, err error) {
			if err == nil {
				sshLogger.Info("login success", "user", meta.User())
			} else if err != nil && method == "publickey" {
				sshLogger.Info("login failure", "user", meta.User(), "err", err.Error())
			}
		},
		Handlers: map[string]sshh.SSHHandler{
			"kappa-client": &EchoHandler{},
		},
	}

	// Create SSH server
	sshServer, err := sshh.NewSSHServer(&config)
	if err != nil {
		logger.Error("SSH Server could not be configured", "error", err.Error())
		return
	}

	// Create database server
	s := &Server{
		config:       c,
		logger:       logger,
		sshServer:    &sshServer,
		serfEventCh:  make(chan serf.Event, 256),
		kappaEventCh: make(chan serf.UserEvent, 256),
		reconcileCh:  make(chan serf.Member, 32),
	}

	// Create serf server
	s.serf, err = s.setupSerf()
	if err != nil {
		err = fmt.Errorf("Failed to start serf: %v", err)
		return
	}

	return s, nil
}

type Server struct {
	config    *DatabaseConfig
	logger    log.Logger
	sshServer *sshh.SSHServer

	// localKappas is used to track the known kappas
	// in the cluster. Used to do leader forwarding.
	localKappas map[string]*NodeDetails
	localLock   sync.RWMutex

	serf         *serf.Serf
	serfEventCh  chan serf.Event
	kappaEventCh chan serf.UserEvent
	reconcileCh  chan serf.Member
	t            tomb.Tomb
}

func (s *Server) Start() {
	s.sshServer.Start()

	// Start serf handler
	s.t.Go(s.serfEventHandler)
}

func (s *Server) Stop() {
	s.sshServer.Stop()

	// Kill Serf handler
	s.t.Kill(nil)
	s.logger.Info("Shutting down Serf server...")
	s.t.Wait()
}

func (s *Server) setupSerf() (*serf.Serf, error) {
	conf := serf.DefaultConfig()

	// Generate NodeName if missing
	id, err := uuid.UUID4()
	if err != nil {
		return nil, err
	}

	// Get SSH server port
	port := s.sshServer.Addr.Port

	// Initialize serf
	conf.Init()

	conf.NodeName = s.config.NodeName
	conf.MemberlistConfig.BindAddr = s.config.GossipBindAddr
	conf.MemberlistConfig.BindPort = s.config.GossipBindPort
	conf.MemberlistConfig.AdvertiseAddr = s.config.GossipAdvertiseAddr
	conf.MemberlistConfig.AdvertisePort = s.config.GossipAdvertisePort

	conf.Tags["id"] = id
	conf.Tags["role"] = "kappa"
	conf.Tags["cluster"] = s.config.ClusterName
	conf.Tags["build"] = s.config.Build
	conf.Tags["port"] = fmt.Sprintf("%d", port)
	if s.config.Bootstrap {
		conf.Tags["bootstrap"] = "1"
	}
	if s.config.BootstrapExpect != 0 {
		conf.Tags["expect"] = fmt.Sprintf("%d", s.config.BootstrapExpect)
	}

	conf.MemberlistConfig.LogOutput = s.config.LogOutput
	conf.LogOutput = s.config.LogOutput
	conf.EventCh = s.serfEventCh
	conf.SnapshotPath = filepath.Join(s.config.DataPath, serfSnapshot)
	conf.ProtocolVersion = conf.ProtocolVersion
	conf.RejoinAfterLeave = true
	conf.EnableNameConflictResolution = false

	conf.Merge = &mergeDelegate{name: s.config.ClusterName}
	if err := ensurePath(conf.SnapshotPath, false); err != nil {
		return nil, err
	}
	return serf.Create(conf)
}

// Join is used to have Kappa join the cluster.
// The target address should be another node inside the cluster
// listening on the Serf address
func (s *Server) Join(addrs []string) (int, error) {
	return s.serf.Join(addrs, true)
}

// LocalMember is used to return the local node
func (c *Server) LocalMember() serf.Member {
	return c.serf.LocalMember()
}

// Members is used to return the members of the cluster
func (s *Server) Members() []serf.Member {
	return s.serf.Members()
}

// RemoveFailedNode is used to remove a failed node from the cluster
func (s *Server) RemoveFailedNode(node string) error {
	if err := s.serf.RemoveFailedNode(node); err != nil {
		return err
	}
	return nil
}

// IsLeader checks if this server is the cluster leader
func (s *Server) IsLeader() bool {
	return true
	// return s.raft.State() == raft.Leader
}

// KeyManager returns the Serf keyring manager
func (s *Server) KeyManager() *serf.KeyManager {
	return s.serf.KeyManager()
}

// Encrypted determines if gossip is encrypted
func (s *Server) Encrypted() bool {
	return s.serf.EncryptionEnabled()
}
<commit_msg>Update server start / stop and add logging<commit_after>package server

import (
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"sync"

	"github.com/blacklabeldata/kappa/auth"
	"github.com/blacklabeldata/kappa/datamodel"
	"github.com/blacklabeldata/kappa/pkg/uuid"
	"github.com/blacklabeldata/sshh"
	"github.com/hashicorp/serf/serf"
	log "github.com/mgutz/logxi/v1"
	"golang.org/x/crypto/ssh"
	tomb "gopkg.in/tomb.v2"
)

const serfSnapshot = "serf/local.snapshot"

func NewServer(c *DatabaseConfig) (server *Server, err error) {

	// Create logger
	if c.LogOutput == nil {
		c.LogOutput = log.NewConcurrentWriter(os.Stdout)
	}
	logger := log.NewLogger(c.LogOutput, "kappa")

	// Create data directory
	if err = os.MkdirAll(c.DataPath, 0755); err != nil {
		logger.Warn("Could not create data directory", "err", err)
		// logger.Warn("Could not create data directory", "err", err.Error())
		return
	}

	// Connect to database
	cwd, err := os.Getwd()
	if err != nil {
		logger.Error("Could not get working directory", "error", err.Error())
		return
	}

	file := path.Join(cwd, c.DataPath, "meta.db")
	logger.Info("Connecting to database", "file", file)
	system, err := datamodel.NewSystem(file)
	if err != nil {
		logger.Error("Could not connect to database", "error", err.Error())
		return
	}

	// Get SSH Key file
	sshKeyFile := c.SSHPrivateKeyFile
	logger.Info("Reading private key", "file", sshKeyFile)

	privateKey, err := auth.ReadPrivateKey(logger, sshKeyFile)
	if err != nil {
		return
	}

	// Get admin certificate
	adminCertFile := c.AdminCertificateFile
	logger.Info("Reading admin public key", "file", adminCertFile)

	// Read admin certificate
	cert, err := ioutil.ReadFile(adminCertFile)
	if err != nil {
		logger.Error("admin certificate could not be read", "filename", c.AdminCertificateFile)
		return
	}

	// Add admin cert to key ring
	userStore, err := system.Users()
	if err != nil {
		logger.Error("could not get user store", "error", err.Error())
		return
	}

	// Create admin account
	admin, err := userStore.Create("admin")
	if err != nil {
		logger.Error("error creating admin account", "error", err.Error())
		return
	}

	// Add admin certificate
	keyRing := admin.KeyRing()
	fingerprint, err := keyRing.AddPublicKey(cert)
	if err != nil {
		logger.Error("admin certificate could not be added", "error", err.Error())
		return
	}
	logger.Info("Added admin certificate", "fingerprint", fingerprint)

	// Read root cert
	rootPem, err := ioutil.ReadFile(c.CACertificateFile)
	if err != nil {
		logger.Error("root certificate could not be read", "filename", c.CACertificateFile)
		return
	}

	// Create certificate pool
	roots := x509.NewCertPool()
	if ok := roots.AppendCertsFromPEM(rootPem); !ok {
		logger.Error("failed to parse root certificate")
		return
	}

	// Setup SSH Server
	sshLogger := log.NewLogger(c.LogOutput, "ssh")
	pubKeyCallback, err := PublicKeyCallback(system)
	if err != nil {
		logger.Error("failed to create PublicKeyCallback", err)
		return
	}

	// Setup server config
	config := sshh.Config{
		Deadline:          c.SSHConnectionDeadline,
		Logger:            sshLogger,
		Bind:              c.SSHBindAddress,
		PrivateKey:        privateKey,
		PublicKeyCallback: pubKeyCallback,
		AuthLogCallback: func(meta ssh.ConnMetadata, method string, err error) {
			if err == nil {
				sshLogger.Info("login success", "user", meta.User())
			} else if err != nil && method == "publickey" {
				sshLogger.Info("login failure", "user", meta.User(), "err", err.Error())
			}
		},
		Handlers: map[string]sshh.SSHHandler{
			"kappa-client": &EchoHandler{},
		},
	}

	// Create SSH server
	sshServer, err := sshh.NewSSHServer(&config)
	if err != nil {
		logger.Error("SSH Server could not be configured", "error", err.Error())
		return
	}

	// Create database server
	s := &Server{
		config:       c,
		logger:       logger,
		sshServer:    &sshServer,
		localKappas:  make(map[string]*NodeDetails),
		serfEventCh:  make(chan serf.Event, 256),
		kappaEventCh: make(chan serf.UserEvent, 256),
		reconcileCh:  make(chan serf.Member, 32),
	}

	// Create serf server
	s.serf, err = s.setupSerf()
	if err != nil {
		err = logger.Error("Failed to start serf: %v", err)
		return
	}

	return s, nil
}

type Server struct {
	config    *DatabaseConfig
	logger    log.Logger
	sshServer *sshh.SSHServer

	// localKappas is used to track the known kappas
	// in the cluster. Used to do leader forwarding.
	localKappas map[string]*NodeDetails
	localLock   sync.RWMutex

	serf         *serf.Serf
	serfEventCh  chan serf.Event
	kappaEventCh chan serf.UserEvent
	reconcileCh  chan serf.Member
	t            tomb.Tomb
}

func (s *Server) Start() error {
	s.sshServer.Start()

	// Start serf handler
	s.t.Go(s.serfEventHandler)

	// Join serf cluster
	// if !s.config.Bootstrap && len(s.config.ExistingNodes) > 0 {
	// addr, err := s.serf.Memberlist().LocalNode().Addr.MarshalText()
	s.logger.Info("local node", "port", s.serf.LocalMember().Port)
	s.logger.Info("Joining cluster", "nodes", s.config.ExistingNodes)

	n, err := s.serf.Join(s.config.ExistingNodes, true)
	if err != nil && !s.config.Bootstrap {
		err = s.logger.Error("Failed to join cluster", "err", err)
		return err
	}
	s.logger.Info("Joined cluster", "nodes", n)
	// }
	return nil
}

func (s *Server) Stop() {

	// Stop SSH server
	s.sshServer.Stop()

	// Shutdown serf
	s.serf.Leave()
	s.serf.Shutdown()
	<-s.serf.ShutdownCh()

	// Kill Serf handler
	s.t.Kill(nil)
	s.logger.Info("Shutting down Serf server...")
	s.t.Wait()
}

func (s *Server) setupSerf() (*serf.Serf, error) {
	conf := serf.DefaultConfig()

	// Generate NodeName if missing
	id, err := uuid.UUID4()
	if err != nil {
		return nil, err
	}

	// Get SSH server port
	port := s.sshServer.Addr.Port

	// Initialize serf
	conf.Init()
	// s.logger.Info("local port", "port", conf.MemberlistConfig.BindPort)

	conf.NodeName = s.config.NodeName
	conf.MemberlistConfig.BindAddr = s.config.GossipBindAddr
	s.logger.Info("local node", "BindAddr", conf.MemberlistConfig.BindAddr)
	conf.MemberlistConfig.BindPort = s.config.GossipBindPort
	s.logger.Info("local node", "BindPort", conf.MemberlistConfig.BindPort)
	conf.MemberlistConfig.AdvertiseAddr = s.config.GossipAdvertiseAddr
	s.logger.Info("local node", "AdvertiseAddr", conf.MemberlistConfig.AdvertiseAddr)
	conf.MemberlistConfig.AdvertisePort = s.config.GossipAdvertisePort
	s.logger.Info("local node", "AdvertisePort", conf.MemberlistConfig.AdvertisePort)

	conf.Tags["id"] = id
	conf.Tags["role"] = "kappa"
	conf.Tags["cluster"] = s.config.ClusterName
	conf.Tags["build"] = s.config.Build
	conf.Tags["port"] = fmt.Sprintf("%d", port)
	if s.config.Bootstrap {
		conf.Tags["bootstrap"] = "1"
	}
	if s.config.BootstrapExpect != 0 {
		conf.Tags["expect"] = fmt.Sprintf("%d", s.config.BootstrapExpect)
	}

	conf.MemberlistConfig.LogOutput = s.config.LogOutput
	conf.LogOutput = s.config.LogOutput
	conf.EventCh = s.serfEventCh
	conf.SnapshotPath = filepath.Join(s.config.DataPath, serfSnapshot)
	conf.ProtocolVersion = conf.ProtocolVersion
	conf.RejoinAfterLeave = true
	conf.EnableNameConflictResolution = false

	conf.Merge = &mergeDelegate{name: s.config.ClusterName}
	if err := ensurePath(conf.SnapshotPath, false); err != nil {
		return nil, err
	}
	return serf.Create(conf)
}

// Join is used to have Kappa join the cluster.
// The target address should be another node inside the cluster
// listening on the Serf address
func (s *Server) Join(addrs []string) (int, error) {
	return s.serf.Join(addrs, true)
}

// LocalMember is used to return the local node
func (c *Server) LocalMember() serf.Member {
	return c.serf.LocalMember()
}

// Members is used to return the members of the cluster
func (s *Server) Members() []serf.Member {
	return s.serf.Members()
}

// RemoveFailedNode is used to remove a failed node from the cluster
func (s *Server) RemoveFailedNode(node string) error {
	if err := s.serf.RemoveFailedNode(node); err != nil {
		return err
	}
	return nil
}

// IsLeader checks if this server is the cluster leader
func (s *Server) IsLeader() bool {
	return true
	// return s.raft.State() == raft.Leader
}

// KeyManager returns the Serf keyring manager
func (s *Server) KeyManager() *serf.KeyManager {
	return s.serf.KeyManager()
}

// Encrypted determines if gossip is encrypted
func (s *Server) Encrypted() bool {
	return s.serf.EncryptionEnabled()
}
<|endoftext|>


<commit_before>package server

import (
	"fmt"
	"github.com/andreaskoch/docs/indexer"
	"github.com/andreaskoch/docs/renderer"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
)

var routes map[string]indexer.Addresser

func Serve(repositoryPaths []string) {

	// An array of all indices for
	// the given repositories.
	indices := renderer.Render(repositoryPaths)

	// Initialize the routing table
	InitializeRoutes(indices)

	var error404Handler = func(w http.ResponseWriter, r *http.Request) {
		requestedPath := r.URL.Path
		fmt.Fprintf(w, "Not found: %v", requestedPath)
	}

	var itemHandler = func(w http.ResponseWriter, r *http.Request) {
		requestedPath := r.URL.Path

		fmt.Println(requestedPath)

		item, ok := routes[requestedPath]
		if !ok {
			error404Handler(w, r)
			return
		}

		data, err := ioutil.ReadFile(item.GetAbsolutePath())
		if err != nil {
			error404Handler(w, r)
			return
		}

		fmt.Fprintf(w, "%s", data)
	}

	var indexDebugger = func(w http.ResponseWriter, r *http.Request) {
		for route, _ := range routes {
			fmt.Fprintln(w, route)
		}
	}

	http.HandleFunc("/", itemHandler)
	http.HandleFunc("/debug/index", indexDebugger)
	http.ListenAndServe(":8080", nil)
}

func InitializeRoutes(indices []*indexer.Index) {

	routes = make(map[string]indexer.Addresser)

	for _, index := range indices {

		updateRouteTable := func(item *indexer.Item) {
			// add the item to the route table
			itemRoute := getHttpRouteFromFilePath(item.GetRelativePath(index.Path))
			RegisterRoute(itemRoute, item)

			// add the item's files to the route table
			for _, file := range item.Files {
				fileRoute := getHttpRouteFromFilePath(file.GetRelativePath(index.Path))
				RegisterRoute(fileRoute, file)
			}
		}

		index.Walk(func(item *indexer.Item) {
			updateRouteTable(item)
		})

	}
}

func getHttpRouteFromFilePath(path string) string {
	return strings.Replace(path, string(os.PathSeparator), "/", -1)
}

func RegisterRoute(route string, item indexer.Addresser) {

	if item == nil {
		log.Printf("Cannot add a route for an uninitialized item. Route: %#v\n", route)
		return
	}

	if strings.TrimSpace(route) == "" {
		log.Printf("Cannot add an empty route to the routing table. Item: %#v\n", item)
		return
	}

	anotherItem, ok := routes[route]
	if ok {
		fmt.Printf("The route \"%s\" is already in use by another item. Item: %#v\n", route, anotherItem)
		return
	}

	routes[route] = item
}
<commit_msg>Update the routing table if an item changed<commit_after>package server

import (
	"fmt"
	"github.com/andreaskoch/docs/indexer"
	"github.com/andreaskoch/docs/renderer"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
)

var routes map[string]indexer.Addresser

func Serve(repositoryPaths []string) {

	// An array of all indices for
	// the given repositories.
	indices := renderer.Render(repositoryPaths)

	// Initialize the routing table
	InitializeRoutes(indices)

	var error404Handler = func(w http.ResponseWriter, r *http.Request) {
		requestedPath := r.URL.Path
		fmt.Fprintf(w, "Not found: %v", requestedPath)
	}

	var itemHandler = func(w http.ResponseWriter, r *http.Request) {
		requestedPath := r.URL.Path

		fmt.Println(requestedPath)

		item, ok := routes[requestedPath]
		if !ok {
			error404Handler(w, r)
			return
		}

		data, err := ioutil.ReadFile(item.GetAbsolutePath())
		if err != nil {
			error404Handler(w, r)
			return
		}

		fmt.Fprintf(w, "%s", data)
	}

	var indexDebugger = func(w http.ResponseWriter, r *http.Request) {
		for route, _ := range routes {
			fmt.Fprintln(w, route)
		}
	}

	http.HandleFunc("/", itemHandler)
	http.HandleFunc("/debug/index", indexDebugger)
	http.ListenAndServe(":8080", nil)
}

func InitializeRoutes(indices []*indexer.Index) {

	routes = make(map[string]indexer.Addresser)

	for _, index := range indices {

		updateRouteTable := func(item *indexer.Item) {

			// get the item route and
			// add it to the routing table
			itemRoute := getHttpRouteFromFilePath(item.GetRelativePath(index.Path))
			RegisterRoute(itemRoute, item)

			// get the file routes and
			// add them to the routing table
			for _, file := range item.Files {
				fileRoute := getHttpRouteFromFilePath(file.GetRelativePath(index.Path))
				RegisterRoute(fileRoute, file)
			}
		}

		index.Walk(func(item *indexer.Item) {

			// add the current item to the route table
			updateRouteTable(item)

			// update route table again if item changes
			item.RegisterOnChangeCallback("UpdateRouteTableOnChange", func(i *indexer.Item) {
				i.IndexFiles()
				updateRouteTable(i)
			})
		})

	}
}

func getHttpRouteFromFilePath(path string) string {
	return strings.Replace(path, string(os.PathSeparator), "/", -1)
}

func RegisterRoute(route string, item indexer.Addresser) {

	if item == nil {
		log.Printf("Cannot add a route for an uninitialized item. Route: %#v\n", route)
		return
	}

	if strings.TrimSpace(route) == "" {
		log.Printf("Cannot add an empty route to the routing table. Item: %#v\n", item)
		return
	}

	routes[route] = item
}
<|endoftext|>


<commit_before>package server

import (
	"bufio"
	"bytes"
	"github.com/myechuri/ukd/server/api"
	"golang.org/x/net/context"
	"google.golang.org/grpc/grpclog"
	"io/ioutil"
	"os"
	"os/exec"
	"regexp"
	"strings"
	"syscall"
)

type version_type struct {
	Major int32
	Minor int32
}

const (
	X86_64 = "x86_64"
	ARMv71 = "armv71"
)

type PlatformRuntimeInfo struct {
	platform string
}

type AppRuntimeInfo struct {
	Process *os.Process
	Image   string
}

type ukdServer struct {
	Version         version_type
	PlatformRuntime PlatformRuntimeInfo
	AppRuntime      map[string]*AppRuntimeInfo
}

var arch string

func (s ukdServer) GetVersion(context context.Context, request *api.VersionRequest) (*api.VersionReply, error) {
	reply := api.VersionReply{
		Major: s.Version.Major,
		Minor: s.Version.Minor}
	grpclog.Printf("Version request")
	return &reply, nil
}

func ComposeQemuX86_64Command(name string, location string) (string, []string, error) {
	driveArg := "file=" + location + ",if=none,id=hd0,cache=none,aio=native"

	// Compose application-specific configuration.
	configRoot := "/var/lib/ukd"
	os.Mkdir(configRoot, 0777) // TODO: check error
	configRoot += "/" + name
	os.Mkdir(configRoot, 0777) // TODO: check error
	qemuIfupByteArray := []byte("#!/bin/sh\n" +
		"brctl stp virbr0 off\n" +
		"brctl addif virbr0 $1\n" +
		"ifconfig $1 up\n")
	configRoot += "/qemu-ifup.sh"
	ioutil.WriteFile(configRoot, qemuIfupByteArray, 0700) // TODO: check error
	netdevArg := "tap,id=hn0,script=" + configRoot + ",vhost=on"

	cmdName := "qemu-system-x86_64"
	args := []string{
		"-m", "2G",
		"-smp", "4",
		"-vnc", ":1",
		"-gdb", "tcp::1234,server,nowait",
		"-device", "virtio-blk-pci,id=blk0,bootindex=0,drive=hd0,scsi=off",
		"-drive", driveArg,
		"-netdev", netdevArg,
		"-device", "virtio-net-pci,netdev=hn0,id=nic0",
		"-redir", "tcp:2222::22",
		"-device", "virtio-rng-pci",
		"-enable-kvm",
		"-cpu", "host,+x2apic",
		"-chardev", "stdio,mux=on,id=stdio,signal=off",
		"-mon", "chardev=stdio,mode=readline,default",
		"-device", "isa-serial,chardev=stdio"}

	return cmdName, args, nil
}

func ComposeQemuAarch64Command(name string, location string) (string, []string, error) {

	// Use user networking for now.
	// TODO: add tap netdev.
	cmdName := "qemu-system-aarch64"
	args := []string{
		"-machine", "virt",
		"-cpu", "cortex-a57",
		"-kernel", location,
		"--nographic"}

	return cmdName, args, nil
}

func StartQemu(s ukdServer, name string, location string) (*api.StartReply, error) {

	var cmdName string
	var args []string
	// TODO: Handle error returned by ComposeQemu*
	if arch == X86_64 {
		cmdName, args, _ = ComposeQemuX86_64Command(name, location)
	} else if arch == ARMv71 {
		cmdName, args, _ = ComposeQemuAarch64Command(name, location)
	} else {
		reply := &api.StartReply{
			Success: false,
			Ip:      "",
			Info:    "Platform " + arch + " is not yet supported."}
		return reply, nil
	}

	cmd := exec.Command(cmdName, args...)

	// Disable Glibc's per-thread arena to limit qemu virtual memory.
	// [ References:
	// 1. https://siddhesh.in/posts/malloc-per-thread-arenas-in-glibc.html
	// 2. https://devcenter.heroku.com/articles/tuning-glibc-memory-behavior ]
	cmd.Env = []string{"MALLOC_ARENA_MAX=1"}

	stdout, _ := cmd.StdoutPipe()
	cmd.Start()

	r := bufio.NewReader(stdout)
	matched := false
	var line []byte
	var ip string
	for !(matched) {
		line, _, _ = r.ReadLine()
		grpclog.Printf("%s", string(line))
		matched, _ = regexp.MatchString("eth0:.*", string(line))
	}
	ip = strings.Fields(string(line))[1]
	runtime := &AppRuntimeInfo{Process: cmd.Process,
		Image: location}
	s.AppRuntime[name] = runtime

	reply := api.StartReply{
		Success: true, // TODO: gather err from previous steps
		Ip:      ip,
		Info:    "Successful start"}
	return &reply, nil
}

func (s ukdServer) Status(context context.Context, request *api.StatusRequest) (*api.StatusReply, error) {
	grpclog.Printf("Status request: name: %s", request.Name)

	// Validate application name does not exist.
	if s.AppRuntime[request.Name] != nil {
		reply := api.StatusReply{
			Success: true,
			Status:  "RUNNING",
			Info:    ""}
		return &reply, nil
	} else {
		reply := api.StatusReply{
			Success: true,
			Status:  "STOPPED",
			Info:    ""}
		return &reply, nil
	}

}

func (s ukdServer) Start(context context.Context, request *api.StartRequest) (*api.StartReply, error) {
	grpclog.Printf("Start request: name: %s, Image: %s", request.Name, request.Location)

	// Validate image exists.
	if _, err := os.Stat(request.Location); os.IsNotExist(err) {
		reply := api.StartReply{
			Success: false,
			Ip:      "",
			Info:    "Image " + request.Location + " does not exist, error: " + err.Error()}
		return &reply, nil
	}

	// Validate application name does not exist.
	if s.AppRuntime[request.Name] != nil {
		reply := api.StartReply{
			Success: false,
			Ip:      "",
			Info:    request.Name + " is already running. Please choose a different name for the application if you wish to start a second instance using the same image."}
		return &reply, nil
	}

	var reply *api.StartReply
	if request.Visor == "kvm-qemu" {
		reply, _ = StartQemu(s, request.Name, request.Location)
	} else {
		reply = &api.StartReply{
			Success: false,
			Ip:      "",
			Info:    "Requested hypervisor (" + request.Visor + ") is not yet supported."}
	}
	return reply, nil
}

func (s ukdServer) Stop(context context.Context, request *api.StopRequest) (*api.StopReply, error) {
	grpclog.Printf("Stop request: name: %s", request.Name)
	var success bool
	var info string
	runtime := s.AppRuntime[request.Name]
	if runtime == nil {
		success = true
		info = "App not found. Nothing to do."
	} else {
		runtime.Process.Signal(syscall.SIGTERM) // TODO: check error
		pstate, _ := runtime.Process.Wait()     // TODO: check err
		if pstate.Exited() {
			success = true
			info = "Successfully stopped Application (" + request.Name + ")"
			delete(s.AppRuntime, request.Name)
		} else {
			success = false
			info = pstate.String()
		}
	}
	reply := api.StopReply{
		Success: success,
		Info:    info}
	grpclog.Printf("Stop request")
	return &reply, nil

}

func ComputeSignature(path string) (bool, []byte, string) {
	var success bool
	var info string
	var signature []byte

	if _, err := os.Stat(path); os.IsNotExist(err) {
		success = false
		info = "File not found: " + path
		return success, nil, info
	}

	workDir, _ := ioutil.TempDir("", "ukd-compute-signature-")
	defer os.RemoveAll(workDir)

	// Validate base image signature sent by client matches
	// server base image signature.
	serverImageSignature := workDir + "/serverSignature"
	cmdName := "rdiff"
	args := []string{"signature", path, serverImageSignature}
	cmd := exec.Command(cmdName, args...)
	err := cmd.Run()
	if err != nil {
		success = false
		info = "Failed to compute signature for " + path + ", error: " + err.Error()
		return success, nil, info
	}
	signature, err = ioutil.ReadFile(serverImageSignature)
	success = true
	info = "Successfully computed signature"

	// TODO: delete workDir
	return success, signature, info

}

func (s ukdServer) GetImageSignature(context context.Context, request *api.ImageSignatureRequest) (*api.ImageSignatureReply, error) {
	grpclog.Printf("Signature request: image=%s", request.Path)
	var success bool
	var info string
	var signature []byte

	// TODO: check that image is not currently in use.
	success, signature, info = ComputeSignature(request.Path)

	reply := api.ImageSignatureReply{
		Success:   success,
		Signature: signature,
		Info:      info}
	grpclog.Printf("GetImageSignature request")
	return &reply, nil
}

func ApplyDiff(base string, basesig []byte, diff []byte) (bool, string, string) {
	var success bool
	var info string

	workDir, _ := ioutil.TempDir("", "ukd-update-stage-")

	// Validate base image signature sent by client matches
	// server base image signature.
	serverImageSignature := workDir + "/serverSignature"
	cmdName := "rdiff"
	args := []string{"signature", base, serverImageSignature}
	cmd := exec.Command(cmdName, args...)
	err := cmd.Run()
	serverSignature, err := ioutil.ReadFile(serverImageSignature)
	defer os.RemoveAll(serverImageSignature)
	if !bytes.Equal(serverSignature, basesig) {
		success = false
		info = "Diff was generated for a different base image than " + base
		return success, "", info
	}

	// Write out diff to delta file.
	deltaFile := workDir + "/deltaFile"
	f, err := os.Create(deltaFile)
	if err != nil {
		grpclog.Printf("Failed to create temp file")
		success = false
		info = "Failed to create delta file " + deltaFile + ", error: " + err.Error()
		return success, "", info
	}
	err = ioutil.WriteFile(deltaFile, diff, 0700)
	f.Close()
	defer os.RemoveAll(deltaFile)

	updatedImagePath := workDir + "/newImage.img"
	cmdName = "rdiff"
	args = []string{"patch", base, deltaFile, updatedImagePath}
	cmd = exec.Command(cmdName, args...)
	err = cmd.Run()
	if err != nil {
		success = false
		info = "Failed to patch (" + base + " with " + deltaFile + ", error: " + err.Error()
	} else {
		success = true
		info = "Successfully staged patched image at " + updatedImagePath + ". Please validate the image before replacing master copy."
	}

	return success, updatedImagePath, info
}

func (s ukdServer) UpdateImage(context context.Context, request *api.UpdateImageRequest) (*api.UpdateImageReply, error) {
	grpclog.Printf("Update request: base=%s", request.Base)
	var success bool
	var info string

	// TODO: check that image is not currently in use.
	var newImagePath string
	success, newImagePath, info = ApplyDiff(request.Base, request.Basesig, request.Diff)

	if success {
		var newSignature []byte
		success, newSignature, info = ComputeSignature(newImagePath)
		if !bytes.Equal(newSignature, request.Newsig) {
			success = false
			info = "New image signature on destination does not match new image signature on source"
		} else {
			info = "Verified signature match for new Image on source and destination"
		}
	}

	reply := api.UpdateImageReply{
		Success:         success,
		StagedImagePath: newImagePath,
		Info:            info}
	grpclog.Printf("UpdateImage: success: %t, staged image path: %s, info: %s", success, newImagePath, info)
	return &reply, nil
}

func getPlatformRuntime() (PlatformRuntimeInfo, error) {

	cmdName := "arch"
	args := []string{}
	cmd := exec.Command(cmdName, args...)
	archBytes, err := cmd.Output()
	arch = strings.TrimSpace(string(archBytes))
	platformRuntimeInfo := PlatformRuntimeInfo{platform: arch}
	grpclog.Printf("Detected arch: %s on the system", arch)
	return platformRuntimeInfo, err
}

func NewServer() (*ukdServer, error) {

	var s *ukdServer
	platform, err := getPlatformRuntime()
	if err != nil {
		grpclog.Printf("Failed to detect runtime platform, error: %s", err.Error())
		return s, err
	}

	s = &ukdServer{Version: version_type{Major: 0, Minor: 1},
		PlatformRuntime: platform,
		AppRuntime:      make(map[string]*AppRuntimeInfo)}

	// Image home.
	imagePath := "/var/lib/ukd/images"
	err = os.MkdirAll("/var/lib/ukd/images", 0700)
	if err != nil {
		grpclog.Printf("MkdirAll failed %q: %s", imagePath, err)
	}

	return s, err
}
<commit_msg>remove debug prints<commit_after>package server

import (
	"bufio"
	"bytes"
	"github.com/myechuri/ukd/server/api"
	"golang.org/x/net/context"
	"google.golang.org/grpc/grpclog"
	"io/ioutil"
	"os"
	"os/exec"
	"regexp"
	"strings"
	"syscall"
)

type version_type struct {
	Major int32
	Minor int32
}

const (
	X86_64 = "x86_64"
	ARMv71 = "armv71"
)

type PlatformRuntimeInfo struct {
	platform string
}

type AppRuntimeInfo struct {
	Process *os.Process
	Image   string
}

type ukdServer struct {
	Version         version_type
	PlatformRuntime PlatformRuntimeInfo
	AppRuntime      map[string]*AppRuntimeInfo
}

var arch string

func (s ukdServer) GetVersion(context context.Context, request *api.VersionRequest) (*api.VersionReply, error) {
	reply := api.VersionReply{
		Major: s.Version.Major,
		Minor: s.Version.Minor}
	grpclog.Printf("Version request")
	return &reply, nil
}

func ComposeQemuX86_64Command(name string, location string) (string, []string, error) {
	driveArg := "file=" + location + ",if=none,id=hd0,cache=none,aio=native"

	// Compose application-specific configuration.
	configRoot := "/var/lib/ukd"
	os.Mkdir(configRoot, 0777) // TODO: check error
	configRoot += "/" + name
	os.Mkdir(configRoot, 0777) // TODO: check error
	qemuIfupByteArray := []byte("#!/bin/sh\n" +
		"brctl stp virbr0 off\n" +
		"brctl addif virbr0 $1\n" +
		"ifconfig $1 up\n")
	configRoot += "/qemu-ifup.sh"
	ioutil.WriteFile(configRoot, qemuIfupByteArray, 0700) // TODO: check error
	netdevArg := "tap,id=hn0,script=" + configRoot + ",vhost=on"

	cmdName := "qemu-system-x86_64"
	args := []string{
		"-m", "2G",
		"-smp", "4",
		"-vnc", ":1",
		"-gdb", "tcp::1234,server,nowait",
		"-device", "virtio-blk-pci,id=blk0,bootindex=0,drive=hd0,scsi=off",
		"-drive", driveArg,
		"-netdev", netdevArg,
		"-device", "virtio-net-pci,netdev=hn0,id=nic0",
		"-redir", "tcp:2222::22",
		"-device", "virtio-rng-pci",
		"-enable-kvm",
		"-cpu", "host,+x2apic",
		"-chardev", "stdio,mux=on,id=stdio,signal=off",
		"-mon", "chardev=stdio,mode=readline,default",
		"-device", "isa-serial,chardev=stdio"}

	return cmdName, args, nil
}

func ComposeQemuAarch64Command(name string, location string) (string, []string, error) {

	// Use user networking for now.
	// TODO: add tap netdev.
	cmdName := "qemu-system-aarch64"
	args := []string{
		"-machine", "virt",
		"-cpu", "cortex-a57",
		"-kernel", location,
		"--nographic"}

	return cmdName, args, nil
}

func StartQemu(s ukdServer, name string, location string) (*api.StartReply, error) {

	var cmdName string
	var args []string
	// TODO: Handle error returned by ComposeQemu*
	if arch == X86_64 {
		cmdName, args, _ = ComposeQemuX86_64Command(name, location)
	} else if arch == ARMv71 {
		cmdName, args, _ = ComposeQemuAarch64Command(name, location)
	} else {
		reply := &api.StartReply{
			Success: false,
			Ip:      "",
			Info:    "Platform " + arch + " is not yet supported."}
		return reply, nil
	}

	cmd := exec.Command(cmdName, args...)

	// Disable Glibc's per-thread arena to limit qemu virtual memory.
	// [ References:
	// 1. https://siddhesh.in/posts/malloc-per-thread-arenas-in-glibc.html
	// 2. https://devcenter.heroku.com/articles/tuning-glibc-memory-behavior ]
	cmd.Env = []string{"MALLOC_ARENA_MAX=1"}

	stdout, _ := cmd.StdoutPipe()
	cmd.Start()

	r := bufio.NewReader(stdout)
	matched := false
	var line []byte
	var ip string
	for !(matched) {
		line, _, _ = r.ReadLine()
		matched, _ = regexp.MatchString("eth0:.*", string(line))
	}
	ip = strings.Fields(string(line))[1]
	runtime := &AppRuntimeInfo{Process: cmd.Process,
		Image: location}
	s.AppRuntime[name] = runtime

	reply := api.StartReply{
		Success: true, // TODO: gather err from previous steps
		Ip:      ip,
		Info:    "Successful start"}
	return &reply, nil
}

func (s ukdServer) Status(context context.Context, request *api.StatusRequest) (*api.StatusReply, error) {
	grpclog.Printf("Status request: name: %s", request.Name)

	// Validate application name does not exist.
	if s.AppRuntime[request.Name] != nil {
		reply := api.StatusReply{
			Success: true,
			Status:  "RUNNING",
			Info:    ""}
		return &reply, nil
	} else {
		reply := api.StatusReply{
			Success: true,
			Status:  "STOPPED",
			Info:    ""}
		return &reply, nil
	}

}

func (s ukdServer) Start(context context.Context, request *api.StartRequest) (*api.StartReply, error) {
	grpclog.Printf("Start request: name: %s, Image: %s", request.Name, request.Location)

	// Validate image exists.
	if _, err := os.Stat(request.Location); os.IsNotExist(err) {
		reply := api.StartReply{
			Success: false,
			Ip:      "",
			Info:    "Image " + request.Location + " does not exist, error: " + err.Error()}
		return &reply, nil
	}

	// Validate application name does not exist.
	if s.AppRuntime[request.Name] != nil {
		reply := api.StartReply{
			Success: false,
			Ip:      "",
			Info:    request.Name + " is already running. Please choose a different name for the application if you wish to start a second instance using the same image."}
		return &reply, nil
	}

	var reply *api.StartReply
	if request.Visor == "kvm-qemu" {
		reply, _ = StartQemu(s, request.Name, request.Location)
	} else {
		reply = &api.StartReply{
			Success: false,
			Ip:      "",
			Info:    "Requested hypervisor (" + request.Visor + ") is not yet supported."}
	}
	return reply, nil
}

func (s ukdServer) Stop(context context.Context, request *api.StopRequest) (*api.StopReply, error) {
	grpclog.Printf("Stop request: name: %s", request.Name)
	var success bool
	var info string
	runtime := s.AppRuntime[request.Name]
	if runtime == nil {
		success = true
		info = "App not found. Nothing to do."
	} else {
		runtime.Process.Signal(syscall.SIGTERM) // TODO: check error
		pstate, _ := runtime.Process.Wait()     // TODO: check err
		if pstate.Exited() {
			success = true
			info = "Successfully stopped Application (" + request.Name + ")"
			delete(s.AppRuntime, request.Name)
		} else {
			success = false
			info = pstate.String()
		}
	}
	reply := api.StopReply{
		Success: success,
		Info:    info}
	grpclog.Printf("Stop request")
	return &reply, nil

}

func ComputeSignature(path string) (bool, []byte, string) {
	var success bool
	var info string
	var signature []byte

	if _, err := os.Stat(path); os.IsNotExist(err) {
		success = false
		info = "File not found: " + path
		return success, nil, info
	}

	workDir, _ := ioutil.TempDir("", "ukd-compute-signature-")
	defer os.RemoveAll(workDir)

	// Validate base image signature sent by client matches
	// server base image signature.
	serverImageSignature := workDir + "/serverSignature"
	cmdName := "rdiff"
	args := []string{"signature", path, serverImageSignature}
	cmd := exec.Command(cmdName, args...)
	err := cmd.Run()
	if err != nil {
		success = false
		info = "Failed to compute signature for " + path + ", error: " + err.Error()
		return success, nil, info
	}
	signature, err = ioutil.ReadFile(serverImageSignature)
	success = true
	info = "Successfully computed signature"

	// TODO: delete workDir
	return success, signature, info

}

func (s ukdServer) GetImageSignature(context context.Context, request *api.ImageSignatureRequest) (*api.ImageSignatureReply, error) {
	grpclog.Printf("Signature request: image=%s", request.Path)
	var success bool
	var info string
	var signature []byte

	// TODO: check that image is not currently in use.
	success, signature, info = ComputeSignature(request.Path)

	reply := api.ImageSignatureReply{
		Success:   success,
		Signature: signature,
		Info:      info}
	grpclog.Printf("GetImageSignature request")
	return &reply, nil
}

func ApplyDiff(base string, basesig []byte, diff []byte) (bool, string, string) {
	var success bool
	var info string

	workDir, _ := ioutil.TempDir("", "ukd-update-stage-")

	// Validate base image signature sent by client matches
	// server base image signature.
	serverImageSignature := workDir + "/serverSignature"
	cmdName := "rdiff"
	args := []string{"signature", base, serverImageSignature}
	cmd := exec.Command(cmdName, args...)
	err := cmd.Run()
	serverSignature, err := ioutil.ReadFile(serverImageSignature)
	defer os.RemoveAll(serverImageSignature)
	if !bytes.Equal(serverSignature, basesig) {
		success = false
		info = "Diff was generated for a different base image than " + base
		return success, "", info
	}

	// Write out diff to delta file.
	deltaFile := workDir + "/deltaFile"
	f, err := os.Create(deltaFile)
	if err != nil {
		grpclog.Printf("Failed to create temp file")
		success = false
		info = "Failed to create delta file " + deltaFile + ", error: " + err.Error()
		return success, "", info
	}
	err = ioutil.WriteFile(deltaFile, diff, 0700)
	f.Close()
	defer os.RemoveAll(deltaFile)

	updatedImagePath := workDir + "/newImage.img"
	cmdName = "rdiff"
	args = []string{"patch", base, deltaFile, updatedImagePath}
	cmd = exec.Command(cmdName, args...)
	err = cmd.Run()
	if err != nil {
		success = false
		info = "Failed to patch (" + base + " with " + deltaFile + ", error: " + err.Error()
	} else {
		success = true
		info = "Successfully staged patched image at " + updatedImagePath + ". Please validate the image before replacing master copy."
	}

	return success, updatedImagePath, info
}

func (s ukdServer) UpdateImage(context context.Context, request *api.UpdateImageRequest) (*api.UpdateImageReply, error) {
	grpclog.Printf("Update request: base=%s", request.Base)
	var success bool
	var info string

	// TODO: check that image is not currently in use.
	var newImagePath string
	success, newImagePath, info = ApplyDiff(request.Base, request.Basesig, request.Diff)

	if success {
		var newSignature []byte
		success, newSignature, info = ComputeSignature(newImagePath)
		if !bytes.Equal(newSignature, request.Newsig) {
			success = false
			info = "New image signature on destination does not match new image signature on source"
		} else {
			info = "Verified signature match for new Image on source and destination"
		}
	}

	reply := api.UpdateImageReply{
		Success:         success,
		StagedImagePath: newImagePath,
		Info:            info}
	grpclog.Printf("UpdateImage: success: %t, staged image path: %s, info: %s", success, newImagePath, info)
	return &reply, nil
}

func getPlatformRuntime() (PlatformRuntimeInfo, error) {

	cmdName := "arch"
	args := []string{}
	cmd := exec.Command(cmdName, args...)
	archBytes, err := cmd.Output()
	arch = strings.TrimSpace(string(archBytes))
	platformRuntimeInfo := PlatformRuntimeInfo{platform: arch}
	grpclog.Printf("Detected arch: %s on the system", arch)
	return platformRuntimeInfo, err
}

func NewServer() (*ukdServer, error) {

	var s *ukdServer
	platform, err := getPlatformRuntime()
	if err != nil {
		grpclog.Printf("Failed to detect runtime platform, error: %s", err.Error())
		return s, err
	}

	s = &ukdServer{Version: version_type{Major: 0, Minor: 1},
		PlatformRuntime: platform,
		AppRuntime:      make(map[string]*AppRuntimeInfo)}

	// Image home.
	imagePath := "/var/lib/ukd/images"
	err = os.MkdirAll("/var/lib/ukd/images", 0700)
	if err != nil {
		grpclog.Printf("MkdirAll failed %q: %s", imagePath, err)
	}

	return s, err
}
<|endoftext|>


<commit_before>// +build ignore

package main

import (
	"go/build"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/Masterminds/semver"
	"github.com/sdboyer/gps"
)

// This is probably the simplest possible implementation of gps. It does the
// substantive work that `go get` does, except:
//  1. It drops the resulting tree into vendor instead of GOPATH
//  2. It prefers semver tags (if available) over branches
//  3. It removes any vendor directories nested within dependencies
//
//  This will compile and work...and then blow away any vendor directory present
//  in the cwd. Be careful!
func main() {
	// Assume the current directory is correctly placed on a GOPATH, and that it's the
	// root of the project.
	root, _ := os.Getwd()
	srcprefix := filepath.Join(build.Default.GOPATH, "src") + string(filepath.Separator)
	importroot := filepath.ToSlash(strings.TrimPrefix(root, srcprefix))

	// Set up params, including tracing
	params := gps.SolveParameters{
		RootDir:     root,
		Trace:       true,
		TraceLogger: log.New(os.Stdout, "", 0),
	}
	// Perform static analysis on the current project to find all of its imports.
	params.RootPackageTree, _ = gps.ListPackages(root, importroot)

	// Set up a SourceManager. This manages interaction with sources (repositories).
	sourcemgr, _ := gps.NewSourceManager(NaiveAnalyzer{}, ".repocache")
	defer sourcemgr.Release()

	// Prep and run the solver
	solver, _ := gps.Prepare(params, sourcemgr)
	solution, err := solver.Solve()
	if err == nil {
		// If no failure, blow away the vendor dir and write a new one out,
		// stripping nested vendor directories as we go.
		os.RemoveAll(filepath.Join(root, "vendor"))
		gps.WriteDepTree(filepath.Join(root, "vendor"), solution, sourcemgr, true)
	}
}

type NaiveAnalyzer struct{}

// DeriveManifestAndLock is called when the solver needs manifest/lock data
// for a particular dependency project (identified by the gps.ProjectRoot
// parameter) at a particular version. That version will be checked out in a
// directory rooted at path.
func (a NaiveAnalyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
	return nil, nil, nil
}

// Reports the name and version of the analyzer. This is used internally as part
// of gps' hashing memoization scheme.
func (a NaiveAnalyzer) Info() (name string, version *semver.Version) {
	v, _ := semver.NewVersion("v0.0.1")
	return "example-analyzer", v
}
<commit_msg>Use a tempdir instead of relative dir in example<commit_after>// +build ignore

package main

import (
	"go/build"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/Masterminds/semver"
	"github.com/sdboyer/gps"
)

// This is probably the simplest possible implementation of gps. It does the
// substantive work that `go get` does, except:
//  1. It drops the resulting tree into vendor instead of GOPATH
//  2. It prefers semver tags (if available) over branches
//  3. It removes any vendor directories nested within dependencies
//
//  This will compile and work...and then blow away any vendor directory present
//  in the cwd. Be careful!
func main() {
	// Assume the current directory is correctly placed on a GOPATH, and that it's the
	// root of the project.
	root, _ := os.Getwd()
	srcprefix := filepath.Join(build.Default.GOPATH, "src") + string(filepath.Separator)
	importroot := filepath.ToSlash(strings.TrimPrefix(root, srcprefix))

	// Set up params, including tracing
	params := gps.SolveParameters{
		RootDir:     root,
		Trace:       true,
		TraceLogger: log.New(os.Stdout, "", 0),
	}
	// Perform static analysis on the current project to find all of its imports.
	params.RootPackageTree, _ = gps.ListPackages(root, importroot)

	// Set up a SourceManager. This manages interaction with sources (repositories).
	tempdir, _ := ioutil.TempDir("", "gps-repocache")
	sourcemgr, _ := gps.NewSourceManager(NaiveAnalyzer{}, filepath.Join(tempdir))
	defer sourcemgr.Release()

	// Prep and run the solver
	solver, _ := gps.Prepare(params, sourcemgr)
	solution, err := solver.Solve()
	if err == nil {
		// If no failure, blow away the vendor dir and write a new one out,
		// stripping nested vendor directories as we go.
		os.RemoveAll(filepath.Join(root, "vendor"))
		gps.WriteDepTree(filepath.Join(root, "vendor"), solution, sourcemgr, true)
	}
}

type NaiveAnalyzer struct{}

// DeriveManifestAndLock is called when the solver needs manifest/lock data
// for a particular dependency project (identified by the gps.ProjectRoot
// parameter) at a particular version. That version will be checked out in a
// directory rooted at path.
func (a NaiveAnalyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
	return nil, nil, nil
}

// Reports the name and version of the analyzer. This is used internally as part
// of gps' hashing memoization scheme.
func (a NaiveAnalyzer) Info() (name string, version *semver.Version) {
	v, _ := semver.NewVersion("v0.0.1")
	return "example-analyzer", v
}
<|endoftext|>


<commit_before>// Package server is an interface for a micro server
package server

import (
	"context"
	"os"
	"os/signal"
	"time"

	"github.com/google/uuid"
	"github.com/micro/go-micro/v2/codec"
	"github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/v2/registry"
	signalutil "github.com/micro/go-micro/v2/util/signal"
)

// Server is a simple micro server abstraction
type Server interface {
	Options() Options
	Init(...Option) error
	Handle(Handler) error
	NewHandler(interface{}, ...HandlerOption) Handler
	NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
	Subscribe(Subscriber) error
	Start() error
	Stop() error
	String() string
}

// Router handle serving messages
type Router interface {
	// ProcessMessage processes a message
	ProcessMessage(context.Context, Message) error
	// ServeRequest processes a request to completion
	ServeRequest(context.Context, Request, Response) error
}

// Message is an async message interface
type Message interface {
	// Topic of the message
	Topic() string
	// The decoded payload value
	Payload() interface{}
	// The content type of the payload
	ContentType() string
	// The raw headers of the message
	Header() map[string]string
	// The raw body of the message
	Body() []byte
	// Codec used to decode the message
	Codec() codec.Reader
}

// Request is a synchronous request interface
type Request interface {
	// Service name requested
	Service() string
	// The action requested
	Method() string
	// Endpoint name requested
	Endpoint() string
	// Content type provided
	ContentType() string
	// Header of the request
	Header() map[string]string
	// Body is the initial decoded value
	Body() interface{}
	// Read the undecoded request body
	Read() ([]byte, error)
	// The encoded message stream
	Codec() codec.Reader
	// Indicates whether its a stream
	Stream() bool
}

// Response is the response writer for unencoded messages
type Response interface {
	// Encoded writer
	Codec() codec.Writer
	// Write the header
	WriteHeader(map[string]string)
	// write a response directly to the client
	Write([]byte) error
}

// Stream represents a stream established with a client.
// A stream can be bidirectional which is indicated by the request.
// The last error will be left in Error().
// EOF indicates end of the stream.
type Stream interface {
	Context() context.Context
	Request() Request
	Send(interface{}) error
	Recv(interface{}) error
	Error() error
	Close() error
}

// Handler interface represents a request handler. It's generated
// by passing any type of public concrete object with endpoints into server.NewHandler.
// Most will pass in a struct.
//
// Example:
//
//      type Greeter struct {}
//
//      func (g *Greeter) Hello(context, request, response) error {
//              return nil
//      }
//
type Handler interface {
	Name() string
	Handler() interface{}
	Endpoints() []*registry.Endpoint
	Options() HandlerOptions
}

// Subscriber interface represents a subscription to a given topic using
// a specific subscriber function or object with endpoints.
type Subscriber interface {
	Topic() string
	Subscriber() interface{}
	Endpoints() []*registry.Endpoint
	Options() SubscriberOptions
}

type Option func(*Options)

var (
	DefaultAddress                 = ":0"
	DefaultName                    = "go.micro.server"
	DefaultVersion                 = "latest"
	DefaultId                      = uuid.New().String()
	DefaultServer           Server = newRpcServer()
	DefaultRouter                  = newRpcRouter()
	DefaultRegisterCheck           = func(context.Context) error { return nil }
	DefaultRegisterInterval        = time.Second * 30
	DefaultRegisterTTL             = time.Minute

	// NewServer creates a new server
	NewServer func(...Option) Server = newRpcServer
	log                              = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "server"})
)

// DefaultOptions returns config options for the default service
func DefaultOptions() Options {
	return DefaultServer.Options()
}

// Init initialises the default server with options passed in
func Init(opt ...Option) {
	if DefaultServer == nil {
		DefaultServer = newRpcServer(opt...)
	}
	DefaultServer.Init(opt...)
}

// NewRouter returns a new router
func NewRouter() *router {
	return newRpcRouter()
}

// NewSubscriber creates a new subscriber interface with the given topic
// and handler using the default server
func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber {
	return DefaultServer.NewSubscriber(topic, h, opts...)
}

// NewHandler creates a new handler interface using the default server
// Handlers are required to be a public object with public
// endpoints. Call to a service endpoint such as Foo.Bar expects
// the type:
//
//	type Foo struct {}
//	func (f *Foo) Bar(ctx, req, rsp) error {
//		return nil
//	}
//
func NewHandler(h interface{}, opts ...HandlerOption) Handler {
	return DefaultServer.NewHandler(h, opts...)
}

// Handle registers a handler interface with the default server to
// handle inbound requests
func Handle(h Handler) error {
	return DefaultServer.Handle(h)
}

// Subscribe registers a subscriber interface with the default server
// which subscribes to specified topic with the broker
func Subscribe(s Subscriber) error {
	return DefaultServer.Subscribe(s)
}

// Run starts the default server and waits for a kill
// signal before exiting. Also registers/deregisters the server
func Run() error {
	if err := Start(); err != nil {
		return err
	}

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, signalutil.Shutdown()...)

	if logger.V(logger.InfoLevel, log) {
		log.Infof("Received signal %s", <-ch)
	}
	return Stop()
}

// Start starts the default server
func Start() error {
	config := DefaultServer.Options()
	if logger.V(logger.InfoLevel, log) {
		log.Infof("Starting server %s id %s", config.Name, config.Id)
	}
	return DefaultServer.Start()
}

// Stop stops the default server
func Stop() error {
	if logger.V(logger.InfoLevel, log) {
		log.Infof("Stopping server")
	}
	return DefaultServer.Stop()
}

// String returns name of Server implementation
func String() string {
	return DefaultServer.String()
}
<commit_msg>Update server.go<commit_after>// Package server is an interface for a micro server
package server

import (
	"context"
	"os"
	"os/signal"
	"time"

	"github.com/google/uuid"
	"github.com/micro/go-micro/v2/codec"
	"github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/v2/registry"
	signalutil "github.com/micro/go-micro/v2/util/signal"
)

// Server is a simple micro server abstraction
type Server interface {
	// Initialise options
	Init(...Option) error
	// Retrieve the options
	Options() Options
	// Register a handler
	Handle(Handler) error
	// Create a new handler
	NewHandler(interface{}, ...HandlerOption) Handler
	// Create a new subscriber
	NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
	// Register a subscriber
	Subscribe(Subscriber) error
	// Start the server
	Start() error
	// Stop the server
	Stop() error
	// Server implementation
	String() string
}

// Router handle serving messages
type Router interface {
	// ProcessMessage processes a message
	ProcessMessage(context.Context, Message) error
	// ServeRequest processes a request to completion
	ServeRequest(context.Context, Request, Response) error
}

// Message is an async message interface
type Message interface {
	// Topic of the message
	Topic() string
	// The decoded payload value
	Payload() interface{}
	// The content type of the payload
	ContentType() string
	// The raw headers of the message
	Header() map[string]string
	// The raw body of the message
	Body() []byte
	// Codec used to decode the message
	Codec() codec.Reader
}

// Request is a synchronous request interface
type Request interface {
	// Service name requested
	Service() string
	// The action requested
	Method() string
	// Endpoint name requested
	Endpoint() string
	// Content type provided
	ContentType() string
	// Header of the request
	Header() map[string]string
	// Body is the initial decoded value
	Body() interface{}
	// Read the undecoded request body
	Read() ([]byte, error)
	// The encoded message stream
	Codec() codec.Reader
	// Indicates whether its a stream
	Stream() bool
}

// Response is the response writer for unencoded messages
type Response interface {
	// Encoded writer
	Codec() codec.Writer
	// Write the header
	WriteHeader(map[string]string)
	// write a response directly to the client
	Write([]byte) error
}

// Stream represents a stream established with a client.
// A stream can be bidirectional which is indicated by the request.
// The last error will be left in Error().
// EOF indicates end of the stream.
type Stream interface {
	Context() context.Context
	Request() Request
	Send(interface{}) error
	Recv(interface{}) error
	Error() error
	Close() error
}

// Handler interface represents a request handler. It's generated
// by passing any type of public concrete object with endpoints into server.NewHandler.
// Most will pass in a struct.
//
// Example:
//
//      type Greeter struct {}
//
//      func (g *Greeter) Hello(context, request, response) error {
//              return nil
//      }
//
type Handler interface {
	Name() string
	Handler() interface{}
	Endpoints() []*registry.Endpoint
	Options() HandlerOptions
}

// Subscriber interface represents a subscription to a given topic using
// a specific subscriber function or object with endpoints. It mirrors
// the handler in its behaviour.
type Subscriber interface {
	Topic() string
	Subscriber() interface{}
	Endpoints() []*registry.Endpoint
	Options() SubscriberOptions
}

type Option func(*Options)

var (
	DefaultAddress                 = ":0"
	DefaultName                    = "go.micro.server"
	DefaultVersion                 = "latest"
	DefaultId                      = uuid.New().String()
	DefaultServer           Server = newRpcServer()
	DefaultRouter                  = newRpcRouter()
	DefaultRegisterCheck           = func(context.Context) error { return nil }
	DefaultRegisterInterval        = time.Second * 30
	DefaultRegisterTTL             = time.Minute

	// NewServer creates a new server
	NewServer func(...Option) Server = newRpcServer
	log                              = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "server"})
)

// DefaultOptions returns config options for the default service
func DefaultOptions() Options {
	return DefaultServer.Options()
}

// Init initialises the default server with options passed in
func Init(opt ...Option) {
	if DefaultServer == nil {
		DefaultServer = newRpcServer(opt...)
	}
	DefaultServer.Init(opt...)
}

// NewRouter returns a new router
func NewRouter() *router {
	return newRpcRouter()
}

// NewSubscriber creates a new subscriber interface with the given topic
// and handler using the default server
func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber {
	return DefaultServer.NewSubscriber(topic, h, opts...)
}

// NewHandler creates a new handler interface using the default server
// Handlers are required to be a public object with public
// endpoints. Call to a service endpoint such as Foo.Bar expects
// the type:
//
//	type Foo struct {}
//	func (f *Foo) Bar(ctx, req, rsp) error {
//		return nil
//	}
//
func NewHandler(h interface{}, opts ...HandlerOption) Handler {
	return DefaultServer.NewHandler(h, opts...)
}

// Handle registers a handler interface with the default server to
// handle inbound requests
func Handle(h Handler) error {
	return DefaultServer.Handle(h)
}

// Subscribe registers a subscriber interface with the default server
// which subscribes to specified topic with the broker
func Subscribe(s Subscriber) error {
	return DefaultServer.Subscribe(s)
}

// Run starts the default server and waits for a kill
// signal before exiting. Also registers/deregisters the server
func Run() error {
	if err := Start(); err != nil {
		return err
	}

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, signalutil.Shutdown()...)

	if logger.V(logger.InfoLevel, log) {
		log.Infof("Received signal %s", <-ch)
	}
	return Stop()
}

// Start starts the default server
func Start() error {
	config := DefaultServer.Options()
	if logger.V(logger.InfoLevel, log) {
		log.Infof("Starting server %s id %s", config.Name, config.Id)
	}
	return DefaultServer.Start()
}

// Stop stops the default server
func Stop() error {
	if logger.V(logger.InfoLevel, log) {
		log.Infof("Stopping server")
	}
	return DefaultServer.Stop()
}

// String returns name of Server implementation
func String() string {
	return DefaultServer.String()
}
<|endoftext|>