Created
November 24, 2019 01:18
-
-
Save cherihung/6a67d29cda9c62456f175b03eb62eebe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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