Пример #1
0
	_ "github.com/mattn/go-sqlite3"
	"github.com/spacemonkeygo/errors"
	"github.com/spacemonkeygo/errors/errhttp"
)

var (
	dbType = flag.String("db", "postgres", "the database type to use. "+
		"can be postgres or sqlite3")
	dbConn = flag.String("db.conn",
		"user=cwbench database=cwbench password=password",
		"the database connection string")
	searchParallelism = flag.Int("parallelism", runtime.NumCPU()+1,
		"number of parellel search queries to execute")

	Err         = errors.NewClass("error")
	ErrNotFound = Err.NewClass("not found", errhttp.SetStatusCode(404))
	ErrDenied   = Err.NewClass("denied", errhttp.SetStatusCode(405))
	ErrBadDims  = Err.NewClass("wrong dimensions", errhttp.SetStatusCode(400))
)

type Data struct {
	db gorm.DB
}

func NewData() (*Data, error) {
	db, err := gorm.Open(*dbType, *dbConn)
	if err != nil {
		return nil, Err.Wrap(err)
	}
	return &Data{db: db}, nil
}
Пример #2
0
// Copyright (C) 2016 JT Olds
// See LICENSE for copying information

package webhelp

import (
	"net/http"

	"github.com/spacemonkeygo/errors"
	"github.com/spacemonkeygo/errors/errhttp"
)

var (
	HTTPError     = errors.NewClass("HTTP Error", errors.NoCaptureStack())
	ErrBadRequest = HTTPError.NewClass("Bad request",
		errhttp.SetStatusCode(http.StatusBadRequest))
	ErrNotFound = ErrBadRequest.NewClass("Not found",
		errhttp.SetStatusCode(http.StatusNotFound))
	ErrMethodNotAllowed = ErrBadRequest.NewClass("Method not allowed",
		errhttp.SetStatusCode(http.StatusMethodNotAllowed))
	ErrInternalServerError = HTTPError.NewClass("Internal server error",
		errhttp.SetStatusCode(http.StatusInternalServerError))
	ErrUnauthorized = HTTPError.NewClass("Unauthorized",
		errhttp.SetStatusCode(http.StatusUnauthorized))
)

func Redirect(w ResponseWriter, r *http.Request, redirectTo string) error {
	http.Redirect(w, r, redirectTo, http.StatusSeeOther)
	return nil
}