http stub
parent
6923788200
commit
a8b5942833
|
|
@ -0,0 +1,43 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Context struct {
|
||||||
|
User string
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTTP(port int, db DB) error {
|
||||||
|
foo := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
}
|
||||||
|
foo = withAuth(foo)
|
||||||
|
return http.ListenAndServe(fmt.Sprintf(":%d", port), http.HandlerFunc(foo))
|
||||||
|
}
|
||||||
|
|
||||||
|
func extract(ctx context.Context) Context {
|
||||||
|
v := ctx.Value("__context")
|
||||||
|
v2, _ := v.(Context)
|
||||||
|
return v2
|
||||||
|
}
|
||||||
|
|
||||||
|
func inject(ctx context.Context, v Context) context.Context {
|
||||||
|
return context.WithValue(ctx, "__context", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func withAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
u, _, ok := r.BasicAuth()
|
||||||
|
if !ok || u == "" {
|
||||||
|
w.Header().Set("WWW-Authenticate", "Basic")
|
||||||
|
w.WriteHeader(401)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c := extract(r.Context())
|
||||||
|
c.User = u
|
||||||
|
r = r.WithContext(inject(r.Context(), c))
|
||||||
|
foo(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
8
main.go
8
main.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -59,6 +60,13 @@ func Main() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
if port, _ := strconv.Atoi(os.Getenv("PORT")); port > 0 {
|
||||||
|
return HTTP(port, db)
|
||||||
|
}
|
||||||
|
return Terminal(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Terminal(db DB) error {
|
||||||
user := IDU(os.Getenv("USER"))
|
user := IDU(os.Getenv("USER"))
|
||||||
failed, err := Review(db, user)
|
failed, err := Review(db, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue