turn into one route

This commit is contained in:
grumbulon 2023-02-04 18:48:00 -05:00 committed by Gitea
parent a83ae1bff3
commit 2426a8b393
3 changed files with 22 additions and 149 deletions

View file

@ -34,8 +34,6 @@ func API() http.Handler {
api.Use(jwtauth.Authenticator)
api.With(setDBMiddleware).Post("/upload", ReceiveFile)
api.With(setDBMiddleware).Post("/parse", ParseZoneFiles)
api.With(setDBMiddleware).Post("/save", SaveZoneFiles)
})
// Open routes

View file

@ -30,8 +30,8 @@ type GenericResponse[T map[string]any] struct {
type genericResponseFields map[string]any
type ndr interface {
Parse() error
Save() error
parse() error
save() error
}
var _ ndr = (*ZoneRequest)(nil)

View file

@ -64,9 +64,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
return
}
name := strings.Split(header.Filename, ".")
if err = util.MakeLocal(name[0], claims["username"].(string), b); err != nil {
if err = util.MakeLocal(header.Filename, claims["username"].(string), b); err != nil {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError, "error": err.Error()})
return
@ -83,11 +81,25 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
&ZoneRequest{
User: claims["username"].(string),
Zone: &Zone{
FileName: fmt.Sprintf("tmpfile-%s-%s", name[0], claims["username"].(string)),
RawFileName: name[0],
FileName: fmt.Sprintf("tmpfile-%s-%s", header.Filename, claims["username"].(string)),
RawFileName: header.Filename,
},
})
zoneFile := newDNSRequest(header.Filename, claims["username"].(string))
if err := zoneFile.parse(); err != nil {
APIError(w, r, genericResponseFields{"message": "Unable to parse zonefile", "status": http.StatusInternalServerError, "error": err.Error()})
return
}
if err := zoneFile.save(); err != nil {
APIError(w, r, genericResponseFields{"message": "Unable to save zonefile", "status": http.StatusInternalServerError, "error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
@ -99,144 +111,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, resp)
}
// Parse godoc
//
// @Summary parse your zonefile
// @Description parse your zonefile
//
// @Description Rate limited: 10 requests every 10 second
// @Description you must specify "Bearer" before entering your token
//
// @Tags DNS
// @Accept mpfd
// @Produce json
// @Param filename query string true "Zonefile name"
// @Success 200 {object} internal.SwaggerGenericResponse[internal.Response]
// @Failure 500 {object} internal.SwaggerGenericResponse[internal.Response] "internalServerError is a 500 server error with a logged error call back"
// @Param Authorization header string true "Bearer Token"
//
// @Security Bearer
//
// @Router /api/parse [post]
func ParseZoneFiles(w http.ResponseWriter, r *http.Request) {
var result internal.ZoneRequest
_, claims, _ := jwtauth.FromContext(r.Context())
err := r.ParseForm()
if err != nil {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError, "error": err.Error()})
return
}
filename := r.Form.Get("filename")
if filename == "" {
APIError(w, r, genericResponseFields{"message": "no filename provided", "status": http.StatusInternalServerError})
return
}
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
if !ok {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError, "error": "unable to connect to DB"})
return
}
db.Where(ZoneRequest{
Zone: &Zone{
RawFileName: filename,
},
User: claims["username"].(string),
}).First(&result)
if result == (internal.ZoneRequest{}) {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError})
return
}
zoneFile := newNDSRequest(result.RawFileName, claims["username"].(string))
if err := zoneFile.Parse(); err != nil {
APIError(w, r, genericResponseFields{"message": "Unable to parse zonefile", "status": http.StatusInternalServerError, "error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusCreated)
resp := internal.Response{
Message: "Successfully parsed zonefile",
}
render.JSON(w, r, resp)
}
func SaveZoneFiles(w http.ResponseWriter, r *http.Request) {
var result internal.ZoneRequest
_, claims, _ := jwtauth.FromContext(r.Context())
err := r.ParseForm()
if err != nil {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError, "error": err.Error()})
return
}
filename := r.Form.Get("filename")
if filename == "" {
APIError(w, r, genericResponseFields{"message": "no filename provided", "status": http.StatusInternalServerError})
return
}
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
if !ok {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError, "error": "unable to connect to DB"})
return
}
db.Where(ZoneRequest{
Zone: &Zone{
RawFileName: filename,
},
User: claims["username"].(string),
}).First(&result)
if result == (internal.ZoneRequest{}) {
APIError(w, r, genericResponseFields{"message": "internal server error", "status": http.StatusInternalServerError})
return
}
zoneFile := newNDSRequest(result.RawFileName, claims["username"].(string))
if err := zoneFile.Save(); err != nil {
APIError(w, r, genericResponseFields{"message": "Unable to save zonefile", "status": http.StatusInternalServerError, "error": err.Error()})
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusCreated)
resp := internal.Response{
Message: "Successfully saved zonefile",
}
render.JSON(w, r, resp)
}
func newNDSRequest(filename string, user string) ndr {
func newDNSRequest(filename string, user string) ndr {
dat, err := os.ReadFile(fmt.Sprintf("/tmp/tmpfile-%s-%s", filename, user))
if err != nil {
return &ZoneRequest{}
@ -253,7 +128,7 @@ func newNDSRequest(filename string, user string) ndr {
}
// Parse will be used to parse zonefiles.
func (zone *ZoneRequest) Parse() error {
func (zone *ZoneRequest) parse() error {
zp := dns.NewZoneParser(strings.NewReader(zone.Body), "", "")
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
@ -267,7 +142,7 @@ func (zone *ZoneRequest) Parse() error {
return nil
}
func (zone *ZoneRequest) Save() error {
func (zone *ZoneRequest) save() error {
c, err := internal.ReadConfig()
if err != nil {
logHandler(genericResponseFields{"error": err.Error(), "message": "no config file defined"})