Skip to content

Commit

Permalink
omote/ura
Browse files Browse the repository at this point in the history
  • Loading branch information
ledyba committed Aug 14, 2018
1 parent 3e1fb47 commit 2b4720d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
}
Expand Down
64 changes: 35 additions & 29 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down

0 comments on commit 2b4720d

Please sign in to comment.