From 2b4720de1fa9a6ed9c27c22ea72602f0c5fc310b Mon Sep 17 00:00:00 2001 From: psi Date: Wed, 15 Aug 2018 08:08:16 +0900 Subject: [PATCH] omote/ura --- README.md | 19 ++++++++------- main.go | 5 ++-- web/server.go | 64 ++++++++++++++++++++++++++++----------------------- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 622b8c7..a95a180 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,18 @@ #### ServerSide ```bash -# for logging +echo 'for logging' go get -u "github.com/Sirupsen/logrus" go get -u "github.com/fatih/color" -# networking +echo 'networking' go get -u "golang.org/x/net/context" go get -u "github.com/julienschmidt/httprouter" -# data serialization +echo 'data serialization' go get -u "gopkg.in/yaml.v2" -# imaging +echo 'imaging' go get -u "github.com/rwcarlsen/goexif" go get -u "github.com/disintegration/imaging" go get -u "github.com/nfnt/resize" @@ -30,19 +30,22 @@ go get -u "github.com/nfnt/resize" ```bash npm install -# to upgrade, +echo 'to upgrade,' -npm run update # update package.json -npm update # update package-lock.json +npm run update +npm update ``` ## Iterative and incremental development ```bash npm run watch -open http://localhost:8080/ ``` +then, + +open [http://localhost:8080/](http://localhost:8080/)(index) or [http://localhost:8081/](http://localhost:8081/)(admin) + ## Build ```bash diff --git a/main.go b/main.go index 30790c7..59db1fc 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,8 @@ import ( //go:generate bash geninfo.sh -var addr = flag.String("listen", ":8080", "listen") +var listenOmote = flag.String("listen-omote", ":8080", "omote listen") +var listenUra = flag.String("listen-ura", ":8081", "ura listen") var shelfPath = flag.String("shelf", "_shelf", "shelf path") var cachePath = flag.String("cache", "_cache", "cache path") @@ -71,7 +72,7 @@ func main() { } log.Infof("%d entities, %d moments", shelf.NumEntities(), shelf.NumMoments()) - server = web.NewServer(*addr, shelf, *cachePath) + server = web.NewServer(*listenOmote, *listenUra, shelf, *cachePath) if err := server.Prepare(); err != nil { log.Fatalf("Failed to prepare server: %v", err) } diff --git a/web/server.go b/web/server.go index 5fab730..67e430e 100644 --- a/web/server.go +++ b/web/server.go @@ -21,8 +21,10 @@ const ( ) type Server struct { - impl *http.Server - router *httprouter.Router + omoteImpl *http.Server + uraImpl *http.Server + omoteRouter *httprouter.Router + uraRouter *httprouter.Router shelf *shelf.Shelf entityCache *cache.EntityCacheShelf momentCache *cache.MomentCacheShelf @@ -32,41 +34,45 @@ func log() *logrus.Entry { return logrus.WithField("Module", "Web") } -func NewServer(addr string, shelf *shelf.Shelf, cachePath string) *Server { +func NewServer(listenOmote, listenUra string, shelf *shelf.Shelf, cachePath string) *Server { srv := &Server{ - router: httprouter.New(), + omoteRouter: httprouter.New(), + uraRouter: httprouter.New(), shelf: shelf, entityCache: cache.NewEntityCacheShelf(shelf, filepath.Join(cachePath, "entity")), momentCache: cache.NewMomentCacheShelf(shelf), } - srv.impl = &http.Server{ - Addr: addr, - Handler: srv.router, + srv.omoteImpl = &http.Server{ + Addr: listenOmote, + Handler: srv.omoteRouter, + } + srv.uraImpl = &http.Server{ + Addr: listenUra, + Handler: srv.uraRouter, } srv.setupRoute() return srv } func (srv *Server) setupRoute() { - router := srv.router - router.GET("/", srv.serveIndex) - router.GET("/about-us/", srv.serveIndex) - router.GET("/entity/:id", srv.serveEntity) - router.GET("/entity/:id/icon", srv.serveEntityIcon) - router.GET("/entity/:id/medium", srv.serveEntityMedium) - router.GET("/moment/*moment", srv.serveMoment) - - router.GET("/editor/", srv.serveAdminIndex) - router.GET("/editor/new", srv.serveAdminNew) - router.POST("/editor/upload", srv.serveAdminUpload) - - router.GET("/editor/edit/:id", srv.serveAdminEdit) - - router.POST("/editor/edit/preview", srv.serveAdminEditPreview) - - router.ServeFiles("/static/*filepath", http.Dir(StaticPath)) - - router.NotFound = srv + omote := srv.omoteRouter + omote.GET("/", srv.serveIndex) + omote.GET("/about-us/", srv.serveIndex) + omote.GET("/entity/:id", srv.serveEntity) + omote.GET("/entity/:id/icon", srv.serveEntityIcon) + omote.GET("/entity/:id/medium", srv.serveEntityMedium) + omote.GET("/moment/*moment", srv.serveMoment) + omote.ServeFiles("/static/*filepath", http.Dir(StaticPath)) + omote.NotFound = srv + + ura := srv.uraRouter + ura.GET("/", srv.serveAdminIndex) + ura.GET("/new", srv.serveAdminNew) + ura.POST("/upload", srv.serveAdminUpload) + ura.POST("/preview", srv.serveAdminEditPreview) + ura.ServeFiles("/static/*filepath", http.Dir(StaticPath)) + + ura.NotFound = srv } func (srv *Server) Prepare() error { @@ -102,8 +108,8 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (srv *Server) Start() error { - log().Infof("Start at %s", srv.impl.Addr) - err := srv.impl.ListenAndServe() + log().Infof("Start Omote Server at %s", srv.omoteImpl.Addr) + err := srv.omoteImpl.ListenAndServe() if err == http.ErrServerClosed { err = nil } @@ -112,7 +118,7 @@ func (srv *Server) Start() error { func (srv *Server) Stop() error { ctx, _ := context.WithTimeout(context.Background(), time.Second*5) - return srv.impl.Shutdown(ctx) + return srv.omoteImpl.Shutdown(ctx) } func (srv *Server) setError(w http.ResponseWriter, r *http.Request, err error) {