Skip to content

Commit

Permalink
Merge pull request #14 from seungkyua/20230327_appserve_http
Browse files Browse the repository at this point in the history
Add AppServe
  • Loading branch information
ktkfree authored Apr 6, 2023
2 parents de85611 + 5848745 commit 4ebe4f2
Show file tree
Hide file tree
Showing 9 changed files with 499 additions and 608 deletions.
5 changes: 5 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go 1.18

use (
.
)
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
7 changes: 5 additions & 2 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gorm.io/gorm/logger"

"github.com/openinfradev/tks-api/internal/repository"
"github.com/openinfradev/tks-api/pkg/domain"
)

var gormDB *gorm.DB
Expand Down Expand Up @@ -90,10 +91,12 @@ func migrateSchema(db *gorm.DB) error {
if err := db.AutoMigrate(&repository.Application{}); err != nil {
return err
}
if err := db.AutoMigrate(&repository.AppServeApp{}); err != nil {

// AppServe
if err := db.AutoMigrate(&domain.AppServeApp{}); err != nil {
return err
}
if err := db.AutoMigrate(&repository.AppServeAppTask{}); err != nil {
if err := db.AutoMigrate(&domain.AppServeAppTask{}); err != nil {
return err
}
return nil
Expand Down
244 changes: 115 additions & 129 deletions internal/delivery/http/app-serve-app.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package http

import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/gorilla/mux"

"github.com/openinfradev/tks-api/internal/usecase"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/httpErrors"
Expand All @@ -24,165 +24,145 @@ func NewAppServeAppHandler(h usecase.IAppServeAppUsecase) *AppServeAppHandler {
}
}

// GetAppServeApps godoc
// CreateAppServeApp godoc
// @Tags AppServeApps
// @Summary Get appServeApp list
// @Description Get appServeApp list by giving params
// @Summary Install appServeApp
// @Description Install appServeApp
// @Accept json
// @Produce json
// @Param projectId query string false "project_id"
// @Param showAll query string false "show_all"
// @Success 200 {object} []domain.AppServeApp
// @Router /app-serve-apps [get]
// @Param object body domain.CreateAppServeAppRequest true "create appserve request"
// @Success 200 {object} string
// @Router /app-serve-apps [post]
// @Security JWT
func (h *AppServeAppHandler) GetAppServeApps(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()

cont_id := urlParams.Get("contract_id")
if cont_id == "" {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid contract_id")))
func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Request) {
appReq := domain.CreateAppServeAppRequest{}
err := UnmarshalRequestInput(r, &appReq)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

show_all_str := urlParams.Get("show_all")
if show_all_str == "" {
show_all_str = "false"
}

show_all, err := strconv.ParseBool(show_all_str)
if err != nil {
log.Error("Failed to convert show_all params. Err: ", err)
ErrorJSON(w, err)
var app domain.AppServeApp
if err = domain.Map(appReq, &app); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

appServeApps, err := h.usecase.Fetch(cont_id, show_all)
if err != nil {
log.Error("Failed to get Failed to get app-serve-apps ", err)
ErrorJSON(w, err)
now := time.Now()
app.EndpointUrl = "N/A"
app.PreviewEndpointUrl = "N/A"
app.Status = "PREPARING"
app.CreatedAt = now

var task domain.AppServeAppTask
if err = domain.Map(appReq, &task); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

var out struct {
AppServeApps []*domain.AppServeApp `json:"appServeApps"`
}
out.AppServeApps = appServeApps
task.Status = "PREPARING"
task.Output = ""
task.CreatedAt = now

ResponseJSON(w, http.StatusOK, out)
app.AppServeAppTasks = append(app.AppServeAppTasks, task)

}
// Validate port param for springboot app
if app.AppType == "springboot" {
if app.AppServeAppTasks[0].Port == "" {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("error: 'port' param is mandatory")))
return
}
}

// GetAppServeApp godoc
// @Tags AppServeApps
// @Summary Get appServeApp
// @Description Get appServeApp by giving params
// @Accept json
// @Produce json
// @Success 200 {object} domain.AppServeApp
// @Router /app-serve-apps/{appServeAppId} [get]
// @Security JWT
func (h *AppServeAppHandler) GetAppServeApp(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appServeAppId, ok := vars["appServeAppId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appServeAppId")))
// Validate 'strategy' param
if app.AppServeAppTasks[0].Strategy != "rolling-update" {
ErrorJSON(w, httpErrors.NewBadRequestError(
fmt.Errorf("error: 'strategy' should be 'rolling-update' on first deployment")))
return
}

res, err := h.usecase.Get(appServeAppId)
_, _, err = h.usecase.CreateAppServeApp(&app)
if err != nil {
log.Error("Failed to get Failed to get app-serve-app ", err)
ErrorJSON(w, err)
return
}

var out struct {
AppServeAppCombined domain.AppServeAppCombined `json:"appServeApp"`
var out domain.CreateAppServeAppResponse
if err = domain.Map(app, &out); err != nil {
ErrorJSON(w, err)
return
}
out.AppServeAppCombined = *res

ResponseJSON(w, http.StatusOK, out)

}

// CreateAppServeApp godoc
// GetAppServeApps godoc
// @Tags AppServeApps
// @Summary Install appServeApp
// @Description Install appServeApp
// @Summary Get appServeApp list
// @Description Get appServeApp list by giving params
// @Accept json
// @Produce json
// @Param object body string true "body"
// @Success 200 {object} string
// @Router /app-serve-apps [post]
// @Param organization_Id query string false "organization_Id"
// @Param showAll query string false "show_all"
// @Success 200 {object} []domain.AppServeApp
// @Router /app-serve-apps [get]
// @Security JWT
func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Request) {
var appObj = domain.CreateAppServeAppRequest{}
body, err := io.ReadAll(r.Body)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}
err = json.Unmarshal(body, &appObj)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}
func (h *AppServeAppHandler) GetAppServeApps(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()

log.Debug(fmt.Sprintf("*****\nIn handlers, appObj:\n%+v\n*****\n", appObj))

// Validate common params
if appObj.Name == "" || appObj.Type == "" || appObj.Version == "" ||
appObj.AppType == "" || appObj.ContractId == "" {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf(`Error: The following params are always mandatory.
- name
- type
- app_type
- contract_id
- version`)))
organizationId := urlParams.Get("organizationId")
if organizationId == "" {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("invalid organizationId")))
return
}

// Validate port param for springboot app
if appObj.AppType == "springboot" {
if appObj.Port == "" {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf(`Error: 'port' param is mandatory.`)))
return
}
showAllParam := urlParams.Get("showAll")
if showAllParam == "" {
showAllParam = "false"
}

// Validate 'type' param
if !(appObj.Type == "build" || appObj.Type == "deploy" || appObj.Type == "all") {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf(`Error: 'type' should be one of these values.
- build
- deploy
- all`)))
showAll, err := strconv.ParseBool(showAllParam)
if err != nil {
log.Error("Failed to convert showAll params. Err: ", err)
ErrorJSON(w, err)
return
}

// Validate 'strategy' param
if appObj.Strategy != "rolling-update" {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf(`Error: 'strategy' should be 'rolling-update' on first deployment.`)))
apps, err := h.usecase.GetAppServeApps(organizationId, showAll)
if err != nil {
log.Error("Failed to get Failed to get app-serve-apps ", err)
ErrorJSON(w, err)
return
}

// Validate 'app_type' param
if !(appObj.AppType == "spring" || appObj.AppType == "springboot") {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf(`Error: 'type' should be one of these values.
- string
- stringboot`)))
return
}
var out domain.GetAppServeAppsResponse
out.AppServeApps = apps

appServeAppId, err := h.usecase.Create(&appObj)
if err != nil {
ErrorJSON(w, err)
ResponseJSON(w, http.StatusOK, out)

}

// GetAppServeApp godoc
// @Tags AppServeApps
// @Summary Get appServeApp
// @Description Get appServeApp by giving params
// @Accept json
// @Produce json
// @Success 200 {object} domain.AppServeApp
// @Router /app-serve-apps/{appServeAppId} [get]
// @Security JWT
func (h *AppServeAppHandler) GetAppServeApp(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appId, ok := vars["appId"]
fmt.Printf("appId = [%s]", appId)
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("invalid appId")))
return
}
app, _ := h.usecase.GetAppServeAppById(appId)

var out struct {
AppServeAppId string `json:"appServeAppId"`
}
out.AppServeAppId = appServeAppId
var out domain.GetAppServeAppResponse
out.AppServeApp = *app

ResponseJSON(w, http.StatusOK, out)
}
Expand All @@ -193,37 +173,43 @@ func (h *AppServeAppHandler) CreateAppServeApp(w http.ResponseWriter, r *http.Re
// @Description Update appServeApp
// @Accept json
// @Produce json
// @Param object body string true "body"
// @Param object body domain.UpdateAppServeAppRequest true "update appserve request"
// @Success 200 {object} object
// @Router /app-serve-apps [put]
// @Security JWT
func (h *AppServeAppHandler) UpdateAppServeApp(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appServeAppId, ok := vars["appServeAppId"]
appId, ok := vars["appId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("invalid appServeAppId")))
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("invalid appId")))
return
}

var app = domain.UpdateAppServeAppRequest{}
body, err := io.ReadAll(r.Body)
appReq := domain.UpdateAppServeAppRequest{}
err := UnmarshalRequestInput(r, &appReq)
if err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}
err = json.Unmarshal(body, &app)
if err != nil {

var task domain.AppServeAppTask
if err = domain.Map(appReq, &task); err != nil {
ErrorJSON(w, httpErrors.NewBadRequestError(err))
return
}

res := ""
if app.Promote {
res, err = h.usecase.Promote(appServeAppId, &app)
} else if app.Abort {
res, err = h.usecase.Abort(appServeAppId, &app)
task.AppServeAppId = appId
task.Status = "PREPARING"
task.Output = ""
task.CreatedAt = time.Now()

var res string
if appReq.Promote {
res, err = h.usecase.PromoteAppServeApp(appId)
} else if appReq.Abort {
res, err = h.usecase.AbortAppServeApp(appId)
} else {
res, err = h.usecase.Update(appServeAppId, &app)
res, err = h.usecase.UpdateAppServeApp(&task)
}

if err != nil {
Expand All @@ -246,15 +232,15 @@ func (h *AppServeAppHandler) UpdateAppServeApp(w http.ResponseWriter, r *http.Re
// @Security JWT
func (h *AppServeAppHandler) DeleteAppServeApp(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
appServeAppId, ok := vars["appServeAppId"]
appId, ok := vars["appId"]
if !ok {
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("Invalid appServeAppId")))
ErrorJSON(w, httpErrors.NewBadRequestError(fmt.Errorf("invalid appId")))
return
}

res, err := h.usecase.Delete(appServeAppId)
res, err := h.usecase.DeleteAppServeApp(appId)
if err != nil {
log.Error("Failed to delete appServeAppId err : ", err)
log.Error("Failed to delete appId err : ", err)
ErrorJSON(w, err)
return
}
Expand Down
Loading

0 comments on commit 4ebe4f2

Please sign in to comment.