Creating http handler

I have main.go and I want changing to better structure(similarly main2.go ) due to if the proyect would grow up, would not be easy interpret for new developers the code.

My idea is create a folder called handle and put in it the file handle.go with all the handles methods, the trouble that I find is that I do not know how to set up http.HandleFunc("/R1", HandlerOne) and http.HandleFunc("/R2", HandlerTwo) in handler.go and call it from main2.go.

main.go

package main

import (
    "fmt"
    "net/http"

)

func HandlerOne(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message one")
}

func HandlerTwo(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message two")
}

func main() {

    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)

    err := http.ListenAndServe(":9998", nil)

    if err != nil {
        fmt.Printf("Server failed: ", err.Error())
    }
}

main2.go

package main

import (
    "fmt"
    "net/http"

)


func main() {

   // Call handle.go

    err := http.ListenAndServe(":9998", nil)

    if err != nil {
        fmt.Printf("Server failed: ", err.Error())
    }
}

handle.go

package handle

import (
    "fmt"
    "net/http"
)

func HandlerOne(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message one")
 }

func HandlerTwo(w http.ResponseWriter, req *http.Request) {
    fmt.Println("message two")
}

Note: Thanks @kluyg for your answer. I going to add that i want that seems I do not explain very well:

That I want is create a function in
handle.go where can I can put all the mapping of the handles. i.e some like that

func SetUpMapping(...){
    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)

   .....  //Others mapping
    http.HandleFunc("/RN", HandlerN)

}

where ... should be one refence to http, but this is one package, and in the main something like code 3

code 3

main2.go

package main

import (
        "fmt"
        "net/http"
        "handlers/handle"

)


func main() {

        // Call handle.go
        handle.SetUpMapping(...) // i dont know what parameter put here
        err := http.ListenAndServe(":9998", nil)

        if err != nil {
                    fmt.Printf("Server failed: ", err.Error())
        }
}

Upvotes: 1

Views: 537

Answers (1)

Kluyg
Kluyg

Reputation: 5347

I took your code and put main2.go into $GOPATH/handlers folder and handle.go in $GOPATH/handlers/handle folder. Then I changed main2.go to be

package main

import (
    "fmt"
    "net/http"

    "handlers/handle"
)

func main() {

    // Call handle.go

    http.HandleFunc("/R1", handle.HandlerOne)
    http.HandleFunc("/R2", handle.HandlerTwo)

    err := http.ListenAndServe(":9998", nil)

    if err != nil {
        fmt.Printf("Server failed: ", err.Error())
    }
}

It compiles and works fine. I believe this is what you asked for.

UPDATE

OK, you can add this to your handle.go

func SetUpMapping() {
    http.HandleFunc("/R1", HandlerOne)
    http.HandleFunc("/R2", HandlerTwo)

    .....  //Others mapping
    http.HandleFunc("/RN", HandlerN)

}

then in main2.go

package main

import (
        "fmt"
        "net/http"
        "handlers/handle"
)


func main() {

        // Call handle.go
        handle.SetUpMapping() // you don't need any parameters here
        err := http.ListenAndServe(":9998", nil)

        if err != nil {
                    fmt.Printf("Server failed: ", err.Error())
        }
}

Upvotes: 1

Related Questions