1
0
Fork 0

Basic HTTP server

This commit is contained in:
Gregory Eremin 2015-01-19 18:23:04 +07:00
parent 8f3e2b11bc
commit 847c0d2c9e
1 changed files with 21 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package confection
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"strconv" "strconv"
) )
@ -24,10 +25,30 @@ func (s *server) start() {
} }
func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/fields.json":
s.fieldsHandler(w, req)
case "/save":
s.saveHandler(w, req)
default:
http.NotFound(w, req)
}
}
func (s *server) fieldsHandler(w http.ResponseWriter, req *http.Request) {
jsn, err := json.Marshal(s.manager.conf.meta("")) jsn, err := json.Marshal(s.manager.conf.meta(""))
if err != nil { if err != nil {
panic(err) panic(err)
} }
w.Header().Add("Content-Type", "application/json; charset=utf8")
w.Write(jsn) w.Write(jsn)
} }
func (s *server) saveHandler(w http.ResponseWriter, req *http.Request) {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
s.manager.importJson(b)
}