craban/server/web/web.go

40 lines
626 B
Go
Raw Normal View History

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) {
2021-09-12 17:57:42 +01:00
fmt.Fprintf(w, "Hello craban!!")
2021-09-11 23:32:07 +01:00
})
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()
}