Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Update web server related stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
agile6v committed Feb 28, 2019
1 parent 3b1b097 commit d290a2e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
38 changes: 29 additions & 9 deletions pkg/server/web/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package api
import (
"net/http"
"github.com/agile6v/squeeze/pkg/util"
"github.com/agile6v/squeeze/pkg/server/web/controllers"
)

type AppAPI struct {
type AppAPI struct {}


}

func (api *AppAPI) Init() {
http.HandleFunc("/", api.Index)
Expand All @@ -31,32 +32,51 @@ func (api *AppAPI) Init() {
http.HandleFunc("/api/list", api.list)
http.HandleFunc("/api/start", api.start)
http.HandleFunc("/api/stop", api.stop)
http.HandleFunc("/api/callback", api.callback)
}

func (api *AppAPI) Index(w http.ResponseWriter, request *http.Request) {
func (api *AppAPI) Index(w http.ResponseWriter, r *http.Request) {
util.RespondWithJSON(w, http.StatusOK, "")
}

func (api *AppAPI) create(writer http.ResponseWriter, request *http.Request) {
func (api *AppAPI) create(w http.ResponseWriter, r *http.Request) {
// Read body
task := &controllers.CreateTask{}
err := util.ReadBody(r, task)
if err != nil {
util.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}

err = task.Handle()
if err != nil {
util.RespondWithError(w, http.StatusInternalServerError, err.Error())
return
}

util.RespondWithError(w, http.StatusOK, "")
}

func (api *AppAPI) delete(w http.ResponseWriter, r *http.Request) {

}

func (api *AppAPI) delete(writer http.ResponseWriter, request *http.Request) {
func (api *AppAPI) search(w http.ResponseWriter, r *http.Request) {

}

func (api *AppAPI) search(writer http.ResponseWriter, request *http.Request) {
func (api *AppAPI) list(w http.ResponseWriter, r *http.Request) {

}

func (api *AppAPI) list(writer http.ResponseWriter, request *http.Request) {
func (api *AppAPI) start(w http.ResponseWriter, r *http.Request) {

}

func (api *AppAPI) start(writer http.ResponseWriter, request *http.Request) {
func (api *AppAPI) stop(w http.ResponseWriter, r *http.Request) {

}

func (api *AppAPI) stop(writer http.ResponseWriter, request *http.Request) {
func (api *AppAPI) callback(w http.ResponseWriter, r *http.Request) {

}
50 changes: 49 additions & 1 deletion pkg/server/web/controllers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,54 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package api
package controllers

import (
"strings"
"encoding/json"
"github.com/agile6v/squeeze/pkg/pb"
"github.com/agile6v/squeeze/pkg/proto/builder"
"github.com/agile6v/squeeze/pkg/config"
)

type CreateTask struct {
Protocol string
Data json.RawMessage
}

func (createTask *CreateTask) Handle() error {
var err error

args := config.ProtoConfigArgs{}
args.Callback = ""
args.HttpAddr = ""

protocol := pb.Protocol(pb.Protocol_value[strings.ToUpper(createTask.Protocol)])
builder := builder.NewBuilder(protocol)

// Unmarshal
if protocol == pb.Protocol_HTTP {
err = json.Unmarshal(createTask.Data, &args.HttpOpts)
} else if protocol == pb.Protocol_WEBSOCKET {
err = json.Unmarshal(createTask.Data, &args.WsOpts)
} else {

}

if err != nil {
return err
}

_, err = builder.CreateTask(&args)
if err != nil {
return err
}

return nil
}






18 changes: 18 additions & 0 deletions pkg/util/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"
"net/http"
"regexp"
"encoding/json"
"github.com/agile6v/squeeze/pkg/version"
)

Expand Down Expand Up @@ -103,3 +104,20 @@ func CloneRequest(r *http.Request, body []byte) *http.Request {
}
return r2
}

func ReadBody(r *http.Request, obj interface{}) error {
// Read body
b, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return err
}

// Unmarshal
err = json.Unmarshal(b, obj)
if err != nil {
return err
}

return nil
}

0 comments on commit d290a2e

Please sign in to comment.