forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (169 loc) · 5.76 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/pkg/errors"
ucfgYAML "github.com/elastic/go-ucfg/yaml"
"github.com/elastic/package-registry/util"
)
const (
serviceName = "package-registry"
version = "0.15.1"
)
var (
address string
dryRun bool
configPath = "config.yml"
defaultConfig = Config{
CacheTimeIndex: 10 * time.Second,
CacheTimeSearch: 10 * time.Minute,
CacheTimeCategories: 10 * time.Minute,
CacheTimeCatchAll: 10 * time.Minute,
}
)
func init() {
flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.")
// This flag is experimental and might be removed in the future or renamed
flag.BoolVar(&dryRun, "dry-run", false, "Runs a dry-run of the registry without starting the web service (experimental)")
flag.BoolVar(&util.PackageValidationDisabled, "disable-package-validation", false, "Disable package content validation")
}
type Config struct {
PackagePaths []string `config:"package_paths"`
CacheTimeIndex time.Duration `config:"cache_time.index"`
CacheTimeSearch time.Duration `config:"cache_time.search"`
CacheTimeCategories time.Duration `config:"cache_time.categories"`
CacheTimeCatchAll time.Duration `config:"cache_time.catch_all"`
}
func main() {
flag.Parse()
log.Println("Package registry started.")
defer log.Println("Package registry stopped.")
config := mustLoadConfig()
packagesBasePaths := getPackagesBasePaths(config)
ensurePackagesAvailable(packagesBasePaths)
// If -dry-run=true is set, service stops here after validation
if dryRun {
return
}
router := mustLoadRouter(config, packagesBasePaths)
server := &http.Server{Addr: address, Handler: router}
go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Error occurred while serving: %s", err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
ctx := context.TODO()
if err := server.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}
func mustLoadConfig() *Config {
config, err := getConfig()
if err != nil {
log.Fatal(err)
}
printConfig(config)
return config
}
func getConfig() (*Config, error) {
cfg, err := ucfgYAML.NewConfigWithFile(configPath)
if os.IsNotExist(err) {
log.Printf(`Using default configuration options as "%s" is not available.`, configPath)
return &defaultConfig, nil
}
if err != nil {
return nil, errors.Wrapf(err, "reading config failed (path: %s)", configPath)
}
config := defaultConfig
err = cfg.Unpack(&config)
if err != nil {
return nil, errors.Wrapf(err, "unpacking config failed (path: %s)", configPath)
}
return &config, nil
}
func getPackagesBasePaths(config *Config) []string {
var paths []string
paths = append(paths, config.PackagePaths...)
return paths
}
func printConfig(config *Config) {
log.Printf("Packages paths: %s\n", strings.Join(config.PackagePaths, ", "))
log.Println("Cache time for /search: ", config.CacheTimeSearch)
log.Println("Cache time for /categories: ", config.CacheTimeCategories)
log.Println("Cache time for all others: ", config.CacheTimeCatchAll)
}
func ensurePackagesAvailable(packagesBasePaths []string) {
packages, err := util.GetPackages(packagesBasePaths)
if err != nil {
log.Fatal(err)
}
if len(packages) == 0 {
log.Fatal("No packages available")
}
log.Printf("%v package manifests loaded.\n", len(packages))
}
func mustLoadRouter(config *Config, packagesBasePaths []string) *mux.Router {
router, err := getRouter(config, packagesBasePaths)
if err != nil {
log.Fatal(err)
}
return router
}
func getRouter(config *Config, packagesBasePaths []string) (*mux.Router, error) {
artifactsHandler := artifactsHandler(packagesBasePaths, config.CacheTimeCatchAll)
faviconHandleFunc, err := faviconHandler(config.CacheTimeCatchAll)
if err != nil {
return nil, err
}
indexHandlerFunc, err := indexHandler(config.CacheTimeIndex)
if err != nil {
return nil, err
}
packageIndexHandler := packageIndexHandler(packagesBasePaths, config.CacheTimeCatchAll)
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", indexHandlerFunc)
router.HandleFunc("/index.json", indexHandlerFunc)
router.HandleFunc("/search", searchHandler(packagesBasePaths, config.CacheTimeSearch))
router.HandleFunc("/categories", categoriesHandler(packagesBasePaths, config.CacheTimeCategories))
router.HandleFunc("/health", healthHandler)
router.HandleFunc("/favicon.ico", faviconHandleFunc)
router.HandleFunc(artifactsRouterPath, artifactsHandler)
router.HandleFunc(packageIndexRouterPath, packageIndexHandler)
router.PathPrefix("/package").HandlerFunc(staticHandler(packagesBasePaths, "/package", config.CacheTimeCatchAll))
router.Use(loggingMiddleware)
return router, nil
}
// healthHandler is used for Docker/K8s deployments. It returns 200 if the service is live
// In addition ?ready=true can be used for a ready request. Currently both are identical.
func healthHandler(w http.ResponseWriter, r *http.Request) {}
// logging middle to log all requests
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logRequest(r)
next.ServeHTTP(w, r)
})
}
// logRequest converts a request object into a proper logging event
func logRequest(r *http.Request) {
// Do not log requests to the health endpoint
if r.RequestURI == "/health" {
return
}
log.Println(fmt.Sprintf("source.ip: %s, url.original: %s", r.RemoteAddr, r.RequestURI))
}