Пример #1
0
func (p *PanicHandler) Err500(ex interface{}, ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	renderer := render.FromContext(ctx)

	data := make(map[string]interface{})
	data["Title"] = "eek fire FIRE"
	data["Nav"] = -1
	data["environment"] = runMode
	data["authenticated"] = cas.Username(r)
	data["copyright"] = time.Now().Year()
	data["error"] = fmt.Sprintf("%v", ex)

	stackTrace := errors.Wrap(ex, 4).ErrorStack()

	klog.Critical("err  (%v): Internal Server Error: %v", r.RemoteAddr, ex)
	klog.Critical("err  (%v): Stacktrace: %v", r.RemoteAddr, stackTrace)

	if mode := conf.GetDefault("runMode", "dev").(string); mode == "dev" {
		// dump the stacktrace out on the page too
		data["stacktrace"] = stackTrace
	}

	renderer.HTML(rw, 500, "errors/500", data)
}
Пример #2
0
	DataHTML
	DataBinary
	DataText
	DataJSON
	DataJSONP
	DataXML
)

const (
	FlashErr  = "_flash_err"
	FlashWarn = "_flash_warn"
	FlashInfo = "_flash_info"
)

var (
	runMode = conf.GetDefault("runMode", "dev").(string)
)

type Render struct {
	Type     int
	Status   int
	Template string // for DataHTML
	Callback string // for DataJSONP
	Data     interface{}
}

func FromContext(ctx context.Context) *Render {
	r, ok := ctx.Value(datakey).(*Render)
	if !ok {
		panic("unable to retrieve render struct from context")
	}
Пример #3
0
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/jinzhu/gorm"
	"github.com/robxu9/kahinah/common/conf"
	"github.com/robxu9/kahinah/common/klog"
	"github.com/robxu9/kahinah/common/set"
	"github.com/robxu9/kahinah/models"
	"menteslibres.net/gosexy/dig"
	"menteslibres.net/gosexy/to"
)

var (
	abfEnable        = conf.GetDefault("integration.abf.enable", false).(bool)
	abfURL           = conf.GetDefault("integration.abf.host", "https://abf.io").(string)
	abfUser          = conf.Get("integration.abf.user").(string)
	abfAPIKey        = conf.Get("integration.abf.apiKey").(string)
	abfPlatforms     = conf.Get("integration.abf.readPlatforms").([]interface{})
	abfArchWhitelist = conf.Get("integration.abf.archWhitelist").([]interface{})
	abfGitDiff       = conf.Get("integration.abf.gitDiff").(bool)
	abfGitDiffSSH    = conf.Get("integration.abf.gitDiffSSH").(bool)

	abfPlatformsSet     *set.Set
	abfArchWhitelistSet *set.Set
	abfClient           *http.Client
)

func init() {
	abfPlatformsSet = set.NewSet()
Пример #4
0
func main() {
	klog.Info("starting kahinah v4")

	// -- mux -----------------------------------------------------------------
	mux := goji.NewMux()

	// -- middleware ----------------------------------------------------------

	// logging middleware (base middleware)
	mux.UseC(func(inner goji.Handler) goji.Handler {
		return goji.HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
			klog.Debugf("req  (%v): path (%v), user-agent (%v), referrer (%v)", r.RemoteAddr, r.RequestURI, r.UserAgent(), r.Referer())
			wp := mutil.WrapWriter(rw) // proxy the rw for info later
			inner.ServeHTTPC(ctx, wp, r)
			klog.Debugf("resp (%v): status (%v), bytes written (%v)", r.RemoteAddr, wp.Status(), wp.BytesWritten())
		})
	})

	// rendering middleware (required by panic)
	renderer := urender.New(urender.Options{
		Directory:  "views",
		Layout:     "layout",
		Extensions: []string{".tmpl", ".tpl"},
		Funcs: []template.FuncMap{
			template.FuncMap{
				"rfc3339": func(t time.Time) string {
					return t.Format(time.RFC3339)
				},
				"since": func(t time.Time) string {
					hrs := time.Since(t).Hours()
					return fmt.Sprintf("%dd %02dhrs", int(hrs)/24, int(hrs)%24)
				},
				"emailat": func(s string) string {
					return strings.Replace(s, "@", " [@T] ", -1)
				},
				"mapaccess": func(s interface{}, m map[string]string) string {
					return m[to.String(s)]
				},
				"url":     render.ConvertURL,
				"urldata": render.ConvertURLWithData,
			},
		},
		IndentJSON:    true,
		IndentXML:     true,
		IsDevelopment: conf.GetDefault("runMode", "dev").(string) == "dev",
	})

	mux.UseC(func(inner goji.Handler) goji.Handler {
		return goji.HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
			newCtx := render.NewContext(ctx, renderer)
			inner.ServeHTTPC(newCtx, rw, r)
		})
	})

	// panic middleware
	mux.UseC(controllers.PanicMiddleware)

	// not found middleware
	mux.UseC(func(inner goji.Handler) goji.Handler {
		return goji.HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
			routeFound := middleware.Pattern(ctx)

			if routeFound != nil {
				inner.ServeHTTPC(ctx, rw, r)
				return
			}

			panic(controllers.ErrNotFound)
		})
	})

	// authentication (cas) middleware
	if enable, ok := conf.GetDefault("authentication.cas.enable", false).(bool); ok && enable {
		url, _ := url.Parse(conf.Get("authentication.cas.url").(string))

		casClient := cas.NewClient(&cas.Options{
			URL: url,
		})

		mux.Use(casClient.Handle)
	}

	// sessions middleware
	sessionConfig := &sessionmw.Config{
		Secret:      []byte(securecookie.GenerateRandomKey(64)),
		BlockSecret: []byte(securecookie.GenerateRandomKey(32)),
		Store:       kv.NewMemStore(),
		Name:        "kahinah",
	}
	mux.UseC(sessionConfig.Handler)

	// csrf middleware
	mux.UseC(csrf.Protect(securecookie.GenerateRandomKey(64), csrf.Secure(false)))

	// data rendering middleware
	mux.UseC(func(inner goji.Handler) goji.Handler {
		return goji.HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
			newCtx := data.RenderMiddleware(ctx, rw, r)
			inner.ServeHTTPC(newCtx, rw, r)
			data.RenderAfterware(newCtx, rw, r)
		})
	})

	// --------------------------------------------------------------------
	// HANDLERS
	// --------------------------------------------------------------------

	getHandlers := map[string]goji.HandlerFunc{

		// static paths
		"/static/*": controllers.StaticHandler,

		// main page
		"/": controllers.MainHandler,

		// builds - json
		"/i/list/json/": controllers.ListsAPIHandler,

		// builds
		"/i/list/": controllers.ListsHandler,

		// build - specific
		"/b/:id/": controllers.BuildGetHandler,

		// build - specific - json
		"/b/:id/json/": controllers.BuildGetJSONHandler,

		// activity - json
		"/i/activity/json/": controllers.ActivityJSONHandler,

		// activity - html
		"/i/activity/": controllers.ActivityHandler,

		// admin
		"/admin/": controllers.AdminGetHandler,

		// authentication - login
		"/u/login/": controllers.UserLoginHandler,

		// authentication - logout
		"/u/logout/": controllers.UserLogoutHandler,
	}

	postHandlers := map[string]goji.HandlerFunc{

		// webhooks
		"/hook/*": controllers.IntegrationHandler,

		// build - specific
		"/b/:id/": controllers.BuildPostHandler,

		// admin
		"/admin/": controllers.AdminPostHandler,
	}

	for k, v := range getHandlers {
		if len(k) > 1 && strings.HasSuffix(k, "/") {
			getHandlerRedirectName := render.ConvertURLRelative(k[:len(k)-1])
			klog.Debugf("get handler setup: redirecting %v", getHandlerRedirectName)
			mux.HandleFunc(pat.Get(getHandlerRedirectName), controllers.RedirectHandler)
		}
		getHandlerUseName := render.ConvertURLRelative(k)
		klog.Debugf("get handler setup: using %v", getHandlerUseName)
		mux.HandleC(pat.Get(getHandlerUseName), v)
	}

	for k, v := range postHandlers {
		if len(k) > 1 && strings.HasSuffix(k, "/") {
			postHandlerRedirectName := render.ConvertURLRelative(k[:len(k)-1])
			klog.Debugf("post handler setup: redirecting %v", postHandlerRedirectName)
			mux.HandleFunc(pat.Post(postHandlerRedirectName), controllers.RedirectHandler)
		}
		postHandlerUseName := render.ConvertURLRelative(k)
		klog.Debugf("post handler setup: using %v", postHandlerUseName)
		mux.HandleC(pat.Post(postHandlerUseName), v)
	}

	// -- cronjobs ----------------------------------------------------------

	cronRunner := cron.New()

	// integration polling
	if pollRate, ok := conf.Get("integration.poll").(string); ok && pollRate != "" {
		pollFunc := func() {
			pollAllErr := integration.PollAll()
			for name, err := range pollAllErr {
				klog.Warningf("integration polling failed for %v: %v", name, err)
			}
		}
		cronRunner.AddFunc(pollRate, pollFunc)

		// and do an initial poll
		pollFunc()
	}

	// process new stages/check processes every 10 seconds
	cronRunner.AddFunc("@every 10s", func() {
		models.CheckAllListStages()
	})

	// run the job scheduler every minute
	cronRunner.AddFunc("@every 1m", func() {
		job.ProcessQueue()
	})

	// start cron
	cronRunner.Start()

	// -- server setup --------------------------------------------------------

	// bind and listen
	listenAddr := conf.GetDefault("listenAddr", "0.0.0.0").(string)
	listenPort := conf.GetDefault("listenPort", 3000).(int64)

	klog.Infof("listening to %v:%v", listenAddr, listenPort)
	if err := http.ListenAndServe(fmt.Sprintf("%v:%v", listenAddr, listenPort), mux); err != nil {
		klog.Fatalf("unable to serve: %v", err)
	}

	cronRunner.Stop()

	klog.Info("processing leftover jobs...")
	close(job.Queue)
	for len(job.Queue) > 0 {
		job.ProcessQueue()
	}
}
Пример #5
0
import (
	"fmt"

	// mysql functionality
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	// postgresql funcationality
	_ "github.com/lib/pq"
	// sqlite3 functionality
	_ "github.com/mattn/go-sqlite3"
	"github.com/robxu9/kahinah/common/conf"
	"github.com/robxu9/kahinah/common/klog"
)

var (
	DBPrefix = conf.GetDefault("database.prefix", "kh_").(string)
	DBType   = conf.GetDefault("database.type", "sqlite3").(string)
	DBName   = conf.GetDefault("database.name", "data.sqlite").(string)
	DBHost   = conf.GetDefault("database.host", "localhost:3306").(string)
	DBUser   = conf.GetDefault("database.user", "root").(string)
	DBPass   = conf.GetDefault("database.pass", "toor").(string)
	DBDebug  = conf.GetDefault("database.debug", false).(bool)

	DB *gorm.DB
)

func init() {
	var err error
	switch DBType {
	case "mysql":
		DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", DBUser, DBPass, DBHost, DBName))