snippet/gin/gin.go

34 lines
628 B
Go
Raw Normal View History

2021-12-03 03:36:40 +00:00
package gin
import (
"fmt"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func Service(conf *Config) {
2021-12-03 06:00:37 +00:00
if conf == nil {
conf = &Config{
RootPath: "/",
Addr: "0.0.0.0",
Port: 80,
}
}
2021-12-03 03:36:40 +00:00
go func() {
router := gin.New()
2021-12-03 06:00:37 +00:00
routerSetup(router, &conf.RootPath)
2021-12-03 03:36:40 +00:00
s := &http.Server{
Addr: fmt.Sprintf("%s:%d", conf.Addr, conf.Port),
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Printf("start service on %s", fmt.Sprintf("%s:%d", conf.Addr, conf.Port))
log.Fatal(s.ListenAndServe())
}()
}