heritage-api/gin/gin.go

58 lines
921 B
Go
Raw Permalink Normal View History

2026-03-12 09:28:19 +00:00
package gin
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
var (
addr string
port int
ssl string
sslPem string
sslKey string
)
func Service() {
addr = os.Getenv("GIN_ADDR")
if addr == "" {
addr = "0.0.0.0"
}
port, _ = strconv.Atoi(os.Getenv("GIN_PORT"))
if port == 0 {
port = 8080
}
go func() {
gin.DefaultWriter = io.Discard
router := gin.New()
routerSetup(router)
s := &http.Server{
Addr: fmt.Sprintf("%s:%d", addr, port),
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
logrus.WithFields(logrus.Fields{
"func": "Service",
}).Infof("start service on %s:%d", addr, port)
if ssl == "true" {
log.Fatal(s.ListenAndServeTLS(sslPem, sslKey))
} else {
log.Fatal(s.ListenAndServe())
}
}()
}