Added JSON encoding to setup

Uses native Go encoding/json package, plan to move config away from spf13/viper and admin login to SQL for multiple user support.
This commit is contained in:
gmemstr 2017-09-22 08:50:03 -07:00
parent 99cb95d51b
commit 35c77899d8
2 changed files with 37 additions and 15 deletions

View file

@ -11,16 +11,18 @@
<form action="setup" method="post" class="setupform"> <form action="setup" method="post" class="setupform">
<label for="podcastname">Podcast Name</label> <label for="podcastname">Podcast Name</label>
<input type="text" id="podcastname"> <input type="text" id="podcastname" name="podcastname">
<label for="podcasthost">Podcast Host</label> <label for="podcasthost">Podcast Host</label>
<input type="text" id="podcasthost"> <input type="text" id="podcasthost" name="podcasthost">
<label for="podcastemail">Podcast Email</label> <label for="podcastemail">Podcast Email</label>
<input type="text" id="podcastemail"> <input type="text" id="podcastemail" name="podcastemail">
<label for="podcastdescription">Podcast Description</label> <label for="podcastdescription">Podcast Description</label>
<textarea name="" id="podcastdescription" cols="75" rows="5"></textarea> <textarea name="" id="podcastdescription" name="podcastdescription" cols="75" rows="5"></textarea>
<input type="submit" value="Submit">
</form> </form>

View file

@ -3,8 +3,20 @@ package main
import ( import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
// "fmt"
"encoding/json"
"strings"
) )
type NewConfig struct {
Name string
Host string
Email string
Description string
Image string
PodcastUrl string
}
// Serve setup.html and config parameters // Serve setup.html and config parameters
func ServeSetup(w http.ResponseWriter, r *http.Request) { func ServeSetup(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
@ -16,16 +28,24 @@ func ServeSetup(w http.ResponseWriter, r *http.Request) {
w.Write(data) w.Write(data)
} }
if r.Method == "POST" { if r.Method == "POST" {
r.ParseMultipartForm(32 << 20)
} // Parse form and convert to JSON
cnf := NewConfig{
strings.Join(r.Form["podcastname"], ""), // Podcast name
strings.Join(r.Form["podcasthost"], ""), // Podcast host
strings.Join(r.Form["podcastemail"], ""), // Podcast host email
strings.Join(r.Form["podcastdescription"], ""), // Podcast Description
"", // Podcast image
"", // Podcast location
} }
// Write JSON config to file b, err := json.Marshal(cnf)
func WriteConfig() { if err != nil {
panic(err)
} }
// Connect to SQL and create admin user ioutil.WriteFile("config.json", b, 0644)
func CreateAdmin() { w.Write([]byte("Done"))
}
} }