Exemplo n.º 1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	logger := log.LoggerFor("taskboad")

	defer func() {
		err := recover()
		if err != nil {
			logger.Errorf("%s\n%s\n", err, debug.Stack())
		}
		// give time for the loggers to write
		time.Sleep(100 * time.Millisecond)
	}()

	var app = App{}
	app.ContextFactory = impl.ContextFactory
	app.Limit = impl.Limit
	app.AuthenticationFilter = impl.AuthenticationFilter
	app.ResponseBuffer = maze.ResponseBuffer
	app.TransactionFilter = impl.TransactionFilter
	app.LoginFilter = impl.LoginFilter
	app.PingFilter = impl.PingFilter
	app.Poll = impl.Poll
	app.IpPort = impl.IpPort

	appCtx := impl.NewAppCtx(nil, nil, nil, impl.TaskBoardService)
	// service factory
	app.JsonRpc = appCtx.BuildJsonRpcTaskBoardService(app.TransactionFilter)

	app.ContentDir = impl.ContentDir

	app.Start()
}
Exemplo n.º 2
0
func (app *App) Start() {
	startTime := time.Now()

	logger := log.LoggerFor("taskboad")

	// Filters will be executed in order
	fh := maze.NewMaze(app.ContextFactory)

	// limits size
	fh.Push("/*", app.Limit)
	// security
	fh.Push("/rest/*", app.AuthenticationFilter, app.ResponseBuffer)
	// json services will be the most used so, they are at the front
	fh.Add(app.JsonRpc.Build("/rest/taskboard")...)

	fh.Push("/login", app.ResponseBuffer, app.TransactionFilter, app.LoginFilter)
	// this endponint is called as the expiration time of the token approaches
	fh.Push("/ping", app.ResponseBuffer, app.AuthenticationFilter, app.TransactionFilter, app.PingFilter)

	// delivering static content and preventing malicious access
	fh.Static("/*", app.ContentDir)

	http.Handle("/", fh)
	http.Handle("/feed", app.Poll)

	logger.Infof("Listening at %s", app.IpPort)
	logger.Debugf("Started in %f secs", time.Since(startTime).Seconds())
	if err := http.ListenAndServe(app.IpPort, nil); err != nil {
		panic(err)
	}
}
Exemplo n.º 3
0
Arquivo: Db.go Projeto: quintans/goSQL
package db

import (
	"errors"
	"fmt"
	"reflect"

	"github.com/quintans/goSQL/dbx"
	. "github.com/quintans/toolkit/ext"
	"github.com/quintans/toolkit/log"
)

var logger = log.LoggerFor("github.com/quintans/goSQL/db")

var OPTIMISTIC_LOCK_MSG = "No update was possible for this version of the data. Data may have changed."
var VERSION_SET_MSG = "Unable to set Version data."

const (
	sqlOmitionKey = "sql"
	sqlOmitionVal = "omit"
)

// Interface that a struct must implement to inform what columns where changed
type Markable interface {
	// Retrive property names that were changed
	//
	// return names of the changed properties
	Marks() map[string]bool
	Unmark()
}
Exemplo n.º 4
0
func init() {
	TaskBoardService = new(TaskBoardServiceImpl)

	c, err := goconfig.ReadConfigFile("taskboard.ini")
	if err != nil {
		panic(err)
	}
	/*
	 * =======
	 * WEB
	 * =======
	 */
	ContentDir, _ = c.GetString("web", "dir")
	ContentDir = replaceOsEnv(ContentDir)
	fmt.Println("[web]dir=", ContentDir)
	IpPort, _ = c.GetString("web", "ip_port")
	IpPort = replaceOsEnv(IpPort)
	fmt.Println("[web]ip_port=", IpPort)
	HttpsOnly, _ = c.GetBool("web", "https_only")
	fmt.Println("[web]https_only=", HttpsOnly)
	postLimit, _ = c.GetInt64("web", "post_limit")
	fmt.Println("[web]post_limit=", postLimit)

	/*
	 * =======
	 * LOG
	 * =======
	 */
	//// log configuration - its the same the default
	level, _ := c.GetString("log", "level")
	level = replaceOsEnv(level)
	fmt.Println("[log]level=", level)
	file, _ := c.GetString("log", "file")
	file = replaceOsEnv(file)
	fmt.Println("[log]file=", file)
	count, _ := c.GetInt64("log", "count")
	fmt.Println("[log]count=", count)
	size, _ := c.GetInt64("log", "size")
	fmt.Println("[log]size=", size)
	logToConsole, _ := c.GetBool("log", "console")
	fmt.Println("[log]console=", logToConsole)

	logLevel := log.ParseLevel(level, log.ERROR)
	if logLevel <= log.INFO {
		log.ShowCaller(true)
	}

	// setting root writers
	writers := make([]log.LogWriter, 0)
	writers = append(writers, log.NewRollingFileAppender(file, size, int(count), true))
	if logToConsole {
		writers = append(writers, log.NewConsoleAppender(false))
	}
	log.Register("/", logLevel, writers...)

	//log.Register("/", logLevel, log.NewRollingFileAppender(file, size, int(count), true))

	//master.SetLevel("pqp", log.LogLevel(level))
	logger = log.LoggerFor("taskboad/biz/impl")

	// smtp
	SmtpHost, _ = c.GetString("smtp", "host")
	SmtpHost = replaceOsEnv(SmtpHost)
	fmt.Println("[smtp]host=", SmtpHost)
	SmtpPort, _ = c.GetString("smtp", "port")
	SmtpPort = replaceOsEnv(SmtpPort)
	fmt.Println("[smtp]port=", SmtpPort)
	SmtpUser, _ = c.GetString("smtp", "user")
	SmtpUser = replaceOsEnv(SmtpUser)
	fmt.Println("[smtp]user="******"smtp", "pass")
	SmtpPass = replaceOsEnv(SmtpPass)
	SmtpFrom, _ = c.GetString("smtp", "from")
	SmtpFrom = replaceOsEnv(SmtpFrom)
	fmt.Println("[smtp]from=", SmtpFrom)

	/*
	 * =======================
	 * BEGIN DATABASE CONFIG
	 * =======================
	 */
	// database configuration
	driverName, _ := c.GetString("database", "driver_name")
	driverName = replaceOsEnv(driverName)
	fmt.Println("[database]driver_name=", driverName)
	dataSourceName, _ := c.GetString("database", "data_source_name")
	dataSourceName = replaceOsEnv(dataSourceName)
	fmt.Println("[database]data_source_name=", dataSourceName)
	statementCache, _ := c.GetInt64("database", "statement_cache")
	fmt.Println("[database]statement_cache=", statementCache)
	idle, _ := c.GetInt64("database", "idle_connections")
	fmt.Println("[database]idle_connections=", idle)

	appDB, err := sql.Open(driverName, dataSourceName)
	if err != nil {
		panic(err)
	}

	if idle > 0 {
		appDB.SetMaxIdleConns(int(idle))
	}

	// wake up the database pool
	err = appDB.Ping()
	if err != nil {
		panic(err)
	}

	var translator = trx.NewMySQL5Translator()
	TM = db.NewTransactionManager(
		// database
		appDB,
		// databse context factory - called for each transaction
		func(inTx *bool, c dbx.IConnection) db.IDb {
			return db.NewDb(inTx, c, translator)
		},
		// statement cache
		int(statementCache),
	)
	/*
	 * =======================
	 * END DATABASE CONFIG
	 * =======================
	 */

	Poll = poller.NewPoller(30 * time.Second)
}
Exemplo n.º 5
0
package picodi

import (
	"errors"
	"fmt"
	"reflect"
	"strings"

	"github.com/quintans/toolkit/log"
)

var logger = log.LoggerFor("github.com/quintans/picodi")

// IWire is an interface for any implementation that has to implement wiring.
//
// With the advent of plugins (Go 1.8), DI might be usefull.
// We could ask the plugin to wire itself with the supplied Depency PicoDI.
// This way context could evolve independently in the main program and in the plugins.
type IWire interface {
	Wire(interface{}) error
	Get(string) (interface{}, error)
	Set(string, func() interface{})
	SetValue(string, interface{})
}

// PicoDI is a small framework for Dependency Injection.
// With this we can concentrate all the configuration in one place and avoid the import cycles problem.
// I also see it being useful in plugins.
type PicoDI struct {
	providers map[string]func() interface{}
	instances map[string]interface{}
Exemplo n.º 6
0
package collections

import (
	. "github.com/quintans/toolkit"
	"github.com/quintans/toolkit/log"
)

var logger = log.LoggerFor("github.com/quintans/toolkit/collection")

type Collection interface {
	Base

	Size() int
	Clear()
	Contains(value interface{}) bool
	Add(data ...interface{}) bool
	Delete(key interface{}) bool

	Enumerator() Enumerator
	Elements() []interface{}
	AsSlice() interface{} // returns elements in an array. ex: []int
	Sort(greater func(a, b interface{}) bool) []interface{}
}

type IList interface {
	Collection

	Get(pos int) interface{}
	Set(pos int, value interface{})
	Find(value interface{}) (int, interface{})
	DeleteAt(pos int) bool
Exemplo n.º 7
0
package web

import (
	"github.com/quintans/toolkit/cache"
	"github.com/quintans/toolkit/log"

	"crypto/rand"
	"encoding/base64"
	"net/http"
	"time"
)

var logger = log.LoggerFor("github.com/quintans/toolkit/web")

const (
	COOKIE_NAME = "GSESSION"
)

type ISession interface {
	GetId() string
	Invalidate()
	IsInvalid() bool
	Get(key interface{}) interface{}
	Delete(key interface{})
	Put(key interface{}, value interface{})
}

func NewSession() *Session {
	this := new(Session)
	this.Init()
	return this
Exemplo n.º 8
0
import (
	"reflect"

	"github.com/quintans/goSQL/db"
	"github.com/quintans/goSQL/dbx"
	tk "github.com/quintans/toolkit"
	coll "github.com/quintans/toolkit/collection"
	. "github.com/quintans/toolkit/ext"
	"github.com/quintans/toolkit/log"

	"strings"
)

const NOT_DELETED int64 = 0

var logger = log.LoggerFor("pqp/toolkit/app")

// the result of the query is put in the passed struct.
// returns true if a result was found, false if no result
func FindById(DB db.IDb, table *db.Table, instance interface{}, id int64) (bool, error) {
	logger.CallerAt(1).Debugf("DAOUtils.FindById: %v", id)

	keyColumn := table.GetKeyColumns().Enumerator().Next().(*db.Column)

	return DB.Query(table).
		All().
		Where(keyColumn.Matches(id)).
		SelectTo(instance)
}

func FindAll(DB db.IDb, table *db.Table, instance interface{}) (coll.Collection, error) {