-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
140 lines (125 loc) · 3.76 KB
/
model.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright 2024 RapidLoop, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"errors"
"fmt"
"net"
"os"
"regexp"
"strings"
"time"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/jackc/pgx/v5"
"github.com/robfig/cron/v3"
)
type Config struct {
Listen string `hcl:"listen,optional"`
Connection ConnectionConfig `hcl:"connection,block"`
Endpoints []EndpointConfig `hcl:"endpoint,block"`
}
func (c *Config) Validate() error {
if _, _, err := net.SplitHostPort(c.Listen); err != nil {
return fmt.Errorf("listen: %v", err)
}
if err := c.Connection.Validate(); err != nil {
return err
}
if len(c.Endpoints) == 0 {
return errors.New("no endpoints defined")
}
for _, e := range c.Endpoints {
if err := e.Validate(); err != nil {
return err
}
}
return nil
}
type ConnectionConfig struct {
DSN string `hcl:"dsn"`
MaxConns int `hcl:"maxconns,optional"`
IdleTimeout string `hcl:"idletimeout,optional"`
}
func (c *ConnectionConfig) Validate() error {
if _, err := pgx.ParseConfig(c.DSN); err != nil {
return fmt.Errorf("connection.dsn: %v", err)
}
if c.MaxConns < 0 {
return errors.New("connection.maxconns: cannot be negative")
}
if c.IdleTimeout != "" {
if _, err := time.ParseDuration(c.IdleTimeout); err != nil {
return fmt.Errorf("connection.idletimeout: %v", err)
}
}
return nil
}
type EndpointConfig struct {
Path string `hcl:"path,label"`
SQL string `hcl:"sql"`
SQLTimeout string `hcl:"sqltimeout,optional"`
Schedule string `hcl:"schedule"`
RowFormat string `hcl:"rowformat,optional"`
FileBacked bool `hcl:"filebacked,optional"`
}
var rxURI = regexp.MustCompile(`^(/(({[A-Za-z0-9_.-]+})|([A-Za-z0-9_.-]+)))+$`)
func (e *EndpointConfig) Validate() error {
if !rxURI.MatchString(e.Path) && e.Path != "/" {
return fmt.Errorf("endpoint \"%s\": invalid path: must be set to a valid URI", e.Path)
}
if strings.TrimSpace(e.SQL) == "" {
return fmt.Errorf("endpoint \"%s\": invalid sql: must be set", e.Path)
}
if e.SQLTimeout != "" {
if _, err := time.ParseDuration(e.SQLTimeout); err != nil {
return fmt.Errorf("endpoint \"%s\": invalid sqltimeout: %v", e.Path, err)
}
}
if _, err := cron.ParseStandard(e.Schedule); err != nil {
return fmt.Errorf("endpoint \"%s\": invalid schedule: %v", e.Path, err)
}
if e.RowFormat != "" && e.RowFormat != "array" && e.RowFormat != "object" {
return fmt.Errorf("endpoint \"%s\": invalid rowformat: must be 'array' or 'object'", e.Path)
}
return nil
}
func ConfigFromFile(filename string) (*Config, error) {
var cfg Config
src, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
file, diags := hclsyntax.ParseConfig(src, filename, hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
return nil, diags
}
diags = gohcl.DecodeBody(file.Body, nil, &cfg)
if diags.HasErrors() {
return nil, diags
}
return &cfg, nil
}
//------------------------------------------------------------------------------
type EndpointResult struct {
QueriedAt string // as a string to avoid formatting each time
CacheControl string
ValidUntil time.Time
ETag string
Result []byte
File string // if not empty, result is in this file
}