<commit_before>/*
 *    Copyright (C) 2014 <NAME>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Authors:
 *      <NAME> <<EMAIL>>
 */

// beehive's web-module.
package webbee

import (
	"encoding/json"
	"github.com/hoisie/web"
	"github.com/muesli/beehive/modules"
	"io/ioutil"
	"log"
)

type WebBee struct {
	modules.Module

	addr        string
	path		string

	eventChan chan modules.Event
}

func (mod *WebBee) Run(cin chan modules.Event) {
	mod.eventChan = cin

	web.Get(mod.path, mod.GetRequest)
	web.Post(mod.path, mod.PostRequest)

	web.Run(mod.addr)
}

func (mod *WebBee) Action(action modules.Action) []modules.Placeholder {
	outs := []modules.Placeholder{}

	switch action.Name {
	case "post":
/*		json := ""

		for _, opt := range action.Options {
			if opt.Name == "json" {
				json = opt.Value.(string)
			}
		}*/

		//FIXME: do stuff

	default:
		panic("Unknown action triggered in " +mod.Name()+": "+action.Name)
	}

	return outs
}

func (mod *WebBee) GetRequest(ctx *web.Context) {
	ev := modules.Event{
		Bee:  mod.Name(),
		Name: "get",
		Options: []modules.Placeholder{
			modules.Placeholder{
				Name:  "ip",
				Type:  "string",
				Value: "tbd",
			},
		},
	}

	for k, v := range ctx.Params {
		log.Println("GET param:", k, "=", v)

		ph := modules.Placeholder{
			Name: k,
			Type: "string",
			Value: v,
		}
		ev.Options = append(ev.Options, ph)
	}

	mod.eventChan <- ev
}

func (mod *WebBee) PostRequest(ctx *web.Context) {
	b, err := ioutil.ReadAll(ctx.Request.Body)
	if err != nil {
		log.Println("Error:", err)
		return
	}

	var payload interface{}
	err = json.Unmarshal(b, &payload)
	if err != nil {
		log.Println("Error:", err)
		return
	}

	ev := modules.Event{
		Bee:  mod.Name(),
		Name: "post",
		Options: []modules.Placeholder{
			modules.Placeholder{
				Name:  "json",
				Type:  "map",
				Value: payload,
			},
			modules.Placeholder{
				Name:  "ip",
				Type:  "string",
				Value: "tbd",
			},
		},
	}

	j := make(map[string]interface{})
	body, err := ioutil.ReadAll(ctx.Request.Body)
	if err != nil {
		log.Println("Error:", err)
	  return
	}
	err = json.Unmarshal(body, &j)
	if err != nil {
		log.Println("Error:", err)
	  return
	}

	for k, v := range j {
		log.Printf("POST JSON param: %s = %+v\n", k, v)

		ph := modules.Placeholder{
			Name: k,
			Type: "string",
			Value: v,
		}
		ev.Options = append(ev.Options, ph)
	}

	mod.eventChan <- ev
}
<commit_msg>* Body has already been read.<commit_after>/*
 *    Copyright (C) 2014 <NAME>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Authors:
 *      <NAME> <<EMAIL>>
 */

// beehive's web-module.
package webbee

import (
	"encoding/json"
	"github.com/hoisie/web"
	"github.com/muesli/beehive/modules"
	"io/ioutil"
	"log"
)

type WebBee struct {
	modules.Module

	addr        string
	path		string

	eventChan chan modules.Event
}

func (mod *WebBee) Run(cin chan modules.Event) {
	mod.eventChan = cin

	web.Get(mod.path, mod.GetRequest)
	web.Post(mod.path, mod.PostRequest)

	web.Run(mod.addr)
}

func (mod *WebBee) Action(action modules.Action) []modules.Placeholder {
	outs := []modules.Placeholder{}

	switch action.Name {
	case "post":
/*		json := ""

		for _, opt := range action.Options {
			if opt.Name == "json" {
				json = opt.Value.(string)
			}
		}*/

		//FIXME: do stuff

	default:
		panic("Unknown action triggered in " +mod.Name()+": "+action.Name)
	}

	return outs
}

func (mod *WebBee) GetRequest(ctx *web.Context) {
	ev := modules.Event{
		Bee:  mod.Name(),
		Name: "get",
		Options: []modules.Placeholder{
			modules.Placeholder{
				Name:  "ip",
				Type:  "string",
				Value: "tbd",
			},
		},
	}

	for k, v := range ctx.Params {
		log.Println("GET param:", k, "=", v)

		ph := modules.Placeholder{
			Name: k,
			Type: "string",
			Value: v,
		}
		ev.Options = append(ev.Options, ph)
	}

	mod.eventChan <- ev
}

func (mod *WebBee) PostRequest(ctx *web.Context) {
	b, err := ioutil.ReadAll(ctx.Request.Body)
	if err != nil {
		log.Println("Error:", err)
		return
	}

	var payload interface{}
	err = json.Unmarshal(b, &payload)
	if err != nil {
		log.Println("Error:", err)
		return
	}

	ev := modules.Event{
		Bee:  mod.Name(),
		Name: "post",
		Options: []modules.Placeholder{
			modules.Placeholder{
				Name:  "json",
				Type:  "map",
				Value: payload,
			},
			modules.Placeholder{
				Name:  "ip",
				Type:  "string",
				Value: "tbd",
			},
		},
	}

	j := make(map[string]interface{})
	err = json.Unmarshal(b, &j)
	if err != nil {
		log.Println("Error:", err)
	  return
	}

	for k, v := range j {
		log.Printf("POST JSON param: %s = %+v\n", k, v)

		ph := modules.Placeholder{
			Name: k,
			Type: "string",
			Value: v,
		}
		ev.Options = append(ev.Options, ph)
	}

	mod.eventChan <- ev
}

{'fraction_non_alphanumeric': 0.14611947708092007, 'fraction_numerical': 0.0016548072149594572, 'mean_word_length': 2.929778933680104, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2939295', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2154, 'n_words': 622}