Reply back AccessKey, Secretkey through json

master
Harshavardhana 10 years ago
parent 95f214c99b
commit 72d0999dcb
  1. 41
      pkg/api/webuiapi/webuiapi.go
  2. 18
      pkg/utils/config/config.go

@ -17,6 +17,8 @@
package webuiapi
import (
"bytes"
"encoding/json"
"log"
"net/http"
"path"
@ -35,6 +37,11 @@ type webUiApi struct {
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{}
@ -49,6 +56,17 @@ func HttpHandler() http.Handler {
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
@ -58,6 +76,18 @@ func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
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
@ -77,13 +107,6 @@ func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
}
user.SecretKey = string(secretkey)
err = web.conf.ReadConfig()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
web.conf.AddUser(user)
err = web.conf.WriteConfig()
if err != nil {
@ -99,5 +122,7 @@ func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
return
}
log.Println("Config:", web.conf.Users)
// Get user back for sending it over HTTP reply
user = web.conf.GetUser(username)
w.Write(writeResponse(w, user))
}

@ -45,6 +45,24 @@ func (c *Config) GetConfigPath() string {
return c.configPath
}
func (c *Config) IsUserExists(username string) bool {
for _, user := range c.Users {
if user.Name == username {
return true
}
}
return false
}
func (c *Config) GetUser(username string) User {
for _, user := range c.Users {
if user.Name == username {
return user
}
}
return User{}
}
func (c *Config) AddUser(user User) {
var currentUsers map[string]User
if len(c.Users) == 0 {

Loading…
Cancel
Save