nanoapi-crud-postgres
Sample API Setup
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/digitalcircle-com-br/nanoapi"
napi "github.com/digitalcircle-com-br/nanoapi"
nauth "github.com/digitalcircle-com-br/nanoapi-auth-postgres"
crud "github.com/digitalcircle-com-br/nanoapi-crud-postgres"
nsession "github.com/digitalcircle-com-br/nanoapi-session-redis"
)
type Master struct {
crud.BaseVO
Txt string
Details []Detail `gorm:"many2many:master_detail;"`
}
type Detail struct {
crud.BaseVO
Txt string
}
type Detail2 struct {
crud.BaseVO
Txt string
}
type BRequest struct {
A string `json:"a"`
B time.Time `json:"b"`
C int `json:"c"`
}
type BResponse struct {
ARes string `json:"ares"`
BRes time.Time `json:"bres"`
}
func main() {
napi.Setup() //ligando o motor
crud.Setup()
nauth.Setup() //plugin para auth no postgres
nsession.Setup() //plugin de sessao no redis
crud.Register("d", &Detail{})
crud.Register("d2", &Detail2{})
crud.Register("m", &Master{})
m := Master{Details: make([]Detail, 0), Txt: "Master A"}
m.SetTenant("root")
for i := 0; i < 10; i++ {
d := Detail{Txt: fmt.Sprintf("Detail: %v", i)}
m.Details = append(m.Details, d)
crud.Db().Save(&d)
d2 := Detail2{Txt: fmt.Sprintf("Detail: %v", i)}
crud.Db().Save(&d2)
// m.D2 = append(m.D2, d2)
}
crud.Db().Save(&m)
nauth.SetupPerm() //habilita permissões
nauth.AddPerm("*", "a")
nauth.AddPerm("+", "b", "crud_m", "crud_d")
//exemplo de middleware
napi.AddMW(func(nh http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
cmd := napi.ReqCmd(r)
log.Printf("BEFORE - " + cmd)
nh(rw, r)
log.Printf("AFTER - " + cmd)
}
})
//handler a é privado
nanoapi.H("a", func(ctx context.Context, i string) (string, error) {
log.Printf("Got: " + i)
return "GOT FROM A: " + i, nil
})
//handler b é publico
nanoapi.H("b", func(ctx context.Context, in BRequest) (BResponse, error) {
nanoapi.Log(nanoapi.CtxReq(ctx).Header.Get("Content-Type"))
nanoapi.Log(nanoapi.CtxReq(ctx).URL.RawQuery)
mpf := nanoapi.CtxMPF(ctx)
for k, fs := range mpf.File {
for _, f := range fs {
r, err := f.Open()
if err != nil {
return BResponse{}, err
}
defer r.Close()
nanoapi.Log("Reading: %s:%s, %v", k, f.Filename, f.Size)
io.Copy(log.Writer(), r)
}
}
log.Printf("%#v", in)
res := BResponse{ARes: "Got this", BRes: time.Now()}
return res, nil
})
//sera que mudo pra nanoapi.ListenAndServe()?
http.ListenAndServe(":8080", nanoapi.Mux())
}
Sample Http Operations
###
# @name LOGIN
POST http://localhost:8080/login
Content-Type: application/json
{
"username":"root",
"password":"Aa1234"
}
###
# @name LOGOUT
POST http://localhost:8080/logout
Content-Type: application/json
Cookie: {{cookie}}
{
"username":"root",
"password":"Aa1234"
}
###
# @name CALL_CRUD_D_R
POST http://localhost:8080/crud_d
Content-Type: application/json
Cookie: {{cookie}}
{
"op":"R",
"preload":true
}
###
# @name CALL_CRUD_D_C
POST http://localhost:8080/crud_d
Content-Type: application/json
Cookie: {{cookie}}
{
"op":"C",
"data":{"txt":"from test"}
}
###
# @name CALL_CRUD_M_U
POST http://localhost:8080/crud_m
Content-Type: application/json
Cookie: {{cookie}}
{
"op":"U",
"id":1,
"data":{"id":1,"details":[{"id":21}]}
}
###
# @name CALL_CRUD_M_ASSOC
POST http://localhost:8080/crud_m
Content-Type: application/json
Cookie: {{cookie}}
{
"op":"A",
"association":"master_detail",
"masterfield":"master_id",
"detailfield":"detail_id",
"masterid":1,
"detailid":1,
"id":1,
"data":{"id":21}
}
###
# @name CALL_CRUD_M_DESSOC
POST http://localhost:8080/crud_m
Content-Type: application/json
Cookie: {{cookie}}
{
"op":"X",
"association":"master_detail",
"masterfield":"master_id",
"detailfield":"detail_id",
"masterid":1,
"detailid":1,
"id":1,
"data":{"id":21}
}