yats.git

ref: 5646a8896664ecc31a858a514925b94c50848a76

server/service-rest.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */

package main

import (
	"yats-server/config"
	"yats-server/rest"

	"github.com/gin-gonic/gin"
)

func RestService(cfg config.Configuration) {

	//	session = DB
	address := cfg.RestAddress

	router := gin.Default()

	router.Use(CorsHeaders())

	router.SetTrustedProxies([]string{"127.0.0.1"})
	gin.SetMode(gin.ReleaseMode)

	router.POST("/metric", rest.WriteMetric(cfg))
	router.POST("/metric/search", rest.SearchMetrics(cfg))
	router.GET("/metric/:name/:from", rest.SearchMetricsFrom(cfg))

	router.POST("/event", rest.WriteEvent(cfg))
	router.POST("/event/search", rest.SearchEvents(cfg))
	router.GET("/event/:from", rest.GetEventsFrom(cfg))

	router.POST("/position", rest.WritePosition(cfg))

	enableSwaggerEndpoint(router)

	if cfg.GrafanaActive == "true" {
		enableGrafanaEndpoint(router)
	}

	if cfg.TlsActive == "true" {
		router.RunTLS(address, cfg.TlsCertificate, cfg.TlsKeyFile)
	} else {
		router.Run(address)
	}
}