-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 56216c8
Showing
9 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
.idea/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Kenta Iwasaki <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,5 @@ | ||
# flatend | ||
|
||
[![MIT License](https://img.shields.io/apm/l/atomic-design-ui.svg?)](LICENSE) | ||
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/lithdew/flatend) | ||
[![Discord Chat](https://img.shields.io/discord/697002823123992617)](https://discord.gg/58dJzS) |
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,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/lithdew/flatend" | ||
flag "github.com/spf13/pflag" | ||
"github.com/valyala/fasthttp/reuseport" | ||
"net" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
) | ||
|
||
const program = ` | ||
http_get("hello_world") | ||
` | ||
|
||
func check(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
var ( | ||
bindHost string | ||
bindPort uint16 | ||
) | ||
|
||
func main() { | ||
flag.StringVarP(&bindHost, "host", "h", "", "binding host") | ||
flag.Uint16VarP(&bindPort, "port", "p", 0, "binding port") | ||
|
||
flag.Parse() | ||
|
||
addr := net.JoinHostPort(bindHost, strconv.FormatUint(uint64(bindPort), 10)) | ||
|
||
fmt.Printf("Bind address: %q\n", addr) | ||
|
||
ln, err := reuseport.Listen("tcp4", addr) | ||
check(err) | ||
|
||
_, err = flatend.LoadConfig(program) | ||
check(err) | ||
|
||
srv := flatend.NewHTTP() | ||
check(srv.Listen(ln)) | ||
|
||
defer func() { | ||
check(srv.Close()) | ||
}() | ||
|
||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, os.Interrupt) | ||
<-ch | ||
|
||
println() | ||
} |
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,43 @@ | ||
package flatend | ||
|
||
import ( | ||
"fmt" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
type Config struct { | ||
main *starlark.Thread | ||
vars starlark.StringDict | ||
handlers map[string]func() | ||
|
||
program *starlark.Program | ||
} | ||
|
||
func (c *Config) declareBuiltins() { | ||
c.vars["sql"] = starlark.NewBuiltin("sql", SQL) | ||
c.vars["http_get"] = starlark.NewBuiltin("http_get", GET) | ||
} | ||
|
||
func LoadConfig(src interface{}) (*Config, error) { | ||
cfg := &Config{ | ||
main: &starlark.Thread{Name: "main"}, | ||
vars: make(starlark.StringDict), | ||
handlers: make(map[string]func()), | ||
} | ||
|
||
cfg.declareBuiltins() | ||
|
||
_, program, err := starlark.SourceProgram("", src, cfg.vars.Has) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse config: %w", err) | ||
} | ||
|
||
cfg.program = program | ||
|
||
_, err = program.Init(cfg.main, cfg.vars) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to init config: %w", err) | ||
} | ||
|
||
return cfg, nil | ||
} |
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,9 @@ | ||
module github.com/lithdew/flatend | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/spf13/pflag v1.0.5 | ||
github.com/valyala/fasthttp v1.9.0 | ||
go.starlark.net v0.0.0-20200330013621-be5394c419b6 | ||
) |
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,22 @@ | ||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= | ||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= | ||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= | ||
github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs= | ||
github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= | ||
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w= | ||
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasthttp v1.9.0 h1:hNpmUdy/+ZXYpGy0OBfm7K0UQTzb73W0T0U4iJIVrMw= | ||
github.com/valyala/fasthttp v1.9.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w= | ||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc= | ||
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= | ||
go.starlark.net v0.0.0-20200330013621-be5394c419b6 h1:S2s+dYPyDg/vF7KbcRIB2831xVimJoR4zebfoVBzn7Q= | ||
go.starlark.net v0.0.0-20200330013621-be5394c419b6/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
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,26 @@ | ||
package flatend | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
func SQL(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
return nil, nil | ||
} | ||
|
||
func GET(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
if len(args) == 0 { | ||
return nil, errors.New("http_get: route path must be specified") | ||
} | ||
|
||
path, ok := starlark.AsString(args[0]) | ||
if !ok { | ||
return nil, errors.New("http_get: route path must be a string") | ||
} | ||
|
||
fmt.Printf("Registering HTTP GET route: %q\n", path) | ||
|
||
return starlark.None, nil | ||
} |
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,82 @@ | ||
package flatend | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/valyala/fasthttp" | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type HTTP struct { | ||
srv *fasthttp.Server | ||
|
||
wg sync.WaitGroup | ||
ch chan error | ||
|
||
mu sync.RWMutex | ||
routes map[string]func() | ||
} | ||
|
||
func NewHTTP() *HTTP { | ||
s := &HTTP{ch: make(chan error, 1)} | ||
return s | ||
} | ||
|
||
func (s *HTTP) Load(cfg *Config) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
s.routes = make(map[string]func()) | ||
|
||
for route, fn := range cfg.handlers { | ||
s.routes[route] = fn | ||
} | ||
} | ||
|
||
func (s *HTTP) Listen(ln net.Listener) error { | ||
if ln == nil { | ||
return errors.New("net.Listener must not be nil") | ||
} | ||
|
||
s.wg.Add(1) | ||
|
||
s.srv = &fasthttp.Server{ | ||
Name: "flatend", | ||
Handler: func(ctx *fasthttp.RequestCtx) { | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
|
||
handler, exists := s.routes[string(ctx.Path())] | ||
if !exists { | ||
return | ||
} | ||
|
||
handler() | ||
}, | ||
ReadTimeout: 5 * time.Second, | ||
WriteTimeout: 3 * time.Second, | ||
} | ||
|
||
go func() { | ||
defer s.wg.Done() | ||
s.ch <- s.srv.Serve(ln) | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (s *HTTP) Close() error { | ||
if err := s.srv.Shutdown(); err != nil { | ||
return fmt.Errorf("failed to shutdown http server: %w", err) | ||
} | ||
|
||
s.wg.Wait() | ||
|
||
if err := <-s.ch; err != nil { | ||
return fmt.Errorf("failed to close http server listener: %w", err) | ||
} | ||
|
||
return nil | ||
} |