Templates
This commit is contained in:
+57
-48
@@ -1,63 +1,72 @@
|
||||
package goapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
Mux *http.ServeMux
|
||||
Perms map[string]string
|
||||
import "crypto"
|
||||
|
||||
var perms map[string]string
|
||||
|
||||
func init() {
|
||||
perms = make(map[string]string)
|
||||
|
||||
perms["POST_/someapi"] = "ASD"
|
||||
|
||||
perms["DELETE_/someapi"] = "ASD"
|
||||
|
||||
perms["GET_/someapi"] = "ASD"
|
||||
|
||||
perms["PUT_/someapi"] = "ASD"
|
||||
|
||||
}
|
||||
|
||||
func (a *API) GetPerm(r *http.Request) string {
|
||||
return a.Perms[r.Method+"_"+strings.Split(r.RequestURI, "?")[0]]
|
||||
}
|
||||
|
||||
func Init() API {
|
||||
mux := &http.ServeMux{}
|
||||
|
||||
ret := API{
|
||||
Mux: mux,
|
||||
Perms: make(map[string]string),
|
||||
func GetPerm(c *gin.Context) string {
|
||||
perm, ok := perms[c.Request.Method+"_"+c.Request.URL.Path]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
h_SomeAPI(w, r)
|
||||
default:
|
||||
http.Error(w, "Method not allowed", 500)
|
||||
}
|
||||
})
|
||||
return ret
|
||||
return perm
|
||||
}
|
||||
|
||||
func h_SomeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
func Build(r *gin.Engine) *gin.Engine {
|
||||
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(r.Context(), "REQ", r)
|
||||
ctx = context.WithValue(ctx, "RES", w)
|
||||
var req string
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
r.DELETE("/someapi", func(c *gin.Context) {
|
||||
var req *crypto.Hash
|
||||
c.BindJSON(req)
|
||||
res, err := SomeAPI2(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
c.Error(err)
|
||||
}
|
||||
}
|
||||
c.JSON(200, res)
|
||||
})
|
||||
r.GET("/someapi", func(c *gin.Context) {
|
||||
var req string
|
||||
c.BindJSON(&req)
|
||||
res, err := SomeGET(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
c.JSON(200, res)
|
||||
})
|
||||
r.POST("/someapi", func(c *gin.Context) {
|
||||
var req string
|
||||
c.BindJSON(&req)
|
||||
res, err := SomeAPI(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
c.JSON(200, res)
|
||||
})
|
||||
r.PUT("/someapi", func(c *gin.Context) {
|
||||
var req string
|
||||
c.BindJSON(&req)
|
||||
res, err := SomePUT(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
c.JSON(200, res)
|
||||
})
|
||||
|
||||
res, err := SomeAPI(ctx, req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
w.Header().Add("Content-Type", "Application/json")
|
||||
err = json.NewEncoder(w).Encode(res)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
+21
-7
@@ -35,11 +35,13 @@ func SomeAPI(ctx context.Context, s string) (out string, err error) {
|
||||
@API
|
||||
@PATH: /someapi
|
||||
@PERM: ASD
|
||||
@VERB: PUT
|
||||
@VERB: GET
|
||||
*/
|
||||
//func SomeAPI2(ctx context.Context, s dcsession.Session) ([]string, error) {
|
||||
// return nil, nil
|
||||
//}
|
||||
func SomeGET(ctx context.Context, s string) (out string, err error) {
|
||||
print("Got:" + s)
|
||||
out = time.Now().String() + " - Hey Ya!"
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
@API
|
||||
@@ -47,6 +49,18 @@ func SomeAPI(ctx context.Context, s string) (out string, err error) {
|
||||
@PERM: ASD
|
||||
@VERB: PUT
|
||||
*/
|
||||
//func SomeAPI3(ctx context.Context, a time.Time) ([]string, error) {
|
||||
// return nil, nil
|
||||
//}
|
||||
func SomePUT(ctx context.Context, s string) (out string, err error) {
|
||||
print("Got:" + s)
|
||||
out = time.Now().String() + " - Hey Ya!"
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
@API
|
||||
@PATH: /someapi
|
||||
@PERM: ASD
|
||||
@VERB: DELETE
|
||||
*/
|
||||
func SomeAPI2(ctx context.Context, s *crypto.Hash) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user