-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
43 lines (34 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//go:generate bash -c "mkdir -p codegen && go run github.com/deepmap/oapi-codegen/v2/cmd/[email protected] -generate types,server,spec -package codegen api/openapi.yaml > codegen/hello_world_api.go"
package main
import (
"net"
"net/http"
"time"
"github.com/IceWhaleTech/CasaOS-Common/model"
"github.com/IceWhaleTech/CasaOS-HelloWorld/config"
"github.com/IceWhaleTech/CasaOS-HelloWorld/route"
"github.com/IceWhaleTech/CasaOS-HelloWorld/service"
)
func main() {
service.Initialize(config.CommonInfo.RuntimePath)
// setup listener
listener, _err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", "0"))
if _err != nil { // using `_err` to avoid shadowing the `err` variable
panic(_err)
}
// initialize routers and register at gateway
if err := service.Gateway.CreateRoute(&model.Route{
Path: route.APIPath,
Target: "http://" + listener.Addr().String(),
}); err != nil {
panic(err)
}
s := &http.Server{
Handler: route.GetRouter(),
ReadHeaderTimeout: 5 * time.Second, // fix G112: Potential slowloris attack (see https://github.com/securego/gosec)
}
_err = s.Serve(listener) // not using http.serve() to fix G114: Use of net/http serve function that has no support for setting timeouts (see https://github.com/securego/gosec)
if _err != nil {
panic(_err)
}
}