Re: [go-nuts] How to use .toml file to parse IP address and port to http server
Hi, I have solve the problem and it works now. Thanks. On Monday, September 30, 2019 at 11:06:20 AM UTC+3, Afriyie Abraham Kwabena wrote: > > Hi, > > I have applied similar scenario to the function below but am not getting > the Ip address and the port number. Below is the function > > var addr string > > func init(){ > > var conf Config > if _, err := toml.Decode("config.toml", ); err != nil { > // handle error > } > addr = net.JoinHostPort(conf.Addr, conf.Port) > fmt.Println(addr) // This printing empty > > } > > > func main() { > > server := NewServer() > > go func() { > > if err := > server.ListenAndServeTLS("/home/cumucore/diam/server.crt", > "/home/cumucore/diam/server.key"); err != nil && err != > http.ErrServerClosed { > logger.Fatalf("Could not listen on %s: %v\n", addr, err) > > if err := server.ListenAndServe(); err != nil { > logger.Fatalf("Could not listen on %s: %v\n", addr, err) > } > > } > }() > } > > > // Create a new server > func NewServer() *http.Server { > > return { > Addr: addr, > Handler: diam.NewRouter(), > ErrorLog: logger, > ReadTimeout: 5 * time.Second, > WriteTimeout: 10 * time.Second, > IdleTimeout: 15 * time.Second, > } > } > > Ther config.toml file contain the key values as > Addr = "192.168.9.186" > Port = "8000" > > What might be the problem in this case > > > On Sunday, September 29, 2019 at 5:18:55 PM UTC+3, Andrew Pillar wrote: >> >> Use net.JoinHostPort to concatenate the values you have in the struct >> and pass the to http.Server struct. >> >> if _, err := toml.Decode("config.toml", ); err != nil { >> // handle error >> } >> >> addr, err := net.JoinHostPort(conf.Address, conf.PORT) >> >> if err != nil { >> // handle error >> } >> >> src := { >> Addr: addr, >> } >> >> Be sure to set explicit struct tags on your destination struct that >> will be used for unmarshalling the toml. This way the decoder will know >> which struct fields to populate. >> >> type Config struct { >> PORTstring `toml:"port"` >> Address string `toml:"address"` >> } >> >> This will only be necessary though if you want the fields to map >> differently depending on their name. >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/28360a86-da47-4226-90f2-59515a317210%40googlegroups.com.
Re: [go-nuts] How to use .toml file to parse IP address and port to http server
Hi, I have applied similar scenario to the function below but am not getting the Ip address and the port number. Below is the function var addr string func init(){ var conf Config if _, err := toml.Decode("config.toml", ); err != nil { // handle error } addr = net.JoinHostPort(conf.Addr, conf.Port) fmt.Println(addr) // This printing empty } func main() { server := NewServer() go func() { if err := server.ListenAndServeTLS("/home/cumucore/diam/server.crt", "/home/cumucore/diam/server.key"); err != nil && err != http.ErrServerClosed { logger.Fatalf("Could not listen on %s: %v\n", addr, err) if err := server.ListenAndServe(); err != nil { logger.Fatalf("Could not listen on %s: %v\n", addr, err) } } }() } // Create a new server func NewServer() *http.Server { return { Addr: addr, Handler: diam.NewRouter(), ErrorLog: logger, ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 15 * time.Second, } } Ther config.toml file contain the key values as Addr = "192.168.9.186" Port = "8000" What might be the problem in this case On Sunday, September 29, 2019 at 5:18:55 PM UTC+3, Andrew Pillar wrote: > > Use net.JoinHostPort to concatenate the values you have in the struct > and pass the to http.Server struct. > > if _, err := toml.Decode("config.toml", ); err != nil { > // handle error > } > > addr, err := net.JoinHostPort(conf.Address, conf.PORT) > > if err != nil { > // handle error > } > > src := { > Addr: addr, > } > > Be sure to set explicit struct tags on your destination struct that > will be used for unmarshalling the toml. This way the decoder will know > which struct fields to populate. > > type Config struct { > PORTstring `toml:"port"` > Address string `toml:"address"` > } > > This will only be necessary though if you want the fields to map > differently depending on their name. > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9756a565-79c7-4eda-96f3-172e3ec6a908%40googlegroups.com.
Re: [go-nuts] How to use .toml file to parse IP address and port to http server
Use net.JoinHostPort to concatenate the values you have in the struct and pass the to http.Server struct. if _, err := toml.Decode("config.toml", ); err != nil { // handle error } addr, err := net.JoinHostPort(conf.Address, conf.PORT) if err != nil { // handle error } src := { Addr: addr, } Be sure to set explicit struct tags on your destination struct that will be used for unmarshalling the toml. This way the decoder will know which struct fields to populate. type Config struct { PORTstring `toml:"port"` Address string `toml:"address"` } This will only be necessary though if you want the fields to map differently depending on their name. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/594dc8061305a9d24a85bba3c403524eb4c9c637.camel%40andrewpillar.com.
[go-nuts] How to use .toml file to parse IP address and port to http server
Hi, How I can use the .toml file to parse IP address and port to simple http server instead of using flags I have a config.toml file which contains these parameters in PORT = "8000" Address = "localhost" //Parameters type struct as type Config struct { PORTstring Address string } I can load file in main function as var conf Config if _, err := toml.Decode("config.toml", ); err != nil { // handle error } //simple http server main function func main() { var dir string flag.StringVar(, "dir", ".", "the directory to serve files from. Defaults to the current dir") flag.Parse() r := mux.NewRouter() // This will serve files under http://localhost:8000/static/ r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir srv := { Handler: r, Addr: "127.0.0.1:8000", WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } log.Fatal(srv.ListenAndServe()) } Any help? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f095834a-5bde-4ae2-93dd-5f2f39212aa2%40googlegroups.com.