1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package main
- import (
- "flag"
- "net/http"
- "zero-examples/chat/internal"
- "github.com/tal-tech/go-zero/core/logx"
- "github.com/tal-tech/go-zero/core/service"
- "github.com/tal-tech/go-zero/rest"
- )
- var (
- port = flag.Int("port", 3333, "the port to listen")
- timeout = flag.Int64("timeout", 0, "timeout of milliseconds")
- cpu = flag.Int64("cpu", 500, "cpu threshold")
- )
- func main() {
- flag.Parse()
- logx.Disable()
- engine := rest.MustNewServer(rest.RestConf{
- ServiceConf: service.ServiceConf{
- Log: logx.LogConf{
- Mode: "console",
- },
- },
- Port: *port,
- Timeout: *timeout,
- CpuThreshold: *cpu,
- })
- defer engine.Stop()
- hub := internal.NewHub()
- go hub.Run()
- engine.AddRoute(rest.Route{
- Method: http.MethodGet,
- Path: "/",
- Handler: func(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path != "/" {
- http.Error(w, "Not found", http.StatusNotFound)
- return
- }
- if r.Method != "GET" {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
- http.ServeFile(w, r, "home.html")
- },
- })
- engine.AddRoute(rest.Route{
- Method: http.MethodGet,
- Path: "/ws",
- Handler: func(w http.ResponseWriter, r *http.Request) {
- internal.ServeWs(hub, w, r)
- },
- })
- engine.Start()
- }
|