Beispiel #1
0
func TestGetenv(t *testing.T) {
	func() {
		actual := kocha.Getenv("TEST_KOCHA_ENV", "default value")
		expected := "default value"
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Getenv(%q, %q) => %q, want %q", "TEST_KOCHA_ENV", "default value", actual, expected)
		}

		actual = os.Getenv("TEST_KOCHA_ENV")
		expected = "default value"
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("os.Getenv(%q) => %q, want %q", "TEST_KOCHA_ENV", actual, expected)
		}
	}()

	func() {
		os.Setenv("TEST_KOCHA_ENV", "set kocha env")
		defer os.Clearenv()
		actual := kocha.Getenv("TEST_KOCHA_ENV", "default value")
		expected := "set kocha env"
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("Getenv(%q, %q) => %q, want %q", "TEST_KOCHA_ENV", "default value", actual, expected)
		}

		actual = os.Getenv("TEST_KOCHA_ENV")
		expected = "set kocha env"
		if !reflect.DeepEqual(actual, expected) {
			t.Errorf("os.Getenv(%q) => %q, want %q", "TEST_KOCHA_ENV", actual, expected)
		}
	}()
}
Beispiel #2
0
func GetClient() *oauth.Consumer {
	return oauth.NewConsumer(
		kocha.Getenv("RETWITTER_TWITTER_CONSUMER_KEY", ""),
		kocha.Getenv("RETWITTER_TWITTER_CONSUMER_SECRET", ""),
		oauth.ServiceProvider{
			AuthorizeTokenUrl: "https://api.twitter.com/oauth/authorize",
			RequestTokenUrl:   "https://api.twitter.com/oauth/request_token",
			AccessTokenUrl:    "https://api.twitter.com/oauth/access_token",
		},
	)
}
Beispiel #3
0
func (m *AuthMiddleware) Process(app *kocha.Application, c *kocha.Context, next func() error) (err error) {
	if kocha.Getenv("RETWITTER_AUTH", "0") == "1" && !checkAuth(c.Request) {
		c.Response.StatusCode = http.StatusUnauthorized
		c.Response.Header().Set("WWW-Authenticate", `Basic realm="Input ID and Password"`)
		return c.RenderError(http.StatusUnauthorized, nil, nil)
	}
	return next()
}
Beispiel #4
0
func (au *Auth) GET(c *kocha.Context) error {
	client := twitter.GetClient()
	callbackUrl := kocha.Getenv("RETWITTER_TWITTER_CALLBACK", "")
	requestToken, url, err := client.GetRequestTokenAndUrl(callbackUrl)
	if err != nil {
		return c.RenderError(500, err, nil)
	}

	c.Session.Set("requestToken.Token", requestToken.Token)
	c.Session.Set("requestToken.Secret", requestToken.Secret)
	return c.Redirect(url, false)
}
Beispiel #5
0
// Buffer によってツイートされた最後のツイートを取得します。
func getLastTweet() *model.Tweet {
	accessToken := kocha.Getenv("RETWITTER_BUFFER_ACCESS_TOKEN", "")
	client := buffer.NewClient(buffer.GetOauth2Client(accessToken))
	profileId := kocha.Getenv("RETWITTER_BUFFER_TWITTER_PROFILE_ID", "")
	options := &buffer.UpdateListOptions{Count: 1}
	result, _, err := client.UpdateService.GetSentUpdates(profileId, options)
	if err != nil {
		panic(err)
	}

	if len(result.Updates) == 0 {
		return nil
	}

	update := result.Updates[0]
	return &model.Tweet{
		Id:     update.ServiceUpdateID,
		Text:   update.Text,
		SentAt: update.SentAt.Time,
	}
}
Beispiel #6
0
// 行動期間を取得します。
func getActionPeriod() int64 {
	str := kocha.Getenv("RETWITTER_ACTION_PERIOD", "-1")
	period, err := strconv.ParseInt(str, 10, 64)
	if err != nil {
		panic(err)
	}

	if period < 0 {
		period = DEFAULT_ACTION_PERIOD
	}

	return period
}
Beispiel #7
0
func SendContactMail(app *kocha.Application, args ...interface{}) error {
	msg := gomail.NewMessage(gomail.SetEncoding(gomail.Base64))
	msg.SetHeader("From", "*****@*****.**")
	msg.SetHeader("To", "*****@*****.**")
	msg.SetHeader("Subject", "【ぼかにゅー】ご意見・ご要望")
	msg.SetBody("text/plain", args[0].(string))
	host, ps, err := net.SplitHostPort(kocha.Getenv("VOCANEW_SMTP_ADDR", "localhost:1025"))
	if err != nil {
		return err
	}
	port, err := strconv.Atoi(ps)
	if err != nil {
		return err
	}
	mailer := gomail.NewMailer(host, "", "", port)
	return mailer.Send(msg)
}
Beispiel #8
0
import (
	"fmt"
	"path/filepath"

	"github.com/naoina/genmai"
	"github.com/naoina/kocha"

	_ "github.com/go-sql-driver/mysql"
	// _ "github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
)

var DatabaseMap = kocha.DatabaseMap{
	"default": {
		Driver: kocha.Getenv("VOCANEW_DB_DRIVER", "sqlite3"),
		DSN:    kocha.Getenv("VOCANEW_DB_DSN", filepath.Join("db", "db.sqlite3")),
	},
}

var dbMap = make(map[string]*genmai.DB)

func Get(name string) *genmai.DB {
	return dbMap[name]
}

func init() {
	for name, dbconf := range DatabaseMap {
		var d genmai.Dialect
		switch dbconf.Driver {
		case "mysql":
Beispiel #9
0
package config

import (
	"os"
	"path/filepath"
	"runtime"
	"time"

	"github.com/naoina/kocha"
	"github.com/naoina/kocha/log"
)

var (
	AppName   = "helloworld"
	AppConfig = &kocha.Config{
		Addr:          kocha.Getenv("KOCHA_ADDR", "127.0.0.1:9100"),
		AppPath:       rootPath,
		AppName:       AppName,
		DefaultLayout: "app",
		Template: &kocha.Template{
			PathInfo: kocha.TemplatePathInfo{
				Name: AppName,
				Paths: []string{
					filepath.Join(rootPath, "app", "view"),
				},
			},
			FuncMap: kocha.TemplateFuncMap{},
		},

		// Logger settings.
		Logger: &kocha.LoggerConfig{
Beispiel #10
0
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/naoina/kocha"
	"github.com/naoina/kocha/event/memory"
	"github.com/naoina/kocha/log"
	"github.com/naoina/vocanew/app/event"
	"github.com/naoina/vocanew/app/middleware"
)

var (
	AppName   = "vocanew"
	AppConfig = &kocha.Config{
		Addr:          kocha.Getenv("VOCANEW_ADDR", "127.0.0.1:9100"),
		AppPath:       rootPath,
		AppName:       AppName,
		DefaultLayout: "app",
		Template: &kocha.Template{
			PathInfo: kocha.TemplatePathInfo{
				Name: AppName,
				Paths: []string{
					filepath.Join(rootPath, "app", "view"),
				},
			},
			FuncMap: kocha.TemplateFuncMap{
				"isDev": func() bool {
					return kocha.Getenv("VOCANEW_ENV", "dev") != "prod"
				},
				"splitFirstNLines": func(s string, n int) []string {
Beispiel #11
0
func checkAuth(r *kocha.Request) bool {
	username, password, ok := r.BasicAuth()
	return ok &&
		username == kocha.Getenv("RETWITTER_AUTH_USERNAME", "") &&
		password == kocha.Getenv("RETWITTER_AUTH_PASSWORD", "")
}