func main() { flag.IntVar(&port, "port", 3000, "Specify the port to listen to.") flag.StringVar(&user, "user", "", "Auth user.") flag.StringVar(&pass, "pass", "", "Auth password.") flag.Parse() if user == "" || pass == "" { fmt.Println("User or pass empty!") return } context := registration.NewContext("registration") // Instantiate handler indexHandler := ®istration.AppHandler{context, registration.IndexHandler} registerHandler := ®istration.AppHandler{context, registration.RegisterHandler} // Setup mux mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if registration.Authenticated(w, r, user, pass) { indexHandler.ServeHTTP(w, r) return } w.Header().Set("WWW-Authenticate", `Basic realm="FAIRLANCE"`) w.WriteHeader(http.StatusUnauthorized) w.Write([]byte("401 Unauthorized\n")) }) mux.Handle("/register", registerHandler) panic(http.ListenAndServe(":"+strconv.Itoa(port), mux)) }
func buildTestContext(db string) *reg.RegistrationContext { // Setup context context := reg.NewContext(db) // override context.Mailer = TestMailer{} return context }