Skip to content

Commit

Permalink
feat[Https]: added https support (go-admin-team#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjianzhang committed Jun 11, 2020
1 parent 5d69b32 commit 0177fb1
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 5 deletions.
10 changes: 8 additions & 2 deletions cmd/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ func run() error {

go func() {
// 服务连接
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
if config2.ApplicationConfig.IsHttps {
if err := srv.ListenAndServeTLS(config2.SslConfig.Pem, config2.SslConfig.KeyStr); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
} else {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}
}()
content, _ := ioutil.ReadFile("./static/go-admin.txt")
Expand Down
5 changes: 5 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ settings:
port: 8000
readtimeout: 1
writertimeout: 2
domain: localhost:8000
ishttps: false
ssl:
key: keystring
pem: temp/pem.pem
log:
dir: temp/logs
jwt:
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/json-iterator/go v1.1.8 // indirect
github.com/mailru/easyjson v0.7.1 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mojocn/base64Captcha v1.3.1
github.com/mssola/user_agent v0.5.1
github.com/pkg/errors v0.8.1
Expand All @@ -29,11 +28,11 @@ require (
github.com/shirou/gopsutil v2.20.3+incompatible
github.com/sirupsen/logrus v1.2.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.6.2
github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.5
github.com/ugorji/go v1.1.7 // indirect
github.com/unrolled/secure v1.0.8
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/tools v0.0.0-20200402223321-bcf690261a44 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,12 @@ github.com/ugorji/go/codec v1.1.5-pre h1:5YV9PsFAN+ndcCtTM7s60no7nY7eTG3LPtxhSwu
github.com/ugorji/go/codec v1.1.5-pre/go.mod h1:tULtS6Gy1AE1yCENaw4Vb//HLH5njI2tfCQDUqRd8fI=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/unrolled/secure v1.0.8 h1:JaMvKbe4CRt8oyxVXn+xY+6jlqd7pyJNSVkmsBxxQsM=
github.com/unrolled/secure v1.0.8/go.mod h1:fO+mEan+FLB0CdEnHf6Q4ZZVNqG+5fuLFnP8p0BXDPI=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
21 changes: 21 additions & 0 deletions handler/httpshandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handler

import (
"github.com/gin-gonic/gin"
"github.com/unrolled/secure"
"go-admin/tools/config"
)

func TlsHandler() gin.HandlerFunc {
return func(c *gin.Context) {
secureMiddleware := secure.New(secure.Options{
SSLRedirect: true,
SSLHost: config.ApplicationConfig.Domain,
})
err := secureMiddleware.Process(c.Writer, c.Request)
if err != nil {
return
}
c.Next()
}
}
5 changes: 5 additions & 0 deletions router/init_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package router

import (
"github.com/gin-gonic/gin"
"go-admin/handler"
"go-admin/middleware"
_ "go-admin/pkg/jwtauth"
"go-admin/tools"
config2 "go-admin/tools/config"
)

func InitRouter() *gin.Engine {

r := gin.New()
if config2.ApplicationConfig.IsHttps {
r.Use(handler.TlsHandler())
}
middleware.InitMiddleware(r)
// the jwt middleware
authMiddleware, err := middleware.AuthInit()
Expand Down
22 changes: 21 additions & 1 deletion tools/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,39 @@ type Application struct {
JwtSecret string
Mode string
DemoMsg string
Domain string
IsHttps bool
}

func InitApplication(cfg *viper.Viper) *Application {
return &Application{
ReadTimeout: cfg.GetInt("readTimeout"),
WriterTimeout: cfg.GetInt("writerTimeout"),
Host: cfg.GetString("host"),
Port: cfg.GetString("port"),
Port: portDefault(cfg),
Name: cfg.GetString("name"),
JwtSecret: cfg.GetString("jwtSecret"),
Mode: cfg.GetString("mode"),
DemoMsg: cfg.GetString("demoMsg"),
Domain: cfg.GetString("domain"),
IsHttps: cfg.GetBool("ishttps"),
}
}

var ApplicationConfig = new(Application)

func portDefault(cfg *viper.Viper) string {
if cfg.GetString("port") == "" {
return "8000"
} else {
return cfg.GetString("port")
}
}

func isHttpsDefault(cfg *viper.Viper) bool {
if cfg.GetString("ishttps") == "" || cfg.GetBool("ishttps") == false{
return false
} else {
return true
}
}
7 changes: 7 additions & 0 deletions tools/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var cfgDatabase *viper.Viper
var cfgApplication *viper.Viper
var cfgJwt *viper.Viper
var cfgLog *viper.Viper
var cfgSsl *viper.Viper


//载入配置文件
Expand Down Expand Up @@ -53,6 +54,12 @@ func ConfigSetup(path string) {
panic("config not found settings.log")
}
LogConfig = InitLog(cfgLog)

cfgSsl = viper.Sub("settings.ssl")
if cfgSsl == nil {
panic("config not found settings.ssl")
}
SslConfig = InitSsl(cfgSsl)
}


Expand Down
17 changes: 17 additions & 0 deletions tools/config/ssl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import "github.com/spf13/viper"

type Ssl struct {
KeyStr string
Pem string
}

func InitSsl(cfg *viper.Viper) *Ssl {
return &Ssl{
KeyStr: cfg.GetString("key"),
Pem: cfg.GetString("pem"),
}
}

var SslConfig = new(Ssl)

0 comments on commit 0177fb1

Please sign in to comment.