minio/pkg/api/webuiapi/webuiapi.go

130 lines
3.1 KiB
Go
Raw Normal View History

/*
2015-02-08 05:54:21 -05:00
* Mini Object Storage, (C) 2015 Minio, 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 webuiapi
import (
"bytes"
"encoding/json"
2015-02-05 17:19:48 -05:00
"log"
"net/http"
2015-02-05 17:19:48 -05:00
"path"
"github.com/gorilla/mux"
2015-02-05 17:19:48 -05:00
"github.com/minio-io/minio/pkg/utils/config"
"github.com/minio-io/minio/pkg/utils/crypto/keys"
)
const (
2015-03-06 00:07:19 -05:00
defaultWeb = "polygon"
)
2015-03-06 00:07:19 -05:00
type webAPI struct {
2015-02-05 17:19:48 -05:00
conf config.Config
webPath string
}
// No encoder interface exists, so we create one.
type encoder interface {
Encode(v interface{}) error
}
2015-03-06 00:07:19 -05:00
// HTTPHandler - http wrapper handler
func HTTPHandler() http.Handler {
mux := mux.NewRouter()
2015-03-06 00:07:19 -05:00
var api = webAPI{}
2015-02-01 05:49:09 -05:00
2015-02-05 17:19:48 -05:00
if err := api.conf.SetupConfig(); err != nil {
log.Fatal(err)
}
2015-03-06 00:07:19 -05:00
api.webPath = path.Join(api.conf.GetConfigPath(), defaultWeb)
2015-02-05 17:19:48 -05:00
mux.Handle("/{polygon:.*}", http.FileServer(http.Dir(api.webPath))).Methods("GET")
mux.HandleFunc("/access", api.accessHandler).Methods("POST")
return mux
}
func writeResponse(w http.ResponseWriter, response interface{}) []byte {
var bytesBuffer bytes.Buffer
var encoder encoder
w.Header().Set("Content-Type", "application/json")
encoder = json.NewEncoder(&bytesBuffer)
w.Header().Set("Server", "Minio Management Console")
w.Header().Set("Connection", "close")
encoder.Encode(response)
return bytesBuffer.Bytes()
}
2015-03-06 00:07:19 -05:00
func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
2015-02-05 17:19:48 -05:00
var err error
var accesskey, secretkey []byte
username := req.FormValue("username")
if len(username) <= 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
err = web.conf.ReadConfig()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if web.conf.IsUserExists(username) {
w.WriteHeader(http.StatusConflict)
return
}
2015-02-05 17:19:48 -05:00
var user = config.User{}
user.Name = username
2015-03-05 23:08:29 -05:00
accesskey, err = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID)
2015-02-05 17:19:48 -05:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
user.AccessKey = string(accesskey)
2015-03-05 23:08:29 -05:00
secretkey, err = keys.GenerateRandomBase64(keys.MinioSecretID)
2015-02-05 17:19:48 -05:00
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
user.SecretKey = string(secretkey)
web.conf.AddUser(user)
err = web.conf.WriteConfig()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
err = web.conf.ReadConfig()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
// Get user back for sending it over HTTP reply
user = web.conf.GetUser(username)
w.Write(writeResponse(w, user))
}