diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2021-08-01 17:54:53 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2021-08-01 17:54:53 -0600 |
commit | 069ee93de17579230ef749d5804df7a0ac350ac5 (patch) | |
tree | 0beac3e94e237f1382f35650a2f455a79c63f03c /srv/cmd | |
parent | 6bec0e3964f03f776841cec5e639784222a33958 (diff) |
implemented PoW backend
Diffstat (limited to 'srv/cmd')
-rw-r--r-- | srv/cmd/mediocre-blog/api.go | 24 | ||||
-rw-r--r-- | srv/cmd/mediocre-blog/main.go | 38 | ||||
-rw-r--r-- | srv/cmd/mediocre-blog/pow.go | 22 |
3 files changed, 82 insertions, 2 deletions
diff --git a/srv/cmd/mediocre-blog/api.go b/srv/cmd/mediocre-blog/api.go new file mode 100644 index 0000000..b4f90d6 --- /dev/null +++ b/srv/cmd/mediocre-blog/api.go @@ -0,0 +1,24 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" +) + +func internalServerError(rw http.ResponseWriter, r *http.Request, err error) { + http.Error(rw, "internal server error", 500) + log.Printf("%s %s: internal server error: %v", r.Method, r.URL, err) +} + +func jsonResult(rw http.ResponseWriter, r *http.Request, v interface{}) { + b, err := json.Marshal(v) + if err != nil { + internalServerError(rw, r, err) + return + } + b = append(b, '\n') + + rw.Header().Set("Content-Type", "application/json") + rw.Write(b) +} diff --git a/srv/cmd/mediocre-blog/main.go b/srv/cmd/mediocre-blog/main.go index a1b20be..2952999 100644 --- a/srv/cmd/mediocre-blog/main.go +++ b/srv/cmd/mediocre-blog/main.go @@ -4,20 +4,54 @@ import ( "flag" "log" "net/http" + "strconv" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/pow" + "github.com/tilinna/clock" ) func main() { staticDir := flag.String("static-dir", "", "Directory from which static files are served") - //redisAddr := flag.String("redis-addr", "127.0.0.1:6379", "Address which redis is listening on") listenAddr := flag.String("listen-addr", ":4000", "Address to listen for HTTP requests on") + powTargetStr := flag.String("pow-target", "0x000FFFF", "Proof-of-work target, lower is more difficult") + powSecret := flag.String("pow-secret", "", "Secret used to sign proof-of-work challenge seeds") + + // parse config + flag.Parse() - if *staticDir == "" { + switch { + case *staticDir == "": log.Fatal("-static-dir is required") + case *powSecret == "": + log.Fatal("-pow-secret is required") + } + + powTargetUint, err := strconv.ParseUint(*powTargetStr, 0, 32) + if err != nil { + log.Fatalf("parsing -pow-target: %v", err) } + powTarget := uint32(powTargetUint) + + // initialization + + clock := clock.Realtime() + + powStore := pow.NewMemoryStore(clock) + defer powStore.Close() + + mgr := pow.NewManager(pow.ManagerParams{ + Clock: clock, + Store: powStore, + Secret: []byte(*powSecret), + Target: powTarget, + }) mux := http.NewServeMux() mux.Handle("/", http.FileServer(http.Dir(*staticDir))) + mux.Handle("/api/pow/challenge", newPowChallengeHandler(mgr)) + + // run log.Printf("listening on %q", *listenAddr) log.Fatal(http.ListenAndServe(*listenAddr, mux)) diff --git a/srv/cmd/mediocre-blog/pow.go b/srv/cmd/mediocre-blog/pow.go new file mode 100644 index 0000000..22b82f3 --- /dev/null +++ b/srv/cmd/mediocre-blog/pow.go @@ -0,0 +1,22 @@ +package main + +import ( + "encoding/hex" + "net/http" + + "github.com/mediocregopher/blog.mediocregopher.com/srv/pow" +) + +func newPowChallengeHandler(mgr pow.Manager) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + challenge := mgr.NewChallenge() + + jsonResult(rw, r, struct { + Seed string `json:"seed"` + Target uint32 `json:"target"` + }{ + Seed: hex.EncodeToString(challenge.Seed), + Target: challenge.Target, + }) + }) +} |