• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cinode / go / 6921944157

19 Nov 2023 05:26PM UTC coverage: 95.042% (+4.1%) from 90.909%
6921944157

Pull #41

github

byo
Better static_datastore cmd test
Pull Request #41: Internal refactor

1236 of 1316 new or added lines in 37 files covered. (93.92%)

4 existing lines in 3 files now uncovered.

2971 of 3126 relevant lines covered (95.04%)

1.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.08
/pkg/cmd/cinode_web_proxy/root.go
1
/*
2
Copyright © 2023 Bartłomiej Święcki (byo)
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package cinode_web_proxy
18

19
import (
20
        "bytes"
21
        "context"
22
        "errors"
23
        "fmt"
24
        "net/http"
25
        "os"
26
        "runtime"
27
        "sort"
28
        "strings"
29
        "time"
30

31
        "github.com/cinode/go/pkg/blenc"
32
        "github.com/cinode/go/pkg/cinodefs"
33
        "github.com/cinode/go/pkg/cinodefs/httphandler"
34
        "github.com/cinode/go/pkg/datastore"
35
        "github.com/cinode/go/pkg/utilities/httpserver"
36
        "golang.org/x/exp/slog"
37
)
38

39
func Execute(ctx context.Context) error {
1✔
40
        cfg, err := getConfig()
1✔
41
        if err != nil {
2✔
42
                return err
1✔
43
        }
1✔
44
        return executeWithConfig(ctx, cfg)
1✔
45
}
46

47
func executeWithConfig(ctx context.Context, cfg *config) error {
1✔
48
        mainDS, err := datastore.FromLocation(cfg.mainDSLocation)
1✔
49
        if err != nil {
2✔
50
                return fmt.Errorf("could not create main datastore: %w", err)
1✔
51
        }
1✔
52

53
        additionalDSs := []datastore.DS{}
1✔
54
        for _, loc := range cfg.additionalDSLocations {
2✔
55
                ds, err := datastore.FromLocation(loc)
1✔
56
                if err != nil {
2✔
57
                        return fmt.Errorf("could not create additional datastores: %w", err)
1✔
58
                }
1✔
59
                additionalDSs = append(additionalDSs, ds)
1✔
60
        }
61

62
        entrypoint, err := cinodefs.EntrypointFromString(cfg.entrypoint)
1✔
63
        if err != nil {
2✔
64
                return fmt.Errorf("could not parse entrypoint data: %w", err)
1✔
65
        }
1✔
66

67
        log := slog.Default()
1✔
68

1✔
69
        log.Info("Server listening for connections",
1✔
70
                "address", fmt.Sprintf("http://localhost:%d", cfg.port),
1✔
71
        )
1✔
72
        log.Info("Main datastore", "addr", cfg.mainDSLocation)
1✔
73
        log.Info("Additional datastores", "addrs", cfg.additionalDSLocations)
1✔
74

1✔
75
        log.Info("System info",
1✔
76
                "goos", runtime.GOOS,
1✔
77
                "goarch", runtime.GOARCH,
1✔
78
                "compiler", runtime.Compiler,
1✔
79
                "cpus", runtime.NumCPU(),
1✔
80
        )
1✔
81

1✔
82
        handler, err := setupCinodeProxy(ctx, mainDS, additionalDSs, entrypoint)
1✔
83
        if err != nil {
1✔
NEW
84
                return err
×
NEW
85
        }
×
86

87
        return httpserver.RunGracefully(ctx,
1✔
88
                handler,
1✔
89
                httpserver.ListenPort(cfg.port),
1✔
90
                httpserver.Logger(log),
1✔
91
        )
1✔
92
}
93

94
func setupCinodeProxy(
95
        ctx context.Context,
96
        mainDS datastore.DS,
97
        additionalDSs []datastore.DS,
98
        entrypoint *cinodefs.Entrypoint,
99
) (http.Handler, error) {
1✔
100
        fs, err := cinodefs.New(
1✔
101
                ctx,
1✔
102
                blenc.FromDatastore(
1✔
103
                        datastore.NewMultiSource(
1✔
104
                                mainDS,
1✔
105
                                time.Hour,
1✔
106
                                additionalDSs...,
1✔
107
                        ),
1✔
108
                ),
1✔
109
                cinodefs.RootEntrypoint(entrypoint),
1✔
110
                cinodefs.MaxLinkRedirects(10),
1✔
111
        )
1✔
112
        if err != nil {
1✔
NEW
113
                return nil, err
×
UNCOV
114
        }
×
115

116
        return &httphandler.Handler{
1✔
117
                FS:        fs,
1✔
118
                IndexFile: "index.html",
1✔
119
                Log:       slog.Default(),
1✔
120
        }, nil
1✔
121
}
122

123
type config struct {
124
        entrypoint            string
125
        mainDSLocation        string
126
        additionalDSLocations []string
127
        port                  int
128
}
129

130
func getConfig() (*config, error) {
1✔
131
        cfg := config{}
1✔
132

1✔
133
        entrypoint, found := os.LookupEnv("CINODE_ENTRYPOINT")
1✔
134
        if !found {
2✔
135
                entrypointFile, found := os.LookupEnv("CINODE_ENTRYPOINT_FILE")
1✔
136
                if !found {
2✔
137
                        return nil, errors.New("missing CINODE_ENTRYPOINT or CINODE_ENTRYPOINT_FILE env var")
1✔
138
                }
1✔
139
                entrypointFileData, err := os.ReadFile(entrypointFile)
1✔
140
                if err != nil {
2✔
141
                        return nil, fmt.Errorf("could not read entrypoint file at '%s': %w", entrypointFile, err)
1✔
142
                }
1✔
143
                entrypoint = string(bytes.TrimSpace(entrypointFileData))
1✔
144
        }
145
        cfg.entrypoint = entrypoint
1✔
146

1✔
147
        cfg.mainDSLocation = os.Getenv("CINODE_MAIN_DATASTORE")
1✔
148
        if cfg.mainDSLocation == "" {
2✔
149
                cfg.mainDSLocation = "memory://"
1✔
150
        }
1✔
151

152
        additionalDSEnvNames := []string{}
1✔
153
        for _, e := range os.Environ() {
2✔
154
                if strings.HasPrefix(e, "CINODE_ADDITIONAL_DATASTORE") {
2✔
155
                        split := strings.SplitN(e, "=", 2)
1✔
156
                        additionalDSEnvNames = append(additionalDSEnvNames, split[0])
1✔
157
                }
1✔
158
        }
159
        sort.Strings(additionalDSEnvNames)
1✔
160

1✔
161
        for _, envName := range additionalDSEnvNames {
2✔
162
                location := os.Getenv(envName)
1✔
163
                cfg.additionalDSLocations = append(cfg.additionalDSLocations, location)
1✔
164
        }
1✔
165

166
        cfg.port = 8080
1✔
167

1✔
168
        return &cfg, nil
1✔
169
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc