Skip to content

Commit

Permalink
feat: config documents (zurvan-lab#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Oct 30, 2023
1 parent 24609c3 commit 50bc6d7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
18 changes: 16 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type Server struct {
}

type Log struct {
Path string `yaml:"path"`
WriteToFile bool `yaml:"write_to_file"`
Path string `yaml:"path"`
}

type User struct {
Expand All @@ -38,6 +39,18 @@ func (conf *Config) BasicCheck() error {
if len(conf.Users) <= 0 {
return errors.New("invalid user length")
}

for _, u := range conf.Users {
allCmds := false
for _, c := range u.Cmds {
if c == "*" {
allCmds = true
}
}
if allCmds && len(u.Cmds) > 1 {
return errors.New("can't have all cmds and specific cmd at same time")
}
}
return nil
}

Expand All @@ -48,7 +61,8 @@ func DefaultConfig() *Config {
Port: "7070",
},
Log: Log{
Path: "ttrace.log",
WriteToFile: true,
Path: "ttrace.log",
},
Name: "time_trace",
}
Expand Down
9 changes: 5 additions & 4 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ server:
port: "7070"

log:
write_to_file: true
path: ttrace.log

users:
Expand All @@ -18,10 +19,10 @@ users:
# - name: root
# password: super_secret_password
# cmd:
# - * # all commands.
# - '*' # all commands.
# - name: developer
# password: secret_password
# cmd:
# - GET
# - PUSH
# - DEL
# - 'GET'
# - 'PUSH'
# - 'DEL'
5 changes: 5 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ func TestBasicCheck(t *testing.T) {
c.Users = []User{}
err = c.BasicCheck()
assert.Error(t, errors.New("invalid user length"), err)

c.Users = []User{DefaultConfig().Users[0]}
c.Users[0].Cmds = []string{"*", "GET"}
err = c.BasicCheck()
assert.Error(t, errors.New("can't have all cmds and specific cmd at same time"), err)
}
Empty file removed doc/config/.keep
Empty file.
71 changes: 71 additions & 0 deletions doc/config/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Config

In this part of documentation we are going to explain config in ttrace.

### config file

In ttrace we have config in `yaml` format, you can see default config example [here](../../config/config.yaml).
Now we explain each part of config one by one.

#### name

First of all we have a name, which is name of instance you are running (you can use it on application layer and get it by commands).
This is name field in `config.yaml`:

```yml
name: time_trace
```

#### server

In server part you can config this two: which ip? which port? for listening and serving the TCP server.

This is how it looks in `config.yaml`:

```yml
server:
listen: localhost
port: "7070"
```

#### log

This part will help you to config log stuff (levels, saving path and...).

How it looks in `config.yaml`:

```yml
log:
write_to_file: true
path: ttrace.log
```

#### users

In users part, you define who can access the database and set permission for them. `name` and `password` field is user pass for connecting (required in `CON` command).

In `cmds` you provide which command in [TQL](../TQL/) they have access to. It can be a list of commands or just a `'*'` which means all.
Also the `CON` command is open for everyone.

example:

```yml
users:
- name: root
password: super_secret_password
cmds:
- '*'

# Also you can use this for multiple users and limited commands
# users:
# - name: root
# password: super_secret_password
# cmd:
# - '*' # all commands.
# - name: developer
# password: secret_password
# cmd:
# - 'GET'
# - 'PUSH'
# - 'DEL'
```

0 comments on commit 50bc6d7

Please sign in to comment.