Skip to content

Easily to start locally #35

@suntong

Description

@suntong

Solving #23's problem slightly differently -- since algnhsa wraps normal go http server to use with as lambda, it'd be better to allow people to start their normal go http server locally without the Lambda adapter / wrapper.

The idea is borrowed from here --
https://blog.carlmjohnson.net/post/2020/how-to-host-golang-on-netlify-for-free/

func main() {
    port := flag.Int("port", -1, "specify a port to use http rather than AWS Lambda")
    flag.Parse()
    listener := gateway.ListenAndServe
    portStr := "n/a"
    if *port != -1 {
        portStr = fmt.Sprintf(":%d", *port)
        listener = http.ListenAndServe
        http.Handle("/", http.FileServer(http.Dir("./public")))
    }

    http.Handle("/api/feed", feed2json.Handler(
        feed2json.StaticURLInjector("https://news.ycombinator.com/rss"), nil, nil, nil))
    log.Fatal(listener(portStr, nil))
}

With this version of the code, if we run go run main.go -port 8000, the server will start on http://localhost:8000, but if we omit a port, it will start in AWS Lambda mode. It also runs a file server in HTTP mode, so we can preview the static files that we’re working on in our browser as we’re developing.

I realized that doing this way will have API changes for algnhsa, but maybe we can use a different function name or something (say algnhsa.CompatibleListenAndServe), as this seamless switching feature is really neat IMHO.

Please consider. Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions