Skip to content

Commit c84b83c

Browse files
Completed the YAML configuration structure and complete implementation (new fields can just be added on demand)
1 parent 4533742 commit c84b83c

File tree

5 files changed

+110
-28
lines changed

5 files changed

+110
-28
lines changed

cmd/laclm/main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package main
22

33
import (
44
"context"
5+
"fmt"
6+
"net/http"
57
"os"
68
"os/signal"
79
"syscall"
810
"time"
9-
"net/http"
1011

1112
"go.uber.org/zap"
1213

13-
"github.com/PythonHacker24/linux-acl-management-backend/internal/utils"
1414
"github.com/PythonHacker24/linux-acl-management-backend/api/routes"
15+
"github.com/PythonHacker24/linux-acl-management-backend/config"
16+
"github.com/PythonHacker24/linux-acl-management-backend/internal/utils"
1517
)
1618

1719
func main() {
@@ -22,9 +24,13 @@ func main() {
2224

2325
func exec() error {
2426

25-
/*
26-
exec() wraps run() protecting it with user interrupts
27+
/* exec() wraps run() protecting it with user interrupts */
28+
29+
/*
30+
load config file
31+
if there is an error in loading the config file, then it will exit with code 1
2732
*/
33+
config.LoadConfig("./config.yaml")
2834

2935
/* true for production, false for development mode */
3036
utils.InitLogger(false)
@@ -56,7 +62,10 @@ func run(ctx context.Context) error {
5662
routes.RegisterRoutes(mux)
5763

5864
server := &http.Server{
59-
Addr: ":8080",
65+
Addr: fmt.Sprintf("%s:%s",
66+
config.BackendConfig.Server.Host,
67+
config.BackendConfig.Server.Port,
68+
),
6069
Handler: mux,
6170
}
6271

config.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# config.yaml
2+
3+
# backend environment configs
4+
app:
5+
name:
6+
version:
7+
environment:
8+
9+
# backend server deployment configs
10+
server:
11+
host:
12+
port:
13+
14+
# databases for operations
15+
database:
16+
transaction_logs_redis:
17+
address:
18+
password:
19+
db:
20+
21+
# logging configurations
22+
logging:
23+
file:
24+
max_size:
25+
max_backups:
26+
max_age:
27+
compress:
28+
29+
# filesystem server that needs management
30+
filesystem_servers:
31+
- path:
32+
method:
33+
remote:
34+
host:
35+
port:
36+
37+
# authentication information
38+
authentication:
39+
ldap:

config/config.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package config
22

3-
/* structure of config struct for yaml config */
4-
type Config struct {
5-
6-
}
3+
import "github.com/PythonHacker24/linux-acl-management-backend/internal/models"
74

85
/* globally accessible config */
9-
var BackendConfig Config
6+
var BackendConfig models.Config

internal/models/models.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
11
package models
22

3-
type Config struct {
3+
/* app parameters */
4+
type App struct {
5+
Name string `yaml:"name"`
6+
Version string `yaml:"version"`
7+
Environment string `yaml:"environment"`
8+
}
9+
10+
/* server deployment parameters */
11+
type Server struct {
12+
Host string `yaml:"host"`
13+
Port string `yaml:"port"`
14+
}
15+
16+
/* database parameters */
17+
type Database struct {
18+
TransactionLogRedis TransactionLogRedis `yaml:"transaction_log_redis"`
19+
}
20+
21+
/* transaction log redis parameters */
22+
type TransactionLogRedis struct {
23+
Address string `yaml:"address"`
24+
Password string `yaml:"password"`
25+
DB string `yaml:"db"`
26+
}
27+
28+
/* logging parameters */
29+
type Logging struct {
30+
File string `yaml:"file"`
31+
MaxSize int `yaml:"max_size"`
32+
MaxBackups int `yaml:"max_backups"`
33+
MaxAge int `yaml:"max_age"`
34+
Compress int `yaml:"compress"`
35+
}
36+
37+
/* file system server parameters */
38+
type FileSystemServers struct {
39+
Path string `yaml:"path"`
40+
Method string `yaml:"method"`
41+
Remote *Remote `yaml:"remote"`
42+
}
443

44+
/* remote parameters for file system server with laclm daemons installed */
45+
type Remote struct {
46+
Host string `yaml:"host"`
47+
Port int `yaml:"port"`
48+
}
49+
50+
/* complete config for global usage */
51+
type Config struct {
52+
AppInfo App `yaml:"app"`
53+
Server Server `yaml:"server"`
54+
Database Database `yaml:"database"`
55+
Logging Logging `yaml:"logging"`
56+
FileSystemServers []FileSystemServers `yaml:"filesystem_servers"`
557
}
658

59+
/* health response */
760
type HealthResponse struct {
861
Status string `json:"status"`
962
}
1063

64+
/* username and password */
1165
type User struct {
1266
Username string `json:"username"`
1367
Password string `json:"password"`

internal/utils/utils.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66

77
"github.com/google/uuid"
8-
"gopkg.in/yaml.v3"
98
"go.uber.org/zap"
109
"go.uber.org/zap/zapcore"
1110
"gopkg.in/natefinch/lumberjack.v2"
@@ -54,22 +53,6 @@ func InitLogger(isProduction bool) {
5453
log.Println("Initialized Zap Logger")
5554
}
5655

57-
/* yaml file loader for config */
58-
func LoadConfig(filename string) (*models.Config, error) {
59-
data, err := os.ReadFile(filename)
60-
if err != nil {
61-
return nil, err
62-
}
63-
64-
var config models.Config
65-
err = yaml.Unmarshal(data, &config)
66-
if err != nil {
67-
return nil, err
68-
}
69-
70-
return &config, nil
71-
}
72-
7356
/* generate a new uuid */
7457
func GenerateTxnID() string {
7558
return uuid.New().String()

0 commit comments

Comments
 (0)