Skip to content

Commit

Permalink
[app][web] Use go.ice for ayi web static #15
Browse files Browse the repository at this point in the history
- the answer for go web framework #15 is I wrote a thin wrapper around
net/http https://github.com/at15/go.ice
- now there are http access log for static files
- `ayi web static` works, can specify port but can't specify dir
  • Loading branch information
at15 committed Apr 11, 2018
1 parent e60d5b3 commit 490b6ad
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 5 deletions.
20 changes: 19 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ generate:

.PHONY: package
package:
rm -f ayi-v$(VERSION)-linux-amd64.zip
go build -ldflags "$(FLAGS)" -o ayi-v$(VERSION)-linux-amd64 ./cmd/ayi
zip ayi-v$(VERSION)-linux-amd64.zip ayi-v$(VERSION)-linux-amd64
rm ayi-v$(VERSION)-linux-amd64
rm ayi-v$(VERSION)-linux-amd64
2 changes: 1 addition & 1 deletion app/git/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewApp() (*App, error) {
root := &cobra.Command{
Use: "git",
Short: "git helper",
Long: "git helper",
Long: "git helper long",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion app/git/gommon_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions app/web/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package web

import (
"fmt"
"net/http"
"os"

iconfig "github.com/at15/go.ice/ice/config"
ihttp "github.com/at15/go.ice/ice/transport/http"
dlog "github.com/dyweb/gommon/log"
"github.com/spf13/cobra"
)

type App struct {
root *cobra.Command

log *dlog.Logger
}

func NewApp() (*App, error) {
a := &App{}
dlog.NewStructLogger(log, a)
root := &cobra.Command{
Use: "web",
Short: "web helper",
Long: "web helper long",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(1)
},
}
var port int
root.PersistentFlags().IntVar(&port, "port", 3000, "port listen to")
static := &cobra.Command{
Use: "static",
Short: "serve static content",
Long: "Serve static content",
Run: func(cmd *cobra.Command, args []string) {
// TODO: go.ice could have a static http server to be used out of box
cfg := iconfig.HttpServerConfig{
Addr: fmt.Sprintf(":%d", port),
EnableTracing: false,
}
h := http.FileServer(http.Dir("."))
srv, err := ihttp.NewServer(cfg, h, nil)
if err != nil {
a.log.Fatal("failed to create http server")
return
}
if err := srv.Run(); err != nil {
a.log.Fatal(err)
return
}
},
}
root.AddCommand(static)
a.root = root
return a, nil
}

func (a *App) CobraCommand() *cobra.Command {
return a.root
}
3 changes: 3 additions & 0 deletions app/web/gommon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
loggers:
- struct: "*App"
receiver: "a"
17 changes: 17 additions & 0 deletions app/web/gommon_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions app/web/pkg.go
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
package web

import (
"github.com/dyweb/Ayi"
"github.com/dyweb/Ayi/util/logutil"
)

const appName = "web"

var log = logutil.NewPackageLogger()

func init() {
Ayi.RegisterAppFactory(appName, func() (Ayi.App, error) {
return NewApp()
})
}
5 changes: 4 additions & 1 deletion cmd/ayi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
icli "github.com/at15/go.ice/ice/cli"

"github.com/dyweb/Ayi"
_ "github.com/dyweb/Ayi/app/git"
"github.com/dyweb/Ayi/util/logutil"

// apps
_ "github.com/dyweb/Ayi/app/git"
_ "github.com/dyweb/Ayi/app/web"
)

const (
Expand Down

0 comments on commit 490b6ad

Please sign in to comment.