Browse Source

feat: Disable debug output for release mode

build/docker
Evan 9 months ago
parent
commit
7555e63fbf
  1. 7
      Makefile
  2. 2
      cmd/config.go
  3. 1
      models/models.go
  4. 8
      modules/authenticate/middleware.go
  5. 2
      services/auth/auth.go
  6. 8
      services/services.go
  7. 6
      services/services_min.go

7
Makefile

@ -25,12 +25,13 @@ build:
@echo " > Building binary..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
all: swagger build
debug: statik build
all: swagger statik build min
min:
@echo " > Building minimized binary..."
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -tags min $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME) $(GOFILES)
@GOPATH=$(GOPATH) GOBIN=$(GOBIN) go build -tags min $(LDFLAGS) -o $(GOBIN)/$(PROJECTNAME)-min $(GOFILES)
go-get:
@echo " > Checking if there is any missing dependencies..."

2
cmd/config.go

@ -22,7 +22,9 @@ func setDefaults() {
viper.SetDefault("app.mode", "debug")
viper.SetDefault("app.domain", "evanchen.company")
viper.SetDefault("app.timezone", "Asia/Taipei")
viper.SetDefault("calendar.allow_orphan", true)
viper.SetDefault("server.port", "8080")
viper.SetDefault("server.https_only", false)
viper.SetDefault("auth.enabled", true)
viper.SetDefault("ui.disabled", false)
viper.SetDefault("swagger.enabled", true)

1
models/models.go

@ -29,6 +29,7 @@ func InitSqlite() {
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
}
sqliteDB, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
if err != nil {
panic("failed to connect database")

8
modules/authenticate/middleware.go

@ -31,9 +31,9 @@ func Sessions() gin.HandlerFunc {
db = SessionDB
db.AutoMigrate(&Session{})
shouldSecure = true
if viper.GetString("app.mode") == "debug" {
shouldSecure = false
shouldSecure = false
if viper.GetBool("server.https_only") {
shouldSecure = true
}
return func(c *gin.Context) {
sessionID, err := c.Cookie("GCSID")
@ -43,7 +43,6 @@ func Sessions() gin.HandlerFunc {
}
var session Session
if err := db.Where("session_id = ?", sessionID).First(&session).Error; err != nil {
c.SetCookie("GCSID", "", -1, "/", c.Request.Host, true, true)
c.Next()
return
}
@ -65,7 +64,6 @@ func LoginUser(c *gin.Context, userID uint) {
func LogoutUser(c *gin.Context) {
s, ok := c.Get("GCSID")
c.SetCookie("GCSID", "del", -1, "/", c.Request.Host, true, true)
if !ok {
return
}

2
services/auth/auth.go

@ -187,13 +187,11 @@ func loadUser(c *gin.Context) (user models.User, ok bool) {
s, ok := c.Get("GCSID")
if !ok {
c.AbortWithStatus(401)
c.SetCookie("GCSID", "del", -1, "/", c.Request.Host, true, true)
return
}
session, ok := s.(authenticate.Session)
if !ok {
c.AbortWithStatus(401)
c.SetCookie("GCSID", "del", -1, "/", c.Request.Host, true, true)
return
}
user, err := models.LoadUser(session.UserID)

8
services/services.go

@ -17,7 +17,7 @@ import (
)
//Version shows the version of the build.
const Version = "development"
var Version = "development"
//Start starts the service server.
// @title Calendar API
@ -30,8 +30,10 @@ const Version = "development"
// @name GCSID
// @x-extension-openapi {"example": "value on a json format"}
func Start() {
if viper.GetString("app.mode") != "debug" {
gin.SetMode(gin.ReleaseMode)
}
server := gin.Default()
// authenticate.Init()
api := server.Group("/api", authenticate.Sessions())
auth.RegisterAuthService(api)
auth.RegisterUserService(api)
@ -52,6 +54,6 @@ func Start() {
// c.File("../ui/dist/index.html")
// })
}
fmt.Printf("Server started...")
server.Run(fmt.Sprintf(":%s", viper.GetString("server.port")))
}

6
services/services_min.go

@ -13,7 +13,7 @@ import (
)
//Version shows the version of the build.
const Version = "development"
var Version = "development"
//Start starts the service server.
// @title Calendar API
@ -26,8 +26,10 @@ const Version = "development"
// @name GCSID
// @x-extension-openapi {"example": "value on a json format"}
func Start() {
if viper.GetString("app.mode") != "debug" {
gin.SetMode(gin.ReleaseMode)
}
server := gin.Default()
// authenticate.Init()
api := server.Group("/api", authenticate.Sessions())
auth.RegisterAuthService(api)
auth.RegisterUserService(api)