示例#1
0
func setupServer(conf *config.Config) (err error) {
	dbConn, err = db.Open(conf.Redis.DSN)

	if err != nil {
		return
	}

	newWordfilter = func(list wordlist.Wordlist) *wordfilter.Wordfilter {
		var replacer wordfilter.Replacer

		switch conf.Filter {
		case types.Any:
			replacer = wordfilter.NewStringReplacer()
		case types.Word:
			replacer = wordfilter.NewSetReplacer()
		default:
			replacer = wordfilter.NewSetReplacer()
		}

		return &wordfilter.Wordfilter{
			List:     list,
			Replacer: replacer,
		}
	}

	filters = newProfanityFilters()

	// HTTP endpoints
	router = mux.NewRouter()
	router.HandleFunc("/v1/profanity/sanitize/", sanitizeHandle).Methods("GET").Name("sanitize")
	router.HandleFunc("/v1/profanity/blacklist/", updateBlacklistHandle).Methods("POST", "PUT").Name("blacklist")
	router.HandleFunc("/v1/profanity/blacklist/remove/", removeBlacklistHandle).Methods("POST", "PUT").Name("blacklist")
	router.HandleFunc("/v1/profanity/blacklist/", getBlacklistHandle).Methods("GET").Name("blacklist")
	router.StrictSlash(false)

	// global middleware
	var middleware []func(http.Handler) http.Handler

	switch log.Severity {
	case log.LevelDebug:
		middleware = append(middleware, handler.LogHandler, handler.DebugHandle, handler.RecoveryHandler)
	default:
		middleware = append(middleware, handler.LogHandler, handler.RecoveryHandler)
	}

	wrapped := handler.Use(router, middleware...)
	http.Handle("/", wrapped)
	return
}
示例#2
0
func initBackend() {
	c, _ := db.Open("redis://:@localhost:6379/15")
	backends = append(backends, NewRedisWordlist(c, "en_US"))
}