chore: add sqlite init and config, add repository for sql
ci-agent / build (push) Failing after 26s

This commit is contained in:
d3m0k1d
2026-04-03 22:48:31 +03:00
parent 2ebf374413
commit 28ef2dc1fd
12 changed files with 206 additions and 1 deletions
+20
View File
@@ -1 +1,21 @@
package config
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
)
func ImportSettings(path string) (*HellreigN, error) {
data, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
}
var cfg HellreigN
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
+10 -1
View File
@@ -1,4 +1,13 @@
package config
type Config struct {
type HellreigN struct {
Database Databases `yaml:"database"`
}
type Databases struct {
Token_db string `yaml:"token_db"`
Clickhouse_host string `yaml:"clickhouse_host"`
Clickhouse_user string `yaml:"clickhouse_user"`
Clickhouse_password string `yaml:"clickhouse_password"`
}
+35
View File
@@ -1 +1,36 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
)
// AgentsGroup — группа хэндлеров для агентов
type AgentsGroup struct {
*Handlers
}
// List GET /api/v1/agents
func (ag *AgentsGroup) List(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Agents list"})
}
// GetByID GET /api/v1/agents/:id
func (ag *AgentsGroup) GetByID(c *gin.Context) {
id := c.Param("id")
c.JSON(http.StatusOK, gin.H{"id": id})
}
// Create POST /api/v1/agents
func (ag *AgentsGroup) Create(c *gin.Context) {
var body struct {
Name string `json:"name" binding:"required"`
}
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"name": body.Name})
}
+19
View File
@@ -0,0 +1,19 @@
package handlers
import (
"database/sql"
"gitea.d3m0k1d.ru/d3m0k1d/HellreigN/backend/internal/repository"
)
type Handlers struct {
DB *sql.DB
Repo *repository.Repository
}
func New(db *sql.DB) *Handlers {
return &Handlers{
DB: db,
Repo: repository.New(db),
}
}
+11
View File
@@ -0,0 +1,11 @@
package repository
import "database/sql"
type Repository struct {
DB *sql.DB
}
func New(db *sql.DB) *Repository {
return &Repository{DB: db}
}
+3
View File
@@ -0,0 +1,3 @@
package storage
const CreateSqlite = ``
+33
View File
@@ -0,0 +1,33 @@
package storage
import (
"database/sql"
"fmt"
_ "modernc.org/sqlite"
"strings"
)
var pragmas = map[string]string{
`journal_mode`: `wal`,
`synchronous`: `normal`,
`busy_timeout`: `30000`,
}
func buildSqliteDsn(path string, pragmas map[string]string) string {
pragmastrs := make([]string, len(pragmas))
i := 0
for k, v := range pragmas {
pragmastrs[i] = (fmt.Sprintf(`pragma=%s(%s)`, k, v))
i++
}
return path + "?" + "mode=rwc&" + strings.Join(pragmastrs, "&")
}
func Open(path string) (*sql.DB, error) {
dsn := buildSqliteDsn(path, pragmas)
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, err
}
return db, nil
}
+14
View File
@@ -0,0 +1,14 @@
package initial
import (
"crypto/rand"
"encoding/hex"
)
func RandomToken() (string, error) {
token := make([]byte, 32)
if _, err := rand.Read(token); err != nil {
return "", err
}
return hex.EncodeToString(token), nil
}