admin: add public interface management
This commit is contained in:
parent
7240cbb414
commit
416ad65c87
6 changed files with 294 additions and 0 deletions
80
admin/api/public.go
Normal file
80
admin/api/public.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/public.json", apiHandler(getPublic))
|
||||
router.DELETE("/api/public.json", apiHandler(deletePublic))
|
||||
router.PUT("/api/public.json", apiHandler(savePublic))
|
||||
}
|
||||
|
||||
type FICPublicScene struct {
|
||||
Type string `json:"type"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
}
|
||||
|
||||
func readPublic(path string) ([]FICPublicScene, error) {
|
||||
var s []FICPublicScene
|
||||
if fd, err := os.Open(path); err != nil {
|
||||
return s, err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jdec := json.NewDecoder(fd)
|
||||
|
||||
if err := jdec.Decode(&s); err != nil {
|
||||
return s, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
|
||||
func savePublicTo(path string, s []FICPublicScene) error {
|
||||
if fd, err := os.Create(path); err != nil {
|
||||
return err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jenc := json.NewEncoder(fd)
|
||||
|
||||
if err := jenc.Encode(s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func getPublic(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
if _, err := os.Stat(path.Join(TeamsDir, "_public", "public.json")); !os.IsNotExist(err) {
|
||||
return readPublic(path.Join(TeamsDir, "_public", "public.json"))
|
||||
} else {
|
||||
return []FICPublicScene{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func deletePublic(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
if err := savePublicTo(path.Join(TeamsDir, "_public", "public.json"), []FICPublicScene{}); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return []FICPublicScene{}, err
|
||||
}
|
||||
}
|
||||
|
||||
func savePublic(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
var scenes []FICPublicScene
|
||||
if err := json.Unmarshal(body, &scenes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := savePublicTo(path.Join(TeamsDir, "_public", "public.json"), scenes); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return scenes, err
|
||||
}
|
||||
}
|
Reference in a new issue