yats.git

ref: afde882c4708403ba71acf27b1791be2fdc52fda

server/main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * 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 (
	"fmt"
	"os"
	"strings"
	"yats-server/config"
	"yats-server/db"
	"yats-server/docs"
	"yats-server/grpc"
	"yats-server/tcp"
)

var configuration config.Configuration
var instanceName = "default"

// @title           Yats Server
// @version         1.0
// @description     Yet Another Time Serie
// @termsOfService  https://kevwe.com/

// @contact.name   API Support
// @contact.url    https://kevwe.com/message
// @contact.email  info@kevwe.com

// @license.name  Affero GPL
// @license.url   https://www.gnu.org/licenses/agpl-3.0.en.html#license-text

// @BasePath  /

// @externalDocs.description  OpenAPI
// @externalDocs.url          https://swagger.io/resources/open-api/
func main() {
	docs.SwaggerInfo.BasePath = "/"

	args := os.Args
	if len(args) < 2 {
		fmt.Println("Loading default config")
		configuration = config.GetConfig(os.Getenv("HOME") + "/.yats.json")
	} else {
		instanceName = args[1]
		fmt.Printf("Loading instance: [%s] config", instanceName)
		configuration = config.GetConfig(os.Getenv("HOME") + "/.yats/" + instanceName + ".json")
	}

	db.Session = db.InitializeDb(configuration)

	if configuration.IsArchiveNode == "true" {
		fmt.Println("Starting Archive Node")
		go MaintenanceThread(configuration)
	}

	if configuration.GrpcAddress != "" {
		fmt.Printf("Starting GRPC Server on address: %s\n", configuration.GrpcAddress)
		go grpc.RunUnsecureYatsGrpcServer(configuration.GrpcAddress)
	}

	if configuration.TcpAddress != "" {
		fmt.Printf("Starting TCP Server on address: %s\n", configuration.TcpAddress)
		go tcp.RunTcpServer(configuration)
	}

	fmt.Printf("Starting REST Server on address: %s\n", configuration.RestAddress)
	RestService(configuration)
}

func CreateTables(schemafile string) {
	filestring, err := os.ReadFile(schemafile)

	if err != nil {
		fmt.Print(err)
	}
	fullFile := string(filestring)
	fullFile = strings.Replace(fullFile, "\n", "", -1)
	queries := strings.Split(fullFile, ";")
	for _, query := range queries {
		fmt.Printf("query: [%s]\n", query)
		//Session.Query(q).Exec()
	}
}