feat: switch from ioutil to os package and add os.File input to dumpDbTOFile
This commit is contained in:
parent
a31df48b23
commit
65830755b9
@ -4,11 +4,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -133,16 +133,19 @@ func shortener(u string, s int) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dumpDbToFile dumps the kv pairs from the in memory database to file
|
// dumpDbToFile dumps the kv pairs from the in memory database to file
|
||||||
func dumpDbTOFile() (int, error) {
|
func dumpDbTOFile(f *os.File) (int, error) {
|
||||||
i := Pool.Items()
|
i := Pool.Items()
|
||||||
dumpObj, _ := json.Marshal(i)
|
dumpObj, _ := json.Marshal(i)
|
||||||
return len(i), ioutil.WriteFile(DumpFile, dumpObj, 0644)
|
if _, err := f.Write(dumpObj); err != nil {
|
||||||
|
return len(i), err
|
||||||
|
}
|
||||||
|
return len(i), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadFromFile loads kv pairs from the dumpFile json to the in memory database
|
// loadFromFile loads kv pairs from the dumpFile json to the in memory database
|
||||||
func loadFromFile() (int, error) {
|
func loadFromFile() (int, error) {
|
||||||
dumpObj := make(map[string]cache.Item)
|
dumpObj := make(map[string]cache.Item)
|
||||||
jsonFile, err := ioutil.ReadFile(DumpFile)
|
jsonFile, err := os.ReadFile(DumpFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -249,7 +252,14 @@ func Redir(t *template.Template) func(ctx *fasthttp.RequestCtx) {
|
|||||||
// KV db to the DumpFile file
|
// KV db to the DumpFile file
|
||||||
func ToFile(t *template.Template) func(ctx *fasthttp.RequestCtx) {
|
func ToFile(t *template.Template) func(ctx *fasthttp.RequestCtx) {
|
||||||
return func(ctx *fasthttp.RequestCtx) {
|
return func(ctx *fasthttp.RequestCtx) {
|
||||||
i, err := dumpDbTOFile()
|
f, err := os.Create(DumpFile)
|
||||||
|
if err != nil {
|
||||||
|
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||||
|
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
|
||||||
|
t.Execute(ctx, internalError("Failed to create DB dump file", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i, err := dumpDbTOFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SetStatusCode(http.StatusInternalServerError)
|
ctx.SetStatusCode(http.StatusInternalServerError)
|
||||||
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
|
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user