Skip to content

Commit

Permalink
Adds: Custome end points structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Manish Atri committed Jun 3, 2019
1 parent 3c8df64 commit 93f49e3
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
Binary file added __debug_bin
Binary file not shown.
17 changes: 16 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/spf13/pflag"

"github.com/elastic/apm-server/beater"
"github.com/elastic/apm-server/custom_server"
"github.com/elastic/apm-server/idxmgmt"
_ "github.com/elastic/apm-server/include"
"github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/libbeat/common"
Expand All @@ -44,6 +44,21 @@ const IdxPattern = "apm"
var RootCmd *cmd.BeatsRootCmd

func init() {

// r := mux.NewRouter()

// r.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
// // vars := mux.Vars(r)
// // title := vars["title"]
// // page := vars["page"]

// fmt.Fprintf(w, "You've requested the book: on page \n")
// }).Methods("GET")

// go http.ListenAndServe(":9000", r)

s := custom_server.NewServer()
s.Start()
overrides := common.MustNewConfigFrom(map[string]interface{}{
"logging": map[string]interface{}{
"metrics": map[string]interface{}{
Expand Down
57 changes: 57 additions & 0 deletions custom_server/custom_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package custom_server

import (
"net/http"

"github.com/gorilla/mux"
)

type CustomRouter struct {
}

func NewUserRouter(router *mux.Router) *mux.Router {
customRouter := CustomRouter{}

router.HandleFunc("/", customRouter.createUserHandler).Methods("GET")
// router.HandleFunc("/{username}", userRouter.getUserHandler).Methods("GET")
return router
}

func (ur *CustomRouter) createUserHandler(w http.ResponseWriter, r *http.Request) {
// user, err := decodeUser(r)
// if err != nil {
// Error(w, http.StatusBadRequest, "Invalid request payload")
// return
// }

// err = ur.userService.Create(&user)
// if err != nil {
// Error(w, http.StatusInternalServerError, err.Error())
// return
// }
Json(w, http.StatusOK, "")
}

// func (ur *userRouter) getUserHandler(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
// log.Println(vars)
// username := vars["username"]

// user, err := ur.userService.GetByUsername(username)
// if err != nil {
// Error(w, http.StatusNotFound, err.Error())
// return
// }

// Json(w, http.StatusOK, user)
// }

// func decodeUser(ur *http.Request) (root.User, error) {
// var u root.User
// if r.Body == nil {
// return u, errors.New("no request body")
// }
// decoder := json.NewDecoder(r.Body)
// err := decoder.Decode(&u)
// return u, err
// }
31 changes: 31 additions & 0 deletions custom_server/custom_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package custom_server

import (
"log"
"net/http"
"os"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)

type CustomServer struct {
router *mux.Router
}

func NewServer() *CustomServer {
s := CustomServer{router: mux.NewRouter()}
NewUserRouter(s.newSubrouter("/custom_end_point"))
return &s
}

func (s *CustomServer) Start() {
log.Println("Listening on port 8080")
if err := http.ListenAndServe(":8080", handlers.LoggingHandler(os.Stdout, s.router)); err != nil {
log.Fatal("http.ListenAndServe: ", err)
}
}

func (s *CustomServer) newSubrouter(path string) *mux.Router {
return s.router.PathPrefix(path).Subrouter()
}
18 changes: 18 additions & 0 deletions custom_server/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package custom_server

import (
"encoding/json"
"net/http"
)

func Error(w http.ResponseWriter, code int, message string) {
Json(w, code, map[string]string{"error": message})
}

func Json(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"

"github.com/elastic/apm-server/cmd"

)

func main() {
Expand Down

0 comments on commit 93f49e3

Please sign in to comment.