|
|
|
|
|
|
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
|
|
|
"encoding/json" |
|
|
"html/template" |
|
|
"html/template" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"path/filepath" |
|
|
"path/filepath" |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Response struct { |
|
|
|
|
|
Code int |
|
|
|
|
|
Text string |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func JSON(next http.Handler) http.Handler { |
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8") |
|
|
|
|
|
next.ServeHTTP(w, r) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func fetch(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
var buffer = new(bytes.Buffer) |
|
|
|
|
|
var response = Response{Code: http.StatusOK, Text: http.StatusText(http.StatusOK)} |
|
|
|
|
|
if err := json.NewEncoder(buffer).Encode(response); err == nil { |
|
|
|
|
|
w.Write(buffer.Bytes()) |
|
|
|
|
|
} else { |
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadGateway) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func create(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
w.WriteHeader(http.StatusCreated) |
|
|
|
|
|
var buffer = new(bytes.Buffer) |
|
|
|
|
|
var response = Response{Code: http.StatusCreated, Text: http.StatusText(http.StatusCreated)} |
|
|
|
|
|
if err := json.NewEncoder(buffer).Encode(response); err == nil { |
|
|
|
|
|
w.Write(buffer.Bytes()) |
|
|
|
|
|
} else { |
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadGateway) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func update(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
w.WriteHeader(http.StatusAccepted) |
|
|
|
|
|
var buffer = new(bytes.Buffer) |
|
|
|
|
|
var response = Response{Code: http.StatusAccepted, Text: http.StatusText(http.StatusAccepted)} |
|
|
|
|
|
if err := json.NewEncoder(buffer).Encode(response); err == nil { |
|
|
|
|
|
w.Write(buffer.Bytes()) |
|
|
|
|
|
} else { |
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadGateway) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func remove(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
w.WriteHeader(http.StatusGone) |
|
|
|
|
|
var buffer = new(bytes.Buffer) |
|
|
|
|
|
var response = Response{Code: http.StatusGone, Text: http.StatusText(http.StatusGone)} |
|
|
|
|
|
if err := json.NewEncoder(buffer).Encode(response); err == nil { |
|
|
|
|
|
w.Write(buffer.Bytes()) |
|
|
|
|
|
} else { |
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadGateway) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func main() { |
|
|
func main() { |
|
|
rex.Use(livereload.Middleware) |
|
|
rex.Use(livereload.Middleware) |
|
|
rex.Get("/", Index) |
|
|
rex.Get("/", Index) |
|
|
|
|
|
|
|
|
|
|
|
apis := rex.Group("/v1/") |
|
|
|
|
|
apis.Use(JSON) |
|
|
|
|
|
apis.Get("/", fetch) |
|
|
|
|
|
apis.Post("/", create) |
|
|
|
|
|
apis.Put("/", update) |
|
|
|
|
|
apis.Delete("/", remove) |
|
|
|
|
|
|
|
|
rex.Run() |
|
|
rex.Run() |
|
|
} |
|
|
} |