add dev mode

This commit is contained in:
eugene
2017-12-22 13:52:31 -06:00
parent 3a26eec1fc
commit 7b5733e248
3 changed files with 27 additions and 7 deletions
+8 -4
View File
@@ -24,11 +24,10 @@ var opts struct {
GoogleCSEC string `long:"google-csec" env:"REMARK_GOOGLE_CSEC" description:"Google OAuth client secret"`
GithubCID string `long:"github-cid" env:"REMARK_GITHUB_CID" description:"Github OAuth client ID"`
GithubCSEC string `long:"github-csec" env:"REMARK_GITHUB_CSEC" description:"Github OAuth client secret"`
YandexCID string `long:"yandex-cid" env:"REMARK_YANDEX_CID" description:"Yandex OAuth client ID"`
YandexCSEC string `long:"yandex-csec" env:"REMARK_YANDEX_CSEC" description:"Yandex OAuth client secret"`
Admins []string `long:"admin" env:"ADMIN" default:"umputun@gmail.com" description:"admin(s) names" env-delim:","`
Dbg bool `long:"dbg" env:"DEBUG" description:"debug mode"`
Admins []string `long:"admin" env:"ADMIN" default:"umputun@gmail.com" description:"admin(s) names" env-delim:","`
DevMode bool `long:"dev" env:"DEV" description:"dev mode mode"`
Dbg bool `long:"dbg" env:"DEBUG" description:"debug mode"`
}
var revision = "unknown"
@@ -41,6 +40,10 @@ func main() {
setupLog(opts.Dbg)
log.Print("[INFO] started remark")
if opts.DevMode {
log.Printf("[WARN] running in dev mode, no auth!")
}
dataStore, err := store.NewBoltDB(opts.DBFile)
if err != nil {
log.Fatalf("[ERROR] can't initialize data store, %+v", err)
@@ -52,6 +55,7 @@ func main() {
Store: dataStore,
SessionStore: sessionStore,
Admins: opts.Admins,
DevMode: opts.DevMode,
AuthGoogle: auth.NewGoogle(auth.Params{
Cid: opts.GoogleCID,
Csecret: opts.GoogleCSEC,
+17 -1
View File
@@ -110,10 +110,26 @@ func Recoverer(next http.Handler) http.Handler {
type contextKey string
// Auth adds auth from session and populate user info
func Auth(sessionStore *sessions.FilesystemStore, admins []string) func(http.Handler) http.Handler {
func Auth(sessionStore *sessions.FilesystemStore, admins []string, devMode bool) func(http.Handler) http.Handler {
f := func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// for dev mode skip all real auth, make dev admin user
if devMode {
user := store.User{
ID: "dev",
Name: "developer one",
Picture: "https://friends.radio-t.com/resources/images/rt_logo_64.png",
Profile: "https://radio-t.com/info/",
Admin: true,
}
ctx := r.Context()
ctx = context.WithValue(ctx, contextKey("user"), user)
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
return
}
session, err := sessionStore.Get(r, "remark")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
+2 -2
View File
@@ -26,6 +26,7 @@ type Server struct {
AuthGoogle *auth.Provider
AuthGithub *auth.Provider
SessionStore *sessions.FilesystemStore
DevMode bool
}
// Run the lister and request's router, activate rest server
@@ -49,7 +50,7 @@ func (s *Server) Run() {
rapi.Get("/last/{max}", s.getLastComments)
rapi.Get("/count", s.getCountCtrl)
rapi.With(Auth(s.SessionStore, s.Admins)).Group(func(r chi.Router) {
rapi.With(Auth(s.SessionStore, s.Admins, s.DevMode)).Group(func(r chi.Router) {
r.Post("/comment", s.createCommentCtrl)
r.Get("/user", s.getUserInfo)
r.Put("/vote/{id}", s.voteCtrl)
@@ -72,7 +73,6 @@ func (s *Server) addFileServer(r chi.Router, path string, root http.FileSystem)
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Print(r.URL.Path)
// don't show dirs, just serve files
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)