handlers

package
v0.0.0-...-7c0796b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAllMetricsHandler

func GetAllMetricsHandler(cont *GetController) http.Handler
Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/stretchr/testify/assert"

	"github.com/stretchr/testify/require"
)

func isNeededForExampleToShowFullFile() {}

func main() {
	router, controllers := SetUpExampleRouter()
	router.Get("/", GetAllMetricsHandler(controllers.GetController).ServeHTTP)
	server := httptest.NewServer(router)

	req, err := http.NewRequest("GET", server.URL, nil)
	require.NoError(nil, err)

	response, err := server.Client().Do(req)
	require.NoError(nil, err)

	readBytes, err := io.ReadAll(response.Body)
	require.NoError(nil, err)

	err = response.Body.Close()
	require.NoError(nil, err)

	_, err = fmt.Println(string(readBytes))
	assert.NoError(nil, err)
}
Output:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <h2>gauge</h2>
    <ul>

    </ul>
    <h2>counter</h2>
    <ul>

    </ul>
  </body>
</html>

func GetMetricHandler

func GetMetricHandler(cont *GetController) http.Handler
Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/stretchr/testify/assert"

	"github.com/stretchr/testify/require"
)

func isNeededForExampleToShowFullFile2() {}

func main() {
	router, controllers := SetUpExampleRouter()
	router.Get("/value/{metricType}/{metricName}", GetMetricHandler(controllers.GetController).ServeHTTP)
	server := httptest.NewServer(router)

	req, err := http.NewRequest("GET", server.URL+"/value/counter/nm", nil)
	require.NoError(nil, err)

	response, err := server.Client().Do(req)
	require.NoError(nil, err)

	readBytes, err := io.ReadAll(response.Body)
	require.NoError(nil, err)

	err = response.Body.Close()
	require.NoError(nil, err)

	_, err = fmt.Println(string(readBytes))
	assert.NoError(nil, err)
}
Output:

func GetMetricJSONHandler

func GetMetricJSONHandler(cont *GetController) http.Handler

func PingHandler

func PingHandler(cont *PingController) http.Handler

func SaveMetricHandler

func SaveMetricHandler(cont *SaveController) http.Handler

func SaveMetricJSONHandler

func SaveMetricJSONHandler(cont *SaveController) http.Handler

func SaveMetricListHandler

func SaveMetricListHandler(cont *SaveController) http.Handler

Types

type Controllers

type Controllers struct {
	GetController  *GetController
	SaveController *SaveController
	PingController *PingController
	MiddlewareSet  *middleware.Set
}

func NewControllers

func NewControllers(servs *services.Services, middlewareSet *middleware.Set) *Controllers

func SetUpExampleRouter

func SetUpExampleRouter() (*chi.Mux, *Controllers)

type GetController

type GetController struct {
	Service       *services.GetMetricService
	MiddlewareSet *middleware.Set
}

func NewGetController

func NewGetController(serv *services.GetMetricService, middlewareSet *middleware.Set) *GetController

func (GetController) GetAllMetrics

func (cont GetController) GetAllMetrics(res http.ResponseWriter, req *http.Request)

GetAllMetrics returns html page with a list of all metrics with their values @Tags GET @Summary returns html page with a list of all metrics with their values @Description returns html page with a list of all metrics with their values @ID GetAllMetrics @Produce plain @Success 200 {object} string "ok" @Failure 500 {string} string "Internal server error" @Router / [get]

func (GetController) GetMetric

func (cont GetController) GetMetric(res http.ResponseWriter, req *http.Request)

GetMetric get one metric from db in plain text @Tags GET @Summary get one metric from db in plain text @Description get one metric from db in plain text @ID GetMetric @Accept plain @Produce plain @Param metricType path string true "'gauge' or 'counter'" @Param metricName path string true "name of metric, serves as identifier" @Success 200 {object} string "ok" @Failure 400 {string} string "Bad request" @Failure 404 {string} string "unknown metric type" @Failure 500 {string} string "Internal server error" @Router /value/{metricType}/{metricName} [get]

func (GetController) GetMetricJSON

func (cont GetController) GetMetricJSON(res http.ResponseWriter, req *http.Request)

GetMetricJSON get one metric from db in json @Tags GET @Summary get one metric from db in json @Description get one metric from db in json @ID GetMetricJSON @Accept json @Produce json @Param data body string true "requests.GetMetricRequest" @Success 200 {object} string "ok" @Failure 400 {string} string "Bad request" @Failure 404 {string} string "unknown metric type" @Failure 500 {string} string "Internal server error" @Router /value [get]

type PingController

type PingController struct {
	Service *services.PingService
}

func NewPingController

func NewPingController(serv *services.PingService) *PingController

func (*PingController) Ping

func (cont *PingController) Ping(res http.ResponseWriter, _ *http.Request)

Ping check db connection @Summary check db connection @Description check db connection @ID Ping @Accept plain @Produce plain @Success 200 {object} string "ok" @Failure 500 {string} string "Internal server error" @Router /ping [get]

type SaveController

type SaveController struct {
	Service       *services.SaveMetricService
	MiddlewareSet *middleware.Set
}

func NewSaveController

func NewSaveController(serv *services.SaveMetricService, middlewareSet *middleware.Set) *SaveController

func (SaveController) SaveMetric

func (cont SaveController) SaveMetric(res http.ResponseWriter, req *http.Request)

SaveMetric saves metric to db. returns json with saved metric @Tags POST @Summary saves metric to db @Description saves metric to db @ID SaveMetric @Accept plain @Produce plain @Param metricType path string true "'gauge' or 'counter'" @Param metricName path string true "name of metric, serves as identifier" @Param metricValue path string true "int64 if type is counter, float64 if type is gauge" @Success 200 {object} string "ok" @Failure 400 {string} string "Bad request" @Failure 404 {string} string "unknown metric type" @Failure 500 {string} string "Internal server error" @Router /update/{metricType}/{metricName}/{metricValue} [post]

func (SaveController) SaveMetricJSON

func (cont SaveController) SaveMetricJSON(res http.ResponseWriter, req *http.Request)

SaveMetricJSON saves metric to db. returns json with saved metric @Tags POST @Summary saves metric to db @Description saves metric to db @ID SaveMetricJSON @Accept json @Produce json @Param data body string true "requests.SaveMetricRequest" @Success 200 {object} string "ok" @Failure 400 {string} string "Bad request" @Failure 404 {string} string "unknown metric type" @Failure 500 {string} string "Internal server error" @Router /update [post]

func (SaveController) SaveMetricList

func (cont SaveController) SaveMetricList(res http.ResponseWriter, req *http.Request)

SaveMetricList saves metric batch to db. @Tags POST @Summary saves metric batch to db @Description saves metric batch to db @ID SaveMetricList @Accept json @Produce json @Param data body string true "requests.SaveMetricListRequest" @Success 200 {object} string "ok" @Failure 400 {string} string "Bad request" @Failure 404 {string} string "unknown metric type" @Failure 500 {string} string "Internal server error" @Router /updates [post]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL