From 54526cff4c4b15bfcea8e911ba82a34170db0b5b Mon Sep 17 00:00:00 2001 From: Martin Thielecke Date: Thu, 28 Dec 2017 01:38:30 +0000 Subject: [PATCH] split up main.go to different files to get the stuff running on GAE --- appengine.go | 24 +++++++++++++++++++++ assets.go | 18 ++++++++++++++++ common.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ router.go | 43 +++++++++++++++++++++++++++++++++++++ server.go | 12 +++++++++++ 5 files changed, 157 insertions(+) create mode 100644 appengine.go create mode 100644 assets.go create mode 100644 common.go create mode 100644 router.go create mode 100644 server.go diff --git a/appengine.go b/appengine.go new file mode 100644 index 0000000..68dc7a4 --- /dev/null +++ b/appengine.go @@ -0,0 +1,24 @@ +// +build appengine + +package main + +import ( + "context" + "net/http" + + "google.golang.org/appengine" + "google.golang.org/appengine/urlfetch" +) + +func getHTTPClient(ctx context.Context) *http.Client { + return urlfetch.Client(ctx) +} + +func getContext(r *http.Request) context.Context { + return appengine.NewContext(r) +} + +func main() { + initializeStorage() + appengine.Main() +} diff --git a/assets.go b/assets.go new file mode 100644 index 0000000..877404d --- /dev/null +++ b/assets.go @@ -0,0 +1,18 @@ +package main + +import ( + "mime" + "net/http" + "path/filepath" +) + +func serveAssets(res http.ResponseWriter, r *http.Request) { + data, err := Asset(r.RequestURI[1:]) + if err != nil { + http.Error(res, "Not found", http.StatusNotFound) + return + } + + res.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(r.RequestURI))) + res.Write(data) +} diff --git a/common.go b/common.go new file mode 100644 index 0000000..fc6e40e --- /dev/null +++ b/common.go @@ -0,0 +1,60 @@ +package main // import "github.com/Luzifer/cloudkeys-go" + +//go:generate go-bindata assets + +import ( + "crypto/sha1" + "fmt" + "os" + + "github.com/gorilla/sessions" + "github.com/satori/go.uuid" + log "github.com/sirupsen/logrus" +) + +var ( + storage storageAdapter + cookieStore *sessions.CookieStore + + cfg = loadConfig() + version = "dev" +) + +func initialize() { + if cfg.VersionAndQuit { + fmt.Printf("cloudkeys-go %s\n", version) + os.Exit(0) + } + + if _, err := cfg.ParsedStorage(); err != nil { + log.WithError(err).Error("Unable to parse storage URI") + os.Exit(1) + } + + if cfg.CookieSigningKey == "" { + cfg.CookieSigningKey = uuid.NewV4().String()[:32] + log.Warn("cookie-authkey was set randomly, this will break your sessions!") + } + + if cfg.CookieEncryptKey == "" { + cfg.CookieEncryptKey = uuid.NewV4().String()[:32] + log.Warn("cookie-encryptkey was set randomly, this will break your sessions!") + } + + cookieStore = sessions.NewCookieStore( + []byte(cfg.CookieSigningKey), + []byte(cfg.CookieEncryptKey), + ) +} + +func initializeStorage() { + s, err := getStorageAdapter(cfg) + if err != nil { + log.WithError(err).Fatal("Could not instanciate storage") + } + storage = s +} + +func createUserFilename(username string) string { + return fmt.Sprintf("%x", sha1.Sum([]byte(cfg.UsernameSalt+username))) +} diff --git a/router.go b/router.go new file mode 100644 index 0000000..8e91ec9 --- /dev/null +++ b/router.go @@ -0,0 +1,43 @@ +package main + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +func router() *mux.Router { + r := mux.NewRouter() + r.PathPrefix("/assets/").HandlerFunc(serveAssets) + + r.HandleFunc("/register", httpHelper(simpleTemplateOutput("register.html"))). + Methods("GET") + r.HandleFunc("/register", httpHelper(registerHandler)). + Methods("POST") + + r.HandleFunc("/login", httpHelper(simpleTemplateOutput("login.html"))). + Methods("GET") + r.HandleFunc("/login", httpHelper(loginHandler)). + Methods("POST") + + r.HandleFunc("/logout", httpHelper(logoutHandler)). + Methods("GET") + + r.HandleFunc("/u/{userIndex:[0-9]+}/overview", httpHelper(overviewHandler)). + Methods("GET") + + r.HandleFunc("/u/{userIndex:[0-9]+}/ajax", httpHelper(ajaxGetHandler)). + Methods("GET") + r.HandleFunc("/u/{userIndex:[0-9]+}/ajax", httpHelper(ajaxPostHandler)). + Methods("POST") + + /* --- SUPPORT FOR DEPRECATED METHODS --- */ + r.HandleFunc("/", func(res http.ResponseWriter, r *http.Request) { + http.Redirect(res, r, "u/0/overview", http.StatusFound) + }).Methods("GET") + r.HandleFunc("/overview", func(res http.ResponseWriter, r *http.Request) { + http.Redirect(res, r, "u/0/overview", http.StatusFound) + }).Methods("GET") + + return r +} diff --git a/server.go b/server.go new file mode 100644 index 0000000..9805ad6 --- /dev/null +++ b/server.go @@ -0,0 +1,12 @@ +package main + +import ( + "net/http" +) + +//go:generate go-bindata assets templates + +func init() { + initialize() + http.Handle("/", router()) +}