Skip to content

Commit

Permalink
New config route for editing & getting config
Browse files Browse the repository at this point in the history
Now that config no longer handles sensitive account info it's fine to pass this to client. Debating whether to allow anyone to fetch config, but for now restricted to level 2 user accounts.
  • Loading branch information
gmemstr committed Jan 8, 2018
1 parent b0e7a69 commit db24981
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
59 changes: 59 additions & 0 deletions admin/admin.go
Expand Up @@ -47,6 +47,65 @@ type Config struct {
* able to have access to, mostly user management.
*/

func ConfigurationManager() common.Handler {

return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {

if r.Method == "GET" {
return common.ReadAndServeFile("assets/config/config.json", w)
}
fmt.Println(r.Host)
err := r.ParseMultipartForm(32 << 20)
if err != nil {
return &common.HTTPError{
Message: err.Error(),
StatusCode: http.StatusBadRequest,
}
}
file, handler, err := r.FormFile("image")
if err == nil {
defer file.Close()
fmt.Println(handler.Header)
f, err := os.OpenFile("./assets/web/static/podcastimage.png", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
w.Write([]byte("<script>window.location = '/admin#failed';</script>"))

fmt.Println(err)
return nil
}
defer f.Close()
io.Copy(f, file)
} else {
// Do nothing, assume no image was uploaded :(
}
newconfig := Config{
strings.Join(r.Form["feedname"], ""),
strings.Join(r.Form["host"], ""),
strings.Join(r.Form["email"], ""),
strings.Join(r.Form["description"], ""),
r.Host + "/assets/podcastimage.png",
"http://" + r.Host,
}
newconfigjson, err := json.Marshal(newconfig)
if err != nil {
return &common.HTTPError{
Message: err.Error(),
StatusCode: http.StatusBadRequest,
}
}
err = ioutil.WriteFile("./assets/config/config.json", newconfigjson, 0644)
if err != nil {
return &common.HTTPError{
Message: err.Error(),
StatusCode: http.StatusBadRequest,
}
}
w.Write([]byte("<script>window.location = '/admin#/Settings#saved';</script>"))
return nil
}

}

func AddUser() common.Handler {

return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
Expand Down
5 changes: 5 additions & 0 deletions router/router.go
Expand Up @@ -120,6 +120,11 @@ func Init() *mux.Router {
admin.ListUsers(),
)).Methods("GET")

r.Handle("/admin/settings", Handle(
auth.RequireAuthorization(2),
admin.ConfigurationManager(),
)).Methods("GET", "POST")

return r
}

Expand Down

0 comments on commit db24981

Please sign in to comment.