merp/server.go

58 lines
1.1 KiB
Go
Raw Normal View History

2019-10-03 19:30:04 +00:00
//3456789112345676892123456789312345678941234567895123456789612345678971234567898
package main
2019-10-25 15:09:31 +00:00
import "fmt"
2019-10-25 16:17:14 +00:00
import "net/http"
2019-10-25 15:09:31 +00:00
import "os"
2019-10-25 16:17:14 +00:00
import "time"
//import "github.com/rs/zerolog/log"
import "github.com/gin-gonic/gin"
import "github.com/dn365/gin-zerolog"
2019-10-03 19:30:04 +00:00
2019-10-25 15:09:31 +00:00
/* func initDb() {
db, err := sql.Open("postgres", os.Getenv("POSTGRES_DB_URL"))
if err != nil {
log.Fatal().Msg(err)
2019-10-25 12:01:38 +00:00
}
}
2019-10-25 15:09:31 +00:00
*/
2019-10-25 12:01:38 +00:00
2019-10-25 15:09:31 +00:00
func serve() {
2019-10-25 16:17:14 +00:00
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")
2019-10-25 15:09:31 +00:00
}
2019-10-03 19:30:04 +00:00
2019-10-25 16:17:14 +00:00
s := &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
2019-10-03 19:30:04 +00:00
}
2019-10-25 16:17:14 +00:00
s.ListenAndServe()
2019-10-03 19:30:04 +00:00
}