This repository was archived by the owner on Nov 29, 2017. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
168 lines (147 loc) · 4.13 KB
/
Copy pathmain.go
File metadata and controls
168 lines (147 loc) · 4.13 KB
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
package main
import (
"crypto/md5"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"path"
"path/filepath"
"github.com/gorilla/mux"
"github.com/minio/cli"
minio "github.com/minio/minio-go"
homedir "github.com/mitchellh/go-homedir"
)
// Endpoint - represents and S3 endpoint.
type Endpoint struct {
URL string `json:"url"`
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
}
// Config - represents on disk config file.
type Config struct {
Auth string `json:"auth"`
Endpoints []Endpoint `json:"endpoints"`
}
// Holds all the info needed for routing.
type mnm struct {
hashMap map[string]*minio.Client
hashes []string
address string
auth string
}
// Handles PUT requests.
func (m mnm) put(w http.ResponseWriter, r *http.Request) {
auth := r.URL.Query().Get("auth")
object := mux.Vars(r)["object"]
randIdx := rand.Intn(len(m.hashes))
client := m.hashMap[m.hashes[randIdx]]
if m.auth != "" && m.auth != auth {
// If auth configured and the request is not authenticated.
w.WriteHeader(403)
return
}
_, err := client.PutObject("mnm", object, r.Body, "application/octet-stream")
if err != nil {
w.WriteHeader(500)
if errResp, ok := err.(minio.ErrorResponse); ok {
w.Write([]byte(errResp.Message))
}
return
}
// Return the URL with hash info which will be used to fetch the object during
// GET request.
respStr := fmt.Sprintf("%s/mnm/%s/%s", m.address, m.hashes[randIdx], object)
w.Write([]byte(respStr))
}
// Handles GET request.
func (m mnm) get(w http.ResponseWriter, r *http.Request) {
hash := mux.Vars(r)["hash"]
object := mux.Vars(r)["object"]
auth := r.URL.Query().Get("auth")
if m.auth != "" && m.auth != auth {
// If auth configured and the request is not authenticated.
w.WriteHeader(403)
return
}
client := m.hashMap[hash]
reader, err := client.GetObject("mnm", object)
if err != nil {
w.WriteHeader(500)
if errResp, ok := err.(minio.ErrorResponse); ok {
w.Write([]byte(errResp.Message))
}
return
}
_, err = io.Copy(w, reader)
if err != nil {
// In case no data was written, return error.
w.WriteHeader(500)
}
}
// Parse command line and start the mnm server.
func startMnm(ctx *cli.Context) error {
confPath := path.Join(ctx.GlobalString("config-dir"), "config.json")
configContents, err := ioutil.ReadFile(confPath)
if err != nil {
log.Fatal("Unable to read configuration file: ", err)
}
config := Config{}
err = json.Unmarshal(configContents, &config)
if err != nil {
log.Fatal("Unable to parse configuration file: ", err)
}
address := ctx.GlobalString("address")
m := mnm{hashMap: make(map[string]*minio.Client), address: address, auth: config.Auth}
for _, host := range config.Endpoints {
url, err := url.Parse(host.URL)
if err != nil {
log.Fatalf("Unable to parse %s: %s", host.URL, err)
}
client, err := minio.New(url.Host, host.AccessKey, host.SecretKey, url.Scheme == "https")
if err != nil {
log.Fatal("Minio client init failed: ", err)
}
sum := fmt.Sprintf("%x", md5.Sum([]byte(url.Host)))
m.hashMap[sum] = client
m.hashes = append(m.hashes, sum)
}
r := mux.NewRouter()
r.Methods("GET").Path("/mnm/{hash}/{object:.+}").Queries("auth", "{auth:.*}").HandlerFunc(m.get)
r.Methods("GET").Path("/mnm/{hash}/{object:.+}").HandlerFunc(m.get)
r.Methods("PUT").Path("/mnm/{object:.+}").Queries("auth", "{auth:.*}").HandlerFunc(m.put)
r.Methods("PUT").Path("/mnm/{object:.+}").HandlerFunc(m.put)
return http.ListenAndServe(address, r)
}
const globalMnmConfigDir = ".mnm"
// getConfigPath get server config path
func mustGetConfigDir() string {
homeDir, err := homedir.Dir()
if err != nil {
log.Fatal("Unable to get the home directory", err)
}
return filepath.Join(homeDir, globalMnmConfigDir)
}
func main() {
app := cli.NewApp()
app.Usage = "Minio Gateway"
app.Author = "Minio.io"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "address",
Value: "localhost:8000",
Usage: "Local bind address",
},
cli.StringFlag{
Name: "config-dir, C",
Value: mustGetConfigDir(),
Usage: "Path to the configuration directory",
},
}
app.Before = startMnm
app.RunAndExitOnError()
}