Use builtin status codes, change HTTPS option
This commit is contained in:
parent
bdda2de936
commit
5e363df9b0
39
main.go
39
main.go
@ -10,6 +10,7 @@ import (
|
||||
"flag"
|
||||
"time"
|
||||
"strings"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
@ -19,7 +20,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
gVersion = "0.5"
|
||||
gVersion = "0.5.1"
|
||||
)
|
||||
|
||||
func marshalResultToText(res *ldap.SearchResult, delimiter string, showValueName, showDN bool) string {
|
||||
@ -54,9 +55,9 @@ func sendResponse(c *gin.Context, res *ldap.SearchResult, format string) {
|
||||
// 404 Not found
|
||||
if len(res.Entries) == 0 {
|
||||
if strings.EqualFold(format, "json") {
|
||||
c.JSON(404, gin.H{"error": "No result"})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No result"})
|
||||
} else {
|
||||
c.String(404, "No result")
|
||||
c.String(http.StatusNotFound, "No result")
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -69,27 +70,27 @@ func sendResponse(c *gin.Context, res *ldap.SearchResult, format string) {
|
||||
log.Errorf("Error marshalling result to json: %v", err)
|
||||
}
|
||||
log.Debugf("%v\n", string(jsonRes))
|
||||
c.String(200, string(jsonRes))
|
||||
c.String(http.StatusOK, string(jsonRes))
|
||||
|
||||
} else if strings.EqualFold(format, "text") {
|
||||
txtRes := marshalResultToText(res, "=", false, true)
|
||||
log.Debugf("%v\n", string(txtRes))
|
||||
c.String(200, string(txtRes))
|
||||
c.String(http.StatusOK, string(txtRes))
|
||||
|
||||
} else if strings.EqualFold(format, "ldif") {
|
||||
txtRes := marshalResultToText(res, ": ", false, true)
|
||||
log.Debugf("%v\n", string(txtRes))
|
||||
c.String(200, string(txtRes))
|
||||
c.String(http.StatusOK, string(txtRes))
|
||||
|
||||
} else if strings.EqualFold(format, "textvalue") {
|
||||
txtRes := marshalResultToText(res, "", true, true)
|
||||
log.Debugf("%v\n", string(txtRes))
|
||||
c.String(200, string(txtRes))
|
||||
c.String(http.StatusOK, string(txtRes))
|
||||
|
||||
} else if strings.EqualFold(format, "textvalue-nodn") {
|
||||
txtRes := marshalResultToText(res, "", true, false)
|
||||
log.Debugf("%v\n", string(txtRes))
|
||||
c.String(200, string(txtRes))
|
||||
c.String(http.StatusOK, string(txtRes))
|
||||
|
||||
}
|
||||
}
|
||||
@ -149,7 +150,7 @@ func basicAuth(c *gin.Context) {
|
||||
if hasAuth && user == "admin" && password == "admin" {
|
||||
log.Infof("[%s]: User %s successfully authenticated", c.Request.RemoteAddr, user)
|
||||
} else {
|
||||
c.AbortWithStatus(401)
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
|
||||
return
|
||||
}
|
||||
@ -158,7 +159,7 @@ func basicAuth(c *gin.Context) {
|
||||
|
||||
func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "pong",
|
||||
})
|
||||
})
|
||||
@ -178,7 +179,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
// If OU does not exist, we'll get err='LDAP Result Code 32 "No Such Object"'
|
||||
if err != nil {
|
||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||
c.AbortWithError(500, err)
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
sendResponse(c, res, format)
|
||||
@ -195,7 +196,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
|
||||
modified, err := checkIfModifiedSince(c, myldap, ou, cn, class, "ALL")
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -203,12 +204,12 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
res, err := doLdapSearch(myldap, ou, cn, class, "ALL")
|
||||
if err != nil {
|
||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||
c.AbortWithError(500, err)
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
sendResponse(c, res, format)
|
||||
} else {
|
||||
c.String(304, "")
|
||||
c.String(http.StatusNotModified, "")
|
||||
}
|
||||
return
|
||||
})
|
||||
@ -225,7 +226,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
res, err := doLdapSearch(myldap, ou, cn, class, attr)
|
||||
if err != nil {
|
||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||
c.AbortWithError(500, err)
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
sendResponse(c, res, format)
|
||||
@ -243,7 +244,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
|
||||
modified, err := checkIfModifiedSince(c, myldap, ou, cn, class, attr)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -251,12 +252,12 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||
res, err := doLdapSearch(myldap, ou, cn, class, attr)
|
||||
if err != nil {
|
||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||
c.AbortWithError(500, err)
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
sendResponse(c, res, format)
|
||||
} else {
|
||||
c.String(304, "")
|
||||
c.String(http.StatusNotModified, "")
|
||||
}
|
||||
return
|
||||
})
|
||||
@ -335,7 +336,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
if false == doTls {
|
||||
doTls = viper.GetBool("SERVE_HTTPS")
|
||||
doTls = viper.GetBool("HTTPS")
|
||||
}
|
||||
if doTls && len(tlsCert) == 0 {
|
||||
l := viper.GetString("SSL_CERTIFICATE")
|
||||
|
Loading…
Reference in New Issue
Block a user