mirror of
https://github.com/Luzifer/cloudkeys-go.git
synced 2024-11-08 14:10:05 +00:00
split up main.go to different files to get the stuff running on GAE
This commit is contained in:
parent
a7e719a182
commit
54526cff4c
5 changed files with 157 additions and 0 deletions
24
appengine.go
Normal file
24
appengine.go
Normal file
|
@ -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()
|
||||
}
|
18
assets.go
Normal file
18
assets.go
Normal file
|
@ -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)
|
||||
}
|
60
common.go
Normal file
60
common.go
Normal file
|
@ -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)))
|
||||
}
|
43
router.go
Normal file
43
router.go
Normal file
|
@ -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
|
||||
}
|
12
server.go
Normal file
12
server.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//go:generate go-bindata assets templates
|
||||
|
||||
func init() {
|
||||
initialize()
|
||||
http.Handle("/", router())
|
||||
}
|
Loading…
Reference in a new issue