当前位置: 首页>>代码示例>>Golang>>正文


Golang handy.NewHandy函数代码示例

本文整理汇总了Golang中github.com/rafaeljusto/shelter/Godeps/_workspace/src/github.com/rafaeljusto/handy.NewHandy函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHandy函数的具体用法?Golang NewHandy怎么用?Golang NewHandy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewHandy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: deleteDomains

func deleteDomains(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for i := 0; i < 100; i++ {
		r, err := http.NewRequest("DELETE", fmt.Sprintf("/domain/example%d.com.br.", i), nil)
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, nil)

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != http.StatusNoContent {
			utils.Fatalln("Error removing domain", errors.New(string(responseContent)))
		}
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:28,代码来源:rest_handler_domains.go

示例2: retrieveUnknownScan

func retrieveUnknownScan(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Add(-1*time.Millisecond).Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNotFound {
		utils.Fatalln("Error retrieving unknown scan", errors.New(string(responseContent)))
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:26,代码来源:rest_handler_scan.go

示例3: retrieveScansIfNoneMatch

func retrieveScansIfNoneMatch(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	scansCacheTest(database, r, "If-None-Match", []ScansCacheTestData{
		{
			HeaderValue:        w.Header().Get("ETag"),
			ExpectedHTTPStatus: http.StatusNotModified,
		},
		{
			HeaderValue:        w.Header().Get("ETag") + "x",
			ExpectedHTTPStatus: http.StatusOK,
		},
	})
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:28,代码来源:rest_handler_scans.go

示例4: retrieveUnknownDomain

func retrieveUnknownDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNotFound {
		utils.Fatalln("Error retrieving unknown domain", errors.New(string(responseContent)))
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:26,代码来源:rest_handler_domain.go

示例5: domainCacheTest

func domainCacheTest(database *mgo.Database, r *http.Request,
	requestContent, header string, domainCacheTestData []DomainCacheTestData) {

	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for _, item := range domainCacheTestData {
		r.Header.Set(header, item.HeaderValue)

		if len(requestContent) > 0 {
			utils.BuildHTTPHeader(r, []byte(requestContent))
		} else {
			utils.BuildHTTPHeader(r, nil)
		}

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != item.ExpectedHTTPStatus {
			utils.Fatalln(fmt.Sprintf("Error in %s test using %s [%s] "+
				"HTTP header. Expected HTTP status code %d and got %d",
				r.Method, header, item.HeaderValue, item.ExpectedHTTPStatus,
				w.Code), errors.New(string(responseContent)))
		}
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:35,代码来源:rest_handler_domain.go

示例6: retrieveScan

func retrieveScan(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "1" {
		utils.Fatalln("Not setting ETag in scan retrieve response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in scan retrieve response", nil)
	}

	if h.Response.Status != "EXECUTED" {
		utils.Fatalln("Scan's status was not persisted correctly", nil)
	}

	if h.Response.DomainsScanned != 5000000 {
		utils.Fatalln("Scan's domains scanned information is wrong", nil)
	}

	if h.Response.DomainsWithDNSSECScanned != 250000 {
		utils.Fatalln("Scan's domains with DNSSEC scanned information is wrong", nil)
	}

	if h.Response.NameserverStatistics["OK"] != 4800000 ||
		h.Response.NameserverStatistics["TIMEOUT"] != 200000 {
		utils.Fatalln("Scan's nameserver statistics are wrong", nil)
	}

	if h.Response.DSStatistics["OK"] != 220000 ||
		h.Response.DSStatistics["EXPSIG"] != 30000 {
		utils.Fatalln("Scan's nameserver statistics are wrong", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:56,代码来源:rest_handler_scan.go

示例7: retrieveDomain

func retrieveDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "2" {
		utils.Fatalln("Not setting ETag in domain retrieve response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in domain retrieve response", nil)
	}

	if h.Response.FQDN != "example.com.br." {
		utils.Fatalln("Domain's FQDN was not persisted correctly", nil)
	}

	if len(h.Response.Nameservers) != 2 ||
		h.Response.Nameservers[0].Host != "ns1.example.com.br." ||
		h.Response.Nameservers[0].IPv4 != "127.0.0.1" ||
		h.Response.Nameservers[1].Host != "ns3.example.com.br." ||
		h.Response.Nameservers[1].IPv6 != "::1" {
		utils.Fatalln("Domain's nameservers were not persisted correctly", nil)
	}

	if len(h.Response.Owners) != 1 ||
		h.Response.Owners[0].Email != "[email protected]" {

		utils.Fatalln("Domain's owners were not persisted correctly", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:52,代码来源:rest_handler_domain.go

示例8: updateDomain

func updateDomain(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	requestContent := `{
      "Nameservers": [
        { "host": "ns1.example.com.br.", "ipv4": "127.0.0.1" },
        { "host": "ns3.example.com.br.", "ipv6": "::1" }
      ],
      "Owners": [
        { "email": "[email protected]", "language": "pt-br" }
      ]
    }`

	r, err := http.NewRequest("PUT", "/domain/example.com.br.", strings.NewReader(requestContent))
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, []byte(requestContent))

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusNoContent {
		utils.Fatalln(fmt.Sprintf("Error updating domain. "+
			"Expecting %d and got %d", http.StatusNoContent, w.Code),
			errors.New(string(responseContent)))
	}

	if w.Header().Get("ETag") != "2" {
		utils.Fatalln("Not setting ETag in domain update response", nil)
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Not setting Last-Modified in domain update response", nil)
	}

	if len(w.Header().Get("Location")) > 0 {
		utils.Fatalln("Setting Location in domain update response", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:50,代码来源:rest_handler_domain.go

示例9: retrieveScanMetadata

func retrieveScanMetadata(database *mgo.Database, startedAt time.Time) {
	mux := handy.NewHandy()

	h := new(handler.ScanHandler)
	mux.Handle("/scan/{started-at}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	response1 := *h.Response
	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	r, err = http.NewRequest("HEAD", fmt.Sprintf("/scan/%s", startedAt.Format(time.RFC3339Nano)), nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	mux.ServeHTTP(w, r)

	response2 := *h.Response
	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if !utils.CompareProtocolScan(response1, response2) {
		utils.Fatalln("At this point the GET and HEAD method should "+
			"return the same body content", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:50,代码来源:rest_handler_scan.go

示例10: retrieveScansMetadata

func retrieveScansMetadata(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans/?orderby=startedat:desc&pagesize=10&page=1", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	response1 := *h.Response
	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scans", errors.New(string(responseContent)))
	}

	r, err = http.NewRequest("HEAD", "/scans/?orderby=startedat:desc&pagesize=10&page=1", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	mux.ServeHTTP(w, r)

	response2 := *h.Response
	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scans", errors.New(string(responseContent)))
	}

	if !utils.CompareProtocolScans(response1, response2) {
		utils.Fatalln("At this point the GET and HEAD method should "+
			"return the same body content", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:50,代码来源:rest_handler_scans.go

示例11: retrieveDomainMetadata

func retrieveDomainMetadata(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	response1 := *h.Response
	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body from GET", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	r, err = http.NewRequest("HEAD", "/domain/example.com.br.", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	mux.ServeHTTP(w, r)

	response2 := *h.Response
	responseContent, err = ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body from HEAD", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving domain", errors.New(string(responseContent)))
	}

	if !utils.CompareProtocolDomain(response1, response2) {
		utils.Fatalln("At this point the GET and HEAD method should "+
			"return the same body content", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:50,代码来源:rest_handler_domain.go

示例12: retrieveCurrentScan

func retrieveCurrentScan(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.ScansHandler)
	mux.Handle("/scans", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/scans?current", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	responseContent, err := ioutil.ReadAll(w.Body)
	if err != nil {
		utils.Fatalln("Error reading response body", err)
	}

	if w.Code != http.StatusOK {
		utils.Fatalln("Error retrieving scan", errors.New(string(responseContent)))
	}

	if len(w.Header().Get("Last-Modified")) == 0 {
		utils.Fatalln("Current scan should return the Last-Modified header", nil)
	}

	if len(h.Response.Scans) != 1 {
		utils.Fatalln("Current scan not returned when it should", nil)
	}

	if h.Response.Scans[0].Status != "RUNNING" ||
		h.Response.Scans[0].DomainsToBeScanned != 5 ||
		h.Response.Scans[0].DomainsScanned != 4 ||
		h.Response.Scans[0].DomainsWithDNSSECScanned != 1 {

		utils.Fatalln("Not retrieving current scan information correctly", nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:42,代码来源:rest_handler_scans.go

示例13: Start

func Start(listeners []net.Listener) error {
	// Initialize language configuration file
	if err := loadMessages(); err != nil {
		return err
	}

	// Initialize CIDR whitelist
	if err := loadACL(); err != nil {
		return err
	}

	// Handy logger should use the same logger of the Shelter system
	handy.Logger = log.Logger

	mux := handy.NewHandy()
	mux.Recover = func(r interface{}) {
		const size = 64 << 10
		buf := make([]byte, size)
		buf = buf[:runtime.Stack(buf, false)]
		log.Printf("REST panic detected. Details: %v\n%s", r, buf)
	}

	for pattern, handler := range handler.Routes {
		mux.Handle(pattern, handler)
	}

	server := http.Server{
		Handler:      mux,
		ReadTimeout:  time.Duration(config.ShelterConfig.RESTServer.Timeouts.ReadSeconds) * time.Second,
		WriteTimeout: time.Duration(config.ShelterConfig.RESTServer.Timeouts.WriteSeconds) * time.Second,
	}

	for _, v := range listeners {
		// We are not checking the error returned by Serve, because if we check for some
		// reason the HTTP server stop answering the requests
		go server.Serve(v)
	}

	return nil
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:40,代码来源:server.go

示例14: createDomains

func createDomains(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	for i := 0; i < 100; i++ {
		requestContent := `{
      "Nameservers": [
        { "host": "ns1.example.com.br." },
        { "host": "ns2.example.com.br." }
      ],
      "Owners": [
        { "email": "[email protected]", "language": "pt-br" }
      ]
    }`

		r, err := http.NewRequest("PUT", fmt.Sprintf("/domain/example%d.com.br.", i),
			strings.NewReader(requestContent))
		if err != nil {
			utils.Fatalln("Error creating the HTTP request", err)
		}
		utils.BuildHTTPHeader(r, []byte(requestContent))

		w := httptest.NewRecorder()
		mux.ServeHTTP(w, r)

		responseContent, err := ioutil.ReadAll(w.Body)
		if err != nil {
			utils.Fatalln("Error reading response body", err)
		}

		if w.Code != http.StatusCreated {
			utils.Fatalln("Error creating domain", errors.New(string(responseContent)))
		}
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:39,代码来源:rest_handler_domains.go

示例15: invalidFQDN

func invalidFQDN(database *mgo.Database) {
	mux := handy.NewHandy()

	h := new(handler.DomainHandler)
	mux.Handle("/domain/{fqdn}", func() handy.Handler {
		return h
	})

	r, err := http.NewRequest("GET", "/domain/!!!", nil)
	if err != nil {
		utils.Fatalln("Error creating the HTTP request", err)
	}
	utils.BuildHTTPHeader(r, nil)

	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)

	if w.Code != http.StatusBadRequest {
		utils.Fatalln(fmt.Sprintf("Not verifying if FQDN exists in the URI. "+
			"Expected status %d and got %d", http.StatusBadRequest, w.Code), nil)
	}
}
开发者ID:rafaeljusto,项目名称:shelter,代码行数:22,代码来源:rest_handler_domain.go


注:本文中的github.com/rafaeljusto/shelter/Godeps/_workspace/src/github.com/rafaeljusto/handy.NewHandy函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。