48d7be7eab
The API generator was generating invalid code when types were defined in a main package because the generated Go code was defining in import from it. This commit update the Go generator to panic with a explicit error message if that situation happens. The commit also add a new endpoint to the example with a named types (i.e. no anonymous) to show that the Generator works fine with them. Change-Id: Ieddd89c67048de50516f7ac7787d602660dc4a54
156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
// AUTOGENERATED BY private/apigen
|
|
// DO NOT EDIT.
|
|
|
|
package example
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
"github.com/zeebo/errs"
|
|
"go.uber.org/zap"
|
|
|
|
"storj.io/common/uuid"
|
|
"storj.io/storj/private/api"
|
|
"storj.io/storj/private/apigen/example/myapi"
|
|
)
|
|
|
|
const dateLayout = "2006-01-02T15:04:05.999Z"
|
|
|
|
var ErrDocsAPI = errs.Class("example docs api")
|
|
|
|
type DocumentsService interface {
|
|
GetOne(ctx context.Context, path string) (*myapi.Document, api.HTTPError)
|
|
UpdateContent(ctx context.Context, path string, id uuid.UUID, date time.Time, request struct {
|
|
Content string "json:\"content\""
|
|
}) (*struct {
|
|
ID uuid.UUID "json:\"id\""
|
|
Date time.Time "json:\"date\""
|
|
PathParam string "json:\"pathParam\""
|
|
Body string "json:\"body\""
|
|
}, api.HTTPError)
|
|
}
|
|
|
|
// DocumentsHandler is an api handler that exposes all docs related functionality.
|
|
type DocumentsHandler struct {
|
|
log *zap.Logger
|
|
mon *monkit.Scope
|
|
service DocumentsService
|
|
auth api.Auth
|
|
}
|
|
|
|
func NewDocuments(log *zap.Logger, mon *monkit.Scope, service DocumentsService, router *mux.Router, auth api.Auth) *DocumentsHandler {
|
|
handler := &DocumentsHandler{
|
|
log: log,
|
|
mon: mon,
|
|
service: service,
|
|
auth: auth,
|
|
}
|
|
|
|
docsRouter := router.PathPrefix("/api/v0/docs").Subrouter()
|
|
docsRouter.HandleFunc("/{path}", handler.handleGetOne).Methods("GET")
|
|
docsRouter.HandleFunc("/{path}", handler.handleUpdateContent).Methods("POST")
|
|
|
|
return handler
|
|
}
|
|
|
|
func (h *DocumentsHandler) handleGetOne(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
var err error
|
|
defer h.mon.Task()(&ctx)(&err)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
path, ok := mux.Vars(r)["path"]
|
|
if !ok {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("missing path route param"))
|
|
return
|
|
}
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
if err != nil {
|
|
h.auth.RemoveAuthCookie(w)
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
|
|
retVal, httpErr := h.service.GetOne(ctx, path)
|
|
if httpErr.Err != nil {
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
return
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
if err != nil {
|
|
h.log.Debug("failed to write json GetOne response", zap.Error(ErrDocsAPI.Wrap(err)))
|
|
}
|
|
}
|
|
|
|
func (h *DocumentsHandler) handleUpdateContent(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
var err error
|
|
defer h.mon.Task()(&ctx)(&err)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
idParam := r.URL.Query().Get("id")
|
|
if idParam == "" {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'id' can't be empty"))
|
|
return
|
|
}
|
|
|
|
id, err := uuid.FromString(idParam)
|
|
if err != nil {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
dateParam := r.URL.Query().Get("date")
|
|
if dateParam == "" {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'date' can't be empty"))
|
|
return
|
|
}
|
|
|
|
date, err := time.Parse(dateLayout, dateParam)
|
|
if err != nil {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
path, ok := mux.Vars(r)["path"]
|
|
if !ok {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, errs.New("missing path route param"))
|
|
return
|
|
}
|
|
|
|
payload := struct {
|
|
Content string "json:\"content\""
|
|
}{}
|
|
if err = json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
api.ServeError(h.log, w, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
ctx, err = h.auth.IsAuthenticated(ctx, r, true, true)
|
|
if err != nil {
|
|
h.auth.RemoveAuthCookie(w)
|
|
api.ServeError(h.log, w, http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
|
|
retVal, httpErr := h.service.UpdateContent(ctx, path, id, date, payload)
|
|
if httpErr.Err != nil {
|
|
api.ServeError(h.log, w, httpErr.Status, httpErr.Err)
|
|
return
|
|
}
|
|
|
|
err = json.NewEncoder(w).Encode(retVal)
|
|
if err != nil {
|
|
h.log.Debug("failed to write json UpdateContent response", zap.Error(ErrDocsAPI.Wrap(err)))
|
|
}
|
|
}
|