Add API basis
This commit is contained in:
parent
21e4432fad
commit
a5dc600f28
3 changed files with 123 additions and 0 deletions
85
admin/api.go
Normal file
85
admin/api.go
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DispatchFunction func([]string, []byte) (interface{}, error)
|
||||||
|
|
||||||
|
var apiRouting = map[string]*(map[string]DispatchFunction){
|
||||||
|
"version": &ApiVersionRouting,
|
||||||
|
//"images": &ApiImagesRouting,
|
||||||
|
//"users": &ApiUsersRouting,
|
||||||
|
}
|
||||||
|
|
||||||
|
func ApiRouting(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("Handling %s request from %s: %s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||||
|
|
||||||
|
// Extract URL arguments
|
||||||
|
var sURL = strings.Split(r.URL.Path, "/")
|
||||||
|
if sURL[len(sURL)-1] == "" && len(sURL) > 2 {
|
||||||
|
// Remove trailing /
|
||||||
|
sURL = sURL[:len(sURL)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
var ret interface{}
|
||||||
|
var err error = nil
|
||||||
|
|
||||||
|
// Read the body
|
||||||
|
if r.ContentLength < 0 || r.ContentLength > 6553600 {
|
||||||
|
http.Error(w, fmt.Sprintf("{errmsg:\"Request too large or request size unknown\"}", err), http.StatusRequestEntityTooLarge)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var body []byte
|
||||||
|
if r.ContentLength > 0 {
|
||||||
|
tmp := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
n, err := r.Body.Read(tmp)
|
||||||
|
for j := 0; j < n; j++ {
|
||||||
|
body = append(body, tmp[j])
|
||||||
|
}
|
||||||
|
if err != nil || n <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route request
|
||||||
|
if len(sURL) > 1 {
|
||||||
|
if h, ok := apiRouting[sURL[1]]; ok {
|
||||||
|
if f, ok := (*h)[r.Method]; ok {
|
||||||
|
ret, err = f(sURL[2:], body)
|
||||||
|
} else {
|
||||||
|
err = errors.New(fmt.Sprintf("Invalid action (%s) provided for %s.", r.Method, sURL[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = errors.New("No action provided.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format response
|
||||||
|
resStatus := http.StatusOK
|
||||||
|
if err != nil {
|
||||||
|
ret = map[string]string{"errmsg": err.Error()}
|
||||||
|
resStatus = http.StatusBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
if ret == nil {
|
||||||
|
ret = map[string]string{"errmsg": "Page not found"}
|
||||||
|
resStatus = http.StatusNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
if j, err := json.Marshal(ret); err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("{errmsg:\"%q\"}", err), http.StatusInternalServerError)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(resStatus)
|
||||||
|
w.Write(j)
|
||||||
|
}
|
||||||
|
}
|
11
admin/api_version.go
Normal file
11
admin/api_version.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import ()
|
||||||
|
|
||||||
|
var ApiVersionRouting = map[string]DispatchFunction{
|
||||||
|
"GET": showVersion,
|
||||||
|
}
|
||||||
|
|
||||||
|
func showVersion(args []string, body []byte) (interface{}, error) {
|
||||||
|
return map[string]interface{}{"version": 0.1}, nil
|
||||||
|
}
|
27
admin/main.go
Normal file
27
admin/main.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var SubmissionDir string
|
||||||
|
var BaseURL string
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
|
||||||
|
var _ = flag.String("db", "fic.db", "Path to the DB")
|
||||||
|
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
|
||||||
|
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
log.Println("Registering handlers...")
|
||||||
|
http.HandleFunc("/", ApiRouting)
|
||||||
|
|
||||||
|
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||||
|
if err := http.ListenAndServe(*bind, nil); err != nil {
|
||||||
|
log.Fatal("Unable to listen and serve: ", err)
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue