Skip to content

Instantly share code, notes, and snippets.

@cherihung
Created November 24, 2019 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cherihung/6a67d29cda9c62456f175b03eb62eebe to your computer and use it in GitHub Desktop.
Save cherihung/6a67d29cda9c62456f175b03eb62eebe to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/cherihung/go-server-starter/config"
"github.com/gin-gonic/gin"
color "github.com/gookit/color"
)
var (
router *gin.Engine
appConfigs *config.AppConfiguration
)
/* init: initialize configurations before main runs */
func init() {
var err error
appConfigs, err = config.NewAppConfiguration()
mode := "debug"
if err != nil {
fmt.Println(fmt.Errorf("config error: %s", err))
os.Exit(1)
}
if appConfigs.ReleaseMode {
mode = "release"
}
gin.SetMode(mode)
}
/* main: add logging, setup router, start http/https server */
func main() {
var serverErr error
r := setupRouter()
addr := fmt.Sprintf(":%d", 9000)
server := &http.Server{
Addr: addr,
Handler: r,
}
if appConfigs.SSL {
color.Cyan.Println("starting SSL...", addr)
serverErr = server.ListenAndServeTLS("_certs/server.crt", "_certs/server.key")
} else {
color.Cyan.Println("starting...", addr)
serverErr = server.ListenAndServe()
}
if serverErr != nil {
log.Fatal("server start error: ", serverErr)
}
}
func setupRouter() *gin.Engine {
router = gin.New()
router.RedirectTrailingSlash = false
router.Use(gin.Recovery())
return router
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment