-3

i have this code of simple web server but i dont understand this code :

Handler:app.routes(),

const webPort = "80"

type Config struct {}

func main() {

    app := Config{}
    log.Printf("Starting broker service on port %s\n",webPort)
    srv := &http.Server{
        Addr: fmt.Sprintf(":%s",webPort),
        Handler:app.routes(),
    }

    err := srv.ListenAndServe()
    if(err != nil) {
        log.Panic(err)
    }
}

and in routes file :

func (app *Config) routes() http.Handler {
    mux := chi.NewRouter()
    mux.Use(cors.Handler(cors.Options{
        AllowedOrigins: []string{"http://*","https://*"},
        AllowedMethods: []string{"GET", "POST", "DELETE","PUT","OPTIONS"},
        AllowedHeaders: []string{"Accept","Authorization","Content-Type","X-CSRF-Token"},
        ExposedHeaders: []string{"Link"},
        AllowCredentials:true,
        MaxAge:300,
    }))
    
    mux.Use(middleware.Heartbeat("/ping"))
    mux.Post("/",app.Broker)

    return mux
}

this is wroking and the routes() function called when request recived but how does this routes() knows to be triggered when it attached to empty struct

app := Config{}

from where does app knows about routes() ?

what is the : func (app *Config) in the function ?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • routes is a method defined on the Config struct, as shown in your second code snippet. This method returns an http.Handler, hence you can use it as struct field in the server struct. – The Fool Mar 17 '23 at 09:36
  • i dont see it defined on the config struct .. is it in the definition of the func or somthing ? – user63898 Mar 17 '23 at 09:57
  • 1
    If you look at the first line in your second code snippet you see `func (app *Config) routes() http.Handle`. This is the syntax to define a method on a type, in this case on the Config type. – The Fool Mar 17 '23 at 10:13

1 Answers1

3

Routes have been attached to the HTTP server as shown below.

srv := &http.Server{
   Addr: ":8081", // port
   Handler: app.routes() // a handler to invoke
 }

routes is a method of Config struct. We can still call the routes methods as in your code even when Config is empty.

 cfg := Config{}
 r := cfg.routes()

Config struct is acting as a method receiver here.

  • Tnx, few things i dont understand , what do you mean method receiver ? for me ( coming from c++ and java ) its defining empty struct . from where did the Config knows about the routes() method ? – user63898 Mar 17 '23 at 09:55
  • 1
    @user63898 `func (app *Config) routes() http.Handler {/*...*/}` is a method declaration. It "attaches" the function in question to the `*Config` type. In the context of this method, `app` is known as the _method receiver_; you'd call it `this` in traditional OO languages. – jub0bs Mar 17 '23 at 10:02
  • 1
    Please refer this https://go.dev/play/p/Xp29ddR7Eag. In this example, `(a Animal)` is called as the **receiver** and `Animal` is called as the **receiver type**. – Chamith Udayanga Mar 17 '23 at 10:16