You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
minio/pkg/api/webuiapi/webuiapi.go

128 lines
3.1 KiB

/*
* 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"
"log"
"net/http"
"path"
"github.com/gorilla/mux"
"github.com/minio-io/minio/pkg/utils/config"
"github.com/minio-io/minio/pkg/utils/crypto/keys"
)
const (
DEFAULT_WEB = "polygon"
)
type webUiApi struct {
conf config.Config
webPath string
}
// No encoder interface exists, so we create one.
type encoder interface {
Encode(v interface{}) error
}
func HttpHandler() http.Handler {
mux := mux.NewRouter()
var api = webUiApi{}
if err := api.conf.SetupConfig(); err != nil {
log.Fatal(err)
}
api.webPath = path.Join(api.conf.GetConfigPath(), DEFAULT_WEB)
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()
}
func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
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
}
var user = config.User{}
user.Name = username
accesskey, err = keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
user.AccessKey = string(accesskey)
secretkey, err = keys.GetRandomBase64(keys.MINIO_SECRET_ID)
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))
}