github.com/simpleiot/simpleiot@v0.18.3/api/v1.go (about)

     1  package api
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  // V1 handles v1 api requests
     8  type V1 struct {
     9  	GroupsHandler http.Handler
    10  	UsersHandler  http.Handler
    11  	NodesHandler  http.Handler
    12  	AuthHandler   http.Handler
    13  	MsgHandler    http.Handler
    14  }
    15  
    16  // Top level handler for http requests in the coap-server process
    17  func (h *V1) ServeHTTP(res http.ResponseWriter, req *http.Request) {
    18  	var head string
    19  	head, req.URL.Path = ShiftPath(req.URL.Path)
    20  	switch head {
    21  	case "nodes":
    22  		h.NodesHandler.ServeHTTP(res, req)
    23  	case "auth":
    24  		h.AuthHandler.ServeHTTP(res, req)
    25  	default:
    26  		http.Error(res, "Not Found", http.StatusNotFound)
    27  	}
    28  }
    29  
    30  // NewV1Handler returns a handle for V1 API
    31  func NewV1Handler(args ServerArgs) http.Handler {
    32  	return &V1{
    33  		NodesHandler: NewNodesHandler(args.JwtAuth,
    34  			args.AuthToken, args.Nc),
    35  		AuthHandler: NewAuthHandler(args.Nc),
    36  	}
    37  }