diff --git a/pkg/api/webuiapi/webuiapi.go b/pkg/api/webuiapi/webuiapi.go index 2b9ae8ea4..389e5061c 100644 --- a/pkg/api/webuiapi/webuiapi.go +++ b/pkg/api/webuiapi/webuiapi.go @@ -17,25 +17,80 @@ package webuiapi import ( - "fmt" + "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 } func HttpHandler() http.Handler { mux := mux.NewRouter() var api = webUiApi{} - mux.StrictSlash(true) - mux.HandleFunc("/", api.homeHandler).Methods("GET") + 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 (web *webUiApi) homeHandler(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Server", "Minio Management Console") - fmt.Fprintln(w, "Welcome!") +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 + } + + 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 + } + + log.Println("Config:", web.conf.Users) }