Add list playgrounds and create/update playground endpoints. (#221)
User is expected to send a predefined token as basic auth password.
This commit is contained in:
committed by
Marcos Nils
parent
95e4aaa5f7
commit
0ad73acbb9
@@ -75,6 +75,8 @@ func Register(extend HandlerExtender) {
|
||||
r.HandleFunc("/oauth/providers", ListProviders).Methods("GET")
|
||||
r.HandleFunc("/oauth/providers/{provider}/login", Login).Methods("GET")
|
||||
r.HandleFunc("/oauth/providers/{provider}/callback", LoginCallback).Methods("GET")
|
||||
r.HandleFunc("/playgrounds", NewPlayground).Methods("PUT")
|
||||
r.HandleFunc("/playgrounds", ListPlaygrounds).Methods("GET")
|
||||
|
||||
corsRouter.HandleFunc("/", NewSession).Methods("POST")
|
||||
|
||||
|
||||
65
handlers/new_playground.go
Normal file
65
handlers/new_playground.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/play-with-docker/play-with-docker/config"
|
||||
"github.com/play-with-docker/play-with-docker/pwd/types"
|
||||
)
|
||||
|
||||
func NewPlayground(rw http.ResponseWriter, req *http.Request) {
|
||||
if !validateToken(req) {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
var playground types.Playground
|
||||
|
||||
err := json.NewDecoder(req.Body).Decode(&playground)
|
||||
if err != nil {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprintf(rw, "Error creating playground. Got: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
newPlayground, err := core.PlaygroundNew(playground)
|
||||
if err != nil {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Fprintf(rw, "Error creating playground. Got: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(rw).Encode(newPlayground)
|
||||
}
|
||||
|
||||
func ListPlaygrounds(rw http.ResponseWriter, req *http.Request) {
|
||||
if !validateToken(req) {
|
||||
rw.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
playgrounds, err := core.PlaygroundList()
|
||||
if err != nil {
|
||||
log.Printf("Error listing playgrounds. Got: %v\n", err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(rw).Encode(playgrounds)
|
||||
}
|
||||
|
||||
func validateToken(req *http.Request) bool {
|
||||
_, password, ok := req.BasicAuth()
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if password != config.AdminToken {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user