-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
49 lines (40 loc) · 1.08 KB
/
server.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore
import (
"fmt"
"net/http"
"sort"
"labix.org/v2/mgo"
"github.com/juju/charmstore/internal/charmstore"
"github.com/juju/charmstore/internal/v4"
)
// Versions of the API that can be served.
const (
V4 = "v4"
)
var versions = map[string]func(*charmstore.Store) http.Handler{
V4: v4.New,
}
// Versions returns all known API version strings in alphabetical order.
func Versions() []string {
vs := make([]string, 0, len(versions))
for v := range versions {
vs = append(vs, v)
}
sort.Strings(vs)
return vs
}
// NewServer returns a new handler that handles
// charm store requests and stores its data in the given database.
func NewServer(db *mgo.Database, serveVersions ...string) (http.Handler, error) {
newAPIs := make(map[string]charmstore.NewAPIHandler)
for _, vers := range serveVersions {
newAPI := versions[vers]
if newAPI == nil {
return nil, fmt.Errorf("unknown version %q", vers)
}
newAPIs[vers] = newAPI
}
return charmstore.NewServer(db, newAPIs)
}