merp/server.go

58 lines
1.1 KiB
Go

//3456789112345676892123456789312345678941234567895123456789612345678971234567898
package main
import "fmt"
import "net/http"
import "os"
import "time"
//import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
/* func initDb() {
db, err := sql.Open("postgres", os.Getenv("POSTGRES_DB_URL"))
if err != nil {
log.Fatal().Msg(err)
}
}
*/
func serve() {
if os.Getenv("DEBUG") == "" {
gin.SetMode(gin.ReleaseMode)
}
// empty router
r := gin.New()
// wrap panics:
r.Use(gin.Recovery())
// attach logger middleware
r.Use(ginzerolog.Logger("gin"))
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
// call it, it returns the appropriate handler function
r.GET("/merp/for/:thing", HandleNewMerp())
port := "8080"
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
s := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}