zprox_bkup_1/app/main.go
2024-08-24 16:12:40 +03:00

71 lines
1.3 KiB
Go

package main
import (
"context"
"fmt"
"net"
"net/http"
"github.com/zeevdiukman/zprox/helpers"
"go.uber.org/fx"
)
const (
sslCert = "./certs/ssl.crt"
sslKey = "./certs/ssl.key"
)
func init() {
fmt.Println("ZPROX started")
}
func StartNewServer(lc fx.Lifecycle) *http.Server {
s := &http.Server{
Addr: ":8080",
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
}
helpers.Lines("Starting HTTP server at", s.Addr)
go s.ServeTLS(ln, sslCert, sslKey)
return nil
},
OnStop: func(ctx context.Context) error {
return s.Shutdown(ctx)
},
})
return s
}
func StartNewRouter(lc fx.Lifecycle) *http.ServeMux {
r := http.NewServeMux()
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
fmt.Println("HTTP router started.")
return nil
},
OnStop: func(ctx context.Context) error {
fmt.Println("HTTP router stoped.")
return nil
},
})
return r
}
// func GetContructors(lc fx.Lifecycle) any {
// constructors := make(...interface{}, 0)
// constructors = append(constructors, StartNewServer, StartNewRouter)
// return constructors
// }
func main() {
fx.New(
fx.Provide(StartNewServer, StartNewRouter),
fx.Invoke(func(*http.Server) {}),
).Run()
// var h HandlerFunc
// h:=func(a HandlerFunc)HandlerFunc{
}