Skip to content

Commit

Permalink
Merge branch 'NexClipper:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
yoosaemsol authored Jul 14, 2021
2 parents 074824f + 51e5106 commit 6e8cf39
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 32 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/go-sql-driver/mysql v1.5.0
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/gorilla/context v1.1.1
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/kelseyhightower/envconfig v1.4.0
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fanliao/go-promise v0.0.0-20141029170127-1890db352a72 h1:0eU/faU2oDIB2BkQVM02hgRLJjGzzUuRf19HUhp0394=
github.com/fanliao/go-promise v0.0.0-20141029170127-1890db352a72/go.mod h1:PjfxuH4FZdUyfMdtBio2lsRr1AKEaVPwelzuHuh8Lqc=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -117,6 +120,8 @@ github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVf
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
Expand Down
23 changes: 0 additions & 23 deletions pkg/manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,6 @@ type API struct {
BlockKeyMap concurrent.ConcurrentMap
}

// CORS Middleware
func CORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// Set headers
w.Header().Set("Access-Control-Allow-Headers:", "*")
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")

if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}

// Next
next.ServeHTTP(w, r)
return
})
}

// Init initialize API router
// @title Klevr-Manager API
// @version 1.0
Expand All @@ -96,8 +75,6 @@ func Init(ctx *common.Context) *API {
// TODO: ContextLogger interface 구현하여 logger override
// api.DB.SetLogger(log.NewSimpleLogger(f))

api.Manager.RootRouter.Use(CORS)

api.BaseRoutes.Root = api.Manager.RootRouter

// swagger 설정
Expand Down
44 changes: 36 additions & 8 deletions pkg/manager/api_install.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package manager

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/Klevry/klevr/pkg/common"
"github.com/NexClipper/logger"
"github.com/gorilla/mux"
)

// InitInstall initialize install API
// InitInstall initialize install API

func (api *API) InitInstall(install *mux.Router) {
logger.Debug("API InitInstall - init URI")

Expand All @@ -19,19 +22,44 @@ func (api *API) InitInstall(install *mux.Router) {
registURI(install, GET, "/agents/download", api.downloadAgent)
}

// agent setup script 생성
type BootstrapCommand struct {
APIKey string `json:"apiKey"`
Platform string `json:"platform"`
ManagerURL string `json:"managerUrl"`
ZoneID uint64 `json:"zoneId"`
}

//
// generateBootstrapCommand godoc
// @Summary agent setup script 생성
// @Description 에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다
// @Tags Install
// @Accept json
// @Produce plain
// @Router /install/agents/bootstrap [post]
// @Param b body manager.BootstrapCommand true "Bootstrap Info"
// @Success 200 {string} string
func (api *API) generateBootstrapCommand(w http.ResponseWriter, r *http.Request) {
var cmd = "curl -sL gg.gg/klevr |bash && ./klevr -apiKey=\"{apiKey}\" -platform={platform} -manager=\"{managerUrl}\" -zoneId={zoneId}"

var cr = &common.Request{Request: r}
var bc BootstrapCommand
err := json.NewDecoder(r.Body).Decode(&bc)
if err != nil {
common.WriteHTTPError(500, w, err, "JSON parsing error")
return
}

zone := strconv.FormatUint(bc.ZoneID, 10)

cmd = strings.Replace(cmd, "{apiKey}", cr.Param("apiKey"), 1)
cmd = strings.Replace(cmd, "{platform}", cr.Param("platform"), 1)
cmd = strings.Replace(cmd, "{managerUrl}", cr.Param("managerUrl"), 1)
cmd = strings.Replace(cmd, "{zoneId}", cr.Param("zoneId"), 1)
cmd = strings.Replace(cmd, "{apiKey}", bc.APIKey, 1)
cmd = strings.Replace(cmd, "{platform}", bc.Platform, 1)
cmd = strings.Replace(cmd, "{managerUrl}", bc.ManagerURL, 1)
cmd = strings.Replace(cmd, "{zoneId}", zone, 1)

w.WriteHeader(http.StatusUnauthorized)
//w.WriteHeader(http.StatusUnauthorized)
// w.Write([]byte(fmt.Sprintf("%s", cmd)))
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", cmd)
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/manager/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,40 @@ var doc = `{
}
}
}
},
"/install/agents/bootstrap": {
"post": {
"description": "에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다",
"consumes": [
"application/json"
],
"produces": [
"text/plain"
],
"tags": [
"Install"
],
"summary": "agent setup script 생성",
"parameters": [
{
"description": "Bootstrap Info",
"name": "b",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/manager.BootstrapCommand"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -1549,6 +1583,23 @@ var doc = `{
}
}
},
"manager.BootstrapCommand": {
"type": "object",
"properties": {
"apiKey": {
"type": "string"
},
"managerUrl": {
"type": "string"
},
"platform": {
"type": "string"
},
"zoneId": {
"type": "integer"
}
}
},
"manager.KlevrVariable": {
"type": "object",
"properties": {
Expand Down
51 changes: 51 additions & 0 deletions pkg/manager/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,40 @@
}
}
}
},
"/install/agents/bootstrap": {
"post": {
"description": "에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다",
"consumes": [
"application/json"
],
"produces": [
"text/plain"
],
"tags": [
"Install"
],
"summary": "agent setup script 생성",
"parameters": [
{
"description": "Bootstrap Info",
"name": "b",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/manager.BootstrapCommand"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
},
"definitions": {
Expand Down Expand Up @@ -1530,6 +1564,23 @@
}
}
},
"manager.BootstrapCommand": {
"type": "object",
"properties": {
"apiKey": {
"type": "string"
},
"managerUrl": {
"type": "string"
},
"platform": {
"type": "string"
},
"zoneId": {
"type": "integer"
}
}
},
"manager.KlevrVariable": {
"type": "object",
"properties": {
Expand Down
33 changes: 33 additions & 0 deletions pkg/manager/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ definitions:
userId:
type: integer
type: object
manager.BootstrapCommand:
properties:
apiKey:
type: string
managerUrl:
type: string
platform:
type: string
zoneId:
type: integer
type: object
manager.KlevrVariable:
properties:
description:
Expand Down Expand Up @@ -1052,4 +1063,26 @@ paths:
summary: Klevr에서 제공하는 시스템 변수 목록을 조회한다.
tags:
- servers
/install/agents/bootstrap:
post:
consumes:
- application/json
description: 에이전트가 설치되기 위한 최소 정보를 이용해 설치를 할 수 있는 스크립트를 생성한다
parameters:
- description: Bootstrap Info
in: body
name: b
required: true
schema:
$ref: '#/definitions/manager.BootstrapCommand'
produces:
- text/plain
responses:
"200":
description: OK
schema:
type: string
summary: agent setup script 생성
tags:
- Install
swagger: "2.0"
10 changes: 9 additions & 1 deletion pkg/manager/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/Klevry/klevr/pkg/common"
"github.com/Klevry/klevr/pkg/rabbitmq"
"github.com/NexClipper/logger"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
concurrent "github.com/orcaman/concurrent-map"
"github.com/pkg/errors"
Expand Down Expand Up @@ -174,9 +175,16 @@ func (manager *KlevrManager) Run() error {
}
}

headerOk := handlers.AllowedHeaders([]string{"*"})
//originOk := handlers.AllowedOrigins([]string{"http://localhost:3000"})
originOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "OPTIONS", "DELETE"})
credentialsOk := handlers.AllowCredentials()
corsHandler := handlers.CORS(headerOk, originOk, methodsOk, credentialsOk)(manager.RootRouter)

s := &http.Server{
Addr: fmt.Sprintf(":%d", serverConfig.Port),
Handler: manager.RootRouter,
Handler: corsHandler,
ReadTimeout: time.Duration(serverConfig.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(serverConfig.WriteTimeout) * time.Second,
}
Expand Down

0 comments on commit 6e8cf39

Please sign in to comment.