crhntr / jsonapi

http.Handler for {json:api}
0 stars 0 forks source link

Implement Mux #3

Open crhntr opened 6 years ago

crhntr commented 6 years ago

Should feel like standard library

mux := http.NewServeMux()
mux.HandleFunc("/developer", func(res http.ResponseWriter, req *http.Request) {})

// ...

mux.ServeHTTP()

Here is some idea as to how this will look

package main

import (
    "net/http"
    "os"

    "github.com/crhntr/jsonapi"
)

func main() {
    var mux jsonapi.Mux

    type Developer struct {
        ID   string `json:"-"`
        Name string `json:"name"`

        ManagerID string `json:"-"` // one-to-many recursive
        TeamID    string `json:"-"` // one-to-many
    }

    // Developer
    mux.HandleFetchOne("developer", func(res jsonpai.FetchOneResonder, req *http.Request, idStr string) {
        // ...
    })

    mux.HandleFetchMany("developer", func(res jsonpai.FetchManyResponder, req *http.Request) {
        // ...
    })

    mux.HandleCreate("developer", func(res jsonpai.CreateResponder, req *http.Request) {
        // ...
    })

    mux.HandleUpdate("developer", func(res jsonpai.UpdateResponder, req *http.Request, idStr string) {
        // ...
    })

    mux.HandleDelete("developer", func(res jsonpai.DeleteResponder, req *http.Request, idStr string) {
        // ...
    })

    http.ListenAndServe(":"+os.Getenv("PORT"), mux)
}
crhntr commented 6 years ago

I've decided to only support a single name per resource (plural and singular should not be used in the same context)

example: /developers and /developer