Graceful failure plus go fmt
If the db file isn't there, or it's corrupt, allow for the creation of an empty slice of Creds.
This commit is contained in:
parent
56bc88847f
commit
bb12b12ebf
37
main.go
37
main.go
@ -3,11 +3,11 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"text/template"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type Cred struct {
|
||||
@ -36,26 +36,31 @@ func passHandler(resp http.ResponseWriter, req *http.Request) {
|
||||
defer m.Unlock()
|
||||
creds = append(creds, c)
|
||||
b, err := json.Marshal(creds)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(*db_file, b, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(*db_file, b, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
http.Redirect(resp, req, "/", http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
func init_db() {
|
||||
b, err := ioutil.ReadFile(*db_file)
|
||||
if err != nil {
|
||||
log.Println("Problem opening db file", err)
|
||||
}
|
||||
err = json.Unmarshal(b, &creds)
|
||||
if err != nil {
|
||||
log.Println("Problem parsing db file", err)
|
||||
creds = make([]Cred, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
b, err := ioutil.ReadFile(*db_file)
|
||||
if err != nil {
|
||||
log.Fatal("Problem opening db file", err)
|
||||
}
|
||||
err = json.Unmarshal(b, &creds)
|
||||
if err != nil {
|
||||
log.Fatal("Problem parsing db file", err)
|
||||
}
|
||||
init_db()
|
||||
http.HandleFunc("/", homeHandler)
|
||||
http.HandleFunc("/pass", passHandler)
|
||||
http.Handle("/s/", http.StripPrefix("/s/",
|
||||
|
Loading…
Reference in New Issue
Block a user