2021-09-11 23:32:07 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-09-12 17:27:49 +01:00
|
|
|
type WebServer struct {
|
|
|
|
*http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebServer(port int) (*WebServer, error) {
|
2021-09-11 23:32:07 +01:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// ToDo: configure routes
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "Hello rmsn!!")
|
|
|
|
})
|
|
|
|
|
2021-09-12 17:27:49 +01:00
|
|
|
s := &WebServer{
|
|
|
|
&http.Server{
|
|
|
|
Addr: fmt.Sprintf(":%d", port),
|
|
|
|
Handler: mux,
|
|
|
|
},
|
2021-09-11 23:32:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
2021-09-12 17:27:49 +01:00
|
|
|
|
|
|
|
func (w *WebServer) Start() error {
|
|
|
|
if err := w.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WebServer) Close() error {
|
|
|
|
return w.Server.Close()
|
|
|
|
}
|