2022-08-26 04:21:46 +00:00
|
|
|
package nonshy
|
2022-08-10 05:10:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/router"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
// WebServer is the main entry point for the `nonshy web` command.
|
2022-08-10 05:10:47 +00:00
|
|
|
type WebServer struct {
|
|
|
|
// Configuration
|
|
|
|
Host string // host interface, default "0.0.0.0"
|
|
|
|
Port int // default 8080
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the server.
|
|
|
|
func (ws *WebServer) Run() error {
|
|
|
|
// Defaults
|
|
|
|
if ws.Host == "" {
|
|
|
|
ws.Host = "0.0.0.0"
|
|
|
|
}
|
|
|
|
if ws.Port == 0 {
|
|
|
|
ws.Port = 8080
|
|
|
|
}
|
|
|
|
|
|
|
|
s := http.Server{
|
|
|
|
Addr: fmt.Sprintf("%s:%d", ws.Host, ws.Port),
|
|
|
|
Handler: router.New(),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Listening at http://%s:%d", ws.Host, ws.Port)
|
|
|
|
return s.ListenAndServe()
|
|
|
|
}
|