Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Apr 4, 2021
1 parent 6d64546 commit bd0d0a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
48 changes: 26 additions & 22 deletions server/routes.go → server/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ import (
"github.com/ije/rex"
)

func registerRoutes() {
startTime := time.Now()
httpClient := &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (conn net.Conn, err error) {
conn, err = net.DialTimeout(network, addr, 15*time.Second)
if err != nil {
return conn, err
}
var httpClient = &http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (conn net.Conn, err error) {
conn, err = net.DialTimeout(network, addr, 15*time.Second)
if err != nil {
return conn, err
}

// Set a one-time deadline for potential SSL handshaking
conn.SetDeadline(time.Now().Add(60 * time.Second))
return conn, nil
},
MaxIdleConnsPerHost: 5,
ResponseHeaderTimeout: 60 * time.Second,
// Set a one-time deadline for potential SSL handshaking
conn.SetDeadline(time.Now().Add(60 * time.Second))
return conn, nil
},
}
MaxIdleConnsPerHost: 5,
ResponseHeaderTimeout: 60 * time.Second,
},
}

// esm query middleware
func query() rex.Handle {
startTime := time.Now()
queue := newBuildQueue(runtime.NumCPU())

rex.Query("*", func(ctx *rex.Context) interface{} {
return func(ctx *rex.Context) interface{} {
pathname := ctx.Path.String()
switch pathname {
case "/":
Expand All @@ -53,7 +55,7 @@ func registerRoutes() {
return rex.Content("index.html", startTime, bytes.NewReader(html))
case "/favicon.ico":
// todo: add esm.sh logo
return 404
return rex.Err(404)
case "/_error.js":
switch ctx.Form.Value("type") {
case "resolve":
Expand Down Expand Up @@ -155,7 +157,7 @@ func registerRoutes() {
return err
}
if resp.StatusCode != 200 {
return http.StatusBadGateway
return rex.Err(http.StatusBadGateway)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -337,7 +339,7 @@ func registerRoutes() {
ctx.SetHeader("Cache-Control", "public, max-age=31536000, immutable")
return rex.File(fp)
}
return 404
return rex.Err(404)
}

buf := bytes.NewBuffer(nil)
Expand Down Expand Up @@ -395,7 +397,7 @@ func registerRoutes() {
ctx.SetHeader("Cache-Control", fmt.Sprintf("private, max-age=%d", refreshDuration))
ctx.SetHeader("Content-Type", "application/javascript; charset=utf-8")
return buf
})
}
}

func throwErrorJS(ctx *rex.Context, status int, err error) interface{} {
Expand All @@ -410,6 +412,8 @@ func throwErrorJS(ctx *rex.Context, status int, err error) interface{} {
fmt.Fprintf(buf, "export default null;\n")
ctx.SetHeader("Cache-Control", "private, no-store, no-cache, must-revalidate")
ctx.SetHeader("Content-Type", "application/javascript; charset=utf-8")
log.Error(err)
if status >= 500 {
log.Error(err)
}
return rex.Status(status, buf)
}
12 changes: 6 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,17 @@ func Serve(fs *embed.FS) {
rex.Header("Server", domain),
rex.Cors(rex.CORS{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST"},
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding", "Authorization"},
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding"},
MaxAge: 3600,
}),
query(),
)

registerRoutes()

C := rex.Serve(rex.ServerConfig{
Port: uint16(port),
TLS: rex.TLSConfig{
Port: uint16(httpsPort),
AutoRedirect: false,
Port: uint16(httpsPort),
AutoTLS: rex.AutoTLSConfig{
AcceptTOS: !isDev,
Hosts: []string{"www." + domain, domain},
Expand All @@ -202,6 +200,8 @@ func Serve(fs *embed.FS) {
case err = <-C:
log.Error(err)
}

// release resource
log.FlushBuffer()
accessLogger.FlushBuffer()
db.Close()
Expand Down

0 comments on commit bd0d0a2

Please sign in to comment.