Exemple #1
0
// Dispatch the encapsulated request to the according handler methods
// depending on the HTTP method.
func dispatch(ctx *Context, h ResourceHandler) bool {
	defer func() {
		if err := recover(); err != nil {
			// Shit happens! TODO: Better error handling.
			msg := fmt.Sprintf("internal server error: '%v' in context: '%v'", err, ctx)
			applog.Criticalf(msg)
			http.Error(ctx.ResponseWriter, msg, http.StatusInternalServerError)
		}
	}()

	applog.Infof("dispatching %s", ctx)
	switch ctx.Request.Method {
	case "GET":
		return h.Get(ctx)
	case "PUT":
		if ph, ok := h.(PutResourceHandler); ok {
			return ph.Put(ctx)
		}
	case "POST":
		if ph, ok := h.(PostResourceHandler); ok {
			return ph.Post(ctx)
		}
	case "DELETE":
		if dh, ok := h.(DeleteResourceHandler); ok {
			return dh.Delete(ctx)
		}
	}
	applog.Errorf("method not allowed: %s", ctx)
	http.Error(ctx.ResponseWriter, "405 method not allowed", http.StatusMethodNotAllowed)
	return false
}
Exemple #2
0
func Serialize(data interface{}) []byte {
	out, err := json.Marshal(data)
	if err != nil {
		applog.Criticalf("Error serializing data: %s", err)
		panic(err)
	}
	return out
}
Exemple #3
0
// Test logging from level warning and above.
func TestWarningAndAbove(t *testing.T) {
	applog.SetLevel(applog.LevelWarning)

	applog.Debugf("Debug.")
	applog.Infof("Info.")
	applog.Warningf("Warning.")
	applog.Errorf("Error.")
	applog.Criticalf("Critical.")
}
Exemple #4
0
// Test log at all levels.
func TestAllLevels(t *testing.T) {
	applog.SetLevel(applog.LevelDebug)

	applog.Debugf("Debug.")
	applog.Infof("Info.")
	applog.Warningf("Warning.")
	applog.Errorf("Error.")
	applog.Criticalf("Critical.")
}
Exemple #5
0
func (c *Client) read() {
	for packet := range c.incoming {
		applog.Debugf("Received packet from client: %s", packet)
		packetStruct, err := parsePacket(packet)
		if err != nil {
			applog.Criticalf("Error Unmarshalling message. %s", err)
			continue
		}
		(*packetStruct).Handle(world, c.player, c)
	}
}
Exemple #6
0
// Test logging with the go logger.
func TestGoLogger(t *testing.T) {
	log.SetOutput(os.Stdout)

	applog.SetLevel(applog.LevelDebug)
	applog.SetLogger(applog.GoLogger{})

	applog.Debugf("Debug.")
	applog.Infof("Info.")
	applog.Warningf("Warning.")
	applog.Errorf("Error.")
	applog.Criticalf("Critical.")
}
Exemple #7
0
// Test logging with an own logger.
func TestOwnLogger(t *testing.T) {
	assert := asserts.NewTestingAsserts(t, true)
	ownLogger := &testLogger{[]string{}}

	applog.SetLevel(applog.LevelDebug)
	applog.SetLogger(ownLogger)

	applog.Debugf("Debug.")
	applog.Infof("Info.")
	applog.Warningf("Warning.")
	applog.Errorf("Error.")
	applog.Criticalf("Critical.")

	assert.Length(ownLogger.logs, 5, "Everything logged.")
}