mirror of
https://github.com/outbackdingo/databunker.git
synced 2026-01-27 10:18:45 +00:00
refactor code
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
@@ -145,46 +144,6 @@ func (e mainEnv) metrics(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||
prometheusHandler().ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// configuration dump API call.
|
||||
func (e mainEnv) configurationDump(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if e.enforceAuth(w, r, nil) == "" {
|
||||
return
|
||||
}
|
||||
resultJSON, _ := json.Marshal(e.conf)
|
||||
finalJSON := fmt.Sprintf(`{"status":"ok","configuration":%s}`, resultJSON)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(finalJSON))
|
||||
}
|
||||
|
||||
func (e mainEnv) cookieSettings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
resultJSON, scriptsJSON, _, err := e.db.getLegalBasisCookieConf()
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, nil)
|
||||
return
|
||||
}
|
||||
resultUIConfJSON, _ := json.Marshal(e.conf.UI)
|
||||
finalJSON := fmt.Sprintf(`{"status":"ok","ui":%s,"rows":%s,"scripts":%s}`, resultUIConfJSON, resultJSON, scriptsJSON)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(finalJSON))
|
||||
}
|
||||
|
||||
// UI configuration dump API call.
|
||||
func (e mainEnv) uiConfigurationDump(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if len(e.conf.Notification.MagicSyncURL) != 0 &&
|
||||
len(e.conf.Notification.MagicSyncToken) != 0 {
|
||||
e.conf.UI.MagicLookup = true
|
||||
} else {
|
||||
e.conf.UI.MagicLookup = false
|
||||
}
|
||||
resultJSON, _ := json.Marshal(e.conf.UI)
|
||||
finalJSON := fmt.Sprintf(`{"status":"ok","ui":%s}`, resultJSON)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(finalJSON))
|
||||
}
|
||||
|
||||
// backupDB API call.
|
||||
func (e mainEnv) backupDB(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if e.enforceAuth(w, r, nil) == "" {
|
||||
@@ -201,9 +160,6 @@ func (e mainEnv) setupRouter() *httprouter.Router {
|
||||
router := httprouter.New()
|
||||
|
||||
router.GET("/v1/sys/backup", e.backupDB)
|
||||
router.GET("/v1/sys/configuration", e.configurationDump)
|
||||
router.GET("/v1/sys/uiconfiguration", e.uiConfigurationDump)
|
||||
router.GET("/v1/sys/cookiesettings", e.cookieSettings)
|
||||
|
||||
router.POST("/v1/user", e.userNew)
|
||||
router.GET("/v1/user/:mode/:address", e.userGet)
|
||||
@@ -526,6 +482,7 @@ func main() {
|
||||
e.dbCleanup()
|
||||
fmt.Printf("host %s\n", cfg.Server.Host+":"+cfg.Server.Port)
|
||||
router := e.setupRouter()
|
||||
router = e.setupConfRouter(router)
|
||||
srv := &http.Server{Addr: cfg.Server.Host + ":" + cfg.Server.Port, Handler: logRequest(router)}
|
||||
|
||||
stop := make(chan os.Signal, 2)
|
||||
|
||||
56
src/conf.go
Normal file
56
src/conf.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
);
|
||||
|
||||
func (e mainEnv) setupConfRouter(router *httprouter.Router ) *httprouter.Router {
|
||||
router.GET("/v1/sys/configuration", e.configurationDump)
|
||||
router.GET("/v1/sys/uiconfiguration", e.uiConfigurationDump)
|
||||
router.GET("/v1/sys/cookiesettings", e.cookieSettings)
|
||||
return router
|
||||
}
|
||||
|
||||
func (e mainEnv) cookieSettings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
resultJSON, scriptsJSON, _, err := e.db.getLegalBasisCookieConf()
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, nil)
|
||||
return
|
||||
}
|
||||
resultUIConfJSON, _ := json.Marshal(e.conf.UI)
|
||||
finalJSON := fmt.Sprintf(`{"status":"ok","ui":%s,"rows":%s,"scripts":%s}`, resultUIConfJSON, resultJSON, scriptsJSON)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(finalJSON))
|
||||
}
|
||||
|
||||
func (e mainEnv) configurationDump(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if e.enforceAuth(w, r, nil) == "" {
|
||||
return
|
||||
}
|
||||
resultJSON, _ := json.Marshal(e.conf)
|
||||
finalJSON := fmt.Sprintf(`{"status":"ok","configuration":%s}`, resultJSON)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(finalJSON))
|
||||
}
|
||||
|
||||
// UI configuration dump API call.
|
||||
func (e mainEnv) uiConfigurationDump(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if len(e.conf.Notification.MagicSyncURL) != 0 &&
|
||||
len(e.conf.Notification.MagicSyncToken) != 0 {
|
||||
e.conf.UI.MagicLookup = true
|
||||
} else {
|
||||
e.conf.UI.MagicLookup = false
|
||||
}
|
||||
resultJSON, _ := json.Marshal(e.conf.UI)
|
||||
finalJSON := fmt.Sprintf(`{"status":"ok","ui":%s}`, resultJSON)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte(finalJSON))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user