-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[app][web] Use go.ice for ayi web static #15
- 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
Showing
9 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
loggers: | ||
- struct: "*App" | ||
receiver: "a" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters