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
15
main.go
15
main.go
@ -3,11 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"text/template"
|
"text/template"
|
||||||
"io/ioutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cred struct {
|
type Cred struct {
|
||||||
@ -46,16 +46,21 @@ func passHandler(resp http.ResponseWriter, req *http.Request) {
|
|||||||
http.Redirect(resp, req, "/", http.StatusTemporaryRedirect)
|
http.Redirect(resp, req, "/", http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func init_db() {
|
||||||
flag.Parse()
|
|
||||||
b, err := ioutil.ReadFile(*db_file)
|
b, err := ioutil.ReadFile(*db_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Problem opening db file", err)
|
log.Println("Problem opening db file", err)
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(b, &creds)
|
err = json.Unmarshal(b, &creds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Problem parsing db file", err)
|
log.Println("Problem parsing db file", err)
|
||||||
|
creds = make([]Cred, 0)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
init_db()
|
||||||
http.HandleFunc("/", homeHandler)
|
http.HandleFunc("/", homeHandler)
|
||||||
http.HandleFunc("/pass", passHandler)
|
http.HandleFunc("/pass", passHandler)
|
||||||
http.Handle("/s/", http.StripPrefix("/s/",
|
http.Handle("/s/", http.StripPrefix("/s/",
|
||||||
|
Loading…
Reference in New Issue
Block a user