Skip to content

Commit

Permalink
[noodle] Init local fs wraps http.Dir
Browse files Browse the repository at this point in the history
- using `os.Open` would work as well but it would have many 500 if user
has url like `index.html/is-not-a-folder`
  - it is fixed in golang/go#18984 there is
a function called `mapDirOpenError`
- [ ] TODO: serveFile would call `dirList` and it can't be disabled ...
  • Loading branch information
at15 committed Feb 11, 2018
1 parent 084c558 commit 617843c
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It has the following components:
- [Config](config) A YAML config reader with template support
- [Log](log) A Javaish logger for Go, application can control library and set level for different pkg via config or flag
- [Generator](generator) Render go template, generate methods for logger interface based on `gommon.yml`
- [Rice](rice) Embed static assets for web application with `.riceignore` support
- [Noodle](noodle) Embed static assets for web application with `.riceignore` support
- [Requests](requests) A pythonic wrapper for `net/http`, HTTP for Gopher.
- [Cast](cast) Convert Golang types
- [Data structure](structure) Bring Set etc. to Golang.
Expand Down Expand Up @@ -77,7 +77,7 @@ compared to packages it modeled after.
- [benbjohnson/tmpl](https://github.com/benbjohnson/tmpl) for go template generator
- first saw it in [influxdata/influxdb](https://github.com/influxdata/influxdb/blob/master/tsdb/engine/tsm1/encoding.gen.go.tmpl)
- we put template data in `gommon.yml`, so we don't need to pass it via cli
- [GeertJohan/go.rice](https://github.com/GeertJohan/go.rice) for rice
- [GeertJohan/go.rice](https://github.com/GeertJohan/go.rice) for ~~rice~~ noodle
- we implemented `.gitignore` like [feature](https://github.com/at15/go.rice/issues/1) but the upstream didn't respond for the [feature request #83](https://github.com/GeertJohan/go.rice/issues/83)

## About
Expand Down
29 changes: 29 additions & 0 deletions noodle/_examples/single-dir/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<title>I am title</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<p>If you being just staring at the sun light</p>
<table>
<tr>
<td>a</td>
<td>b</td>
</tr>
<tr>
<td>c</td>
<td>d</td>
</tr>
</table>
<p>echarts example
<a target="_blank"
href="https://ecomfe.github.io/echarts-doc/public/en/tutorial.html#Get%20Started%20with%20ECharts%20in%205%20minutes">get
started</a>
</p>
<div id="main" style="width: 600px;height:400px;"></div>
<p>image from unsplash</p>
<img src="https://source.unsplash.com/random" alt="unsplash random">
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.0.2/echarts-en.common.min.js"></script>
<script src="main.js"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions noodle/_examples/single-dir/assets/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background-color: lightcyan;
margin: 20px;
}
30 changes: 30 additions & 0 deletions noodle/_examples/single-dir/assets/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

// alert('this is main');

console.log({'o': 'bject'});

var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
30 changes: 30 additions & 0 deletions noodle/_examples/single-dir/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"net/http"
"fmt"
"os"
"github.com/dyweb/gommon/noodle"
)

func main() {
fs := "default"
if len(os.Args) > 1 {
fs = os.Args[1]
}
fmt.Println(fs)
addr := ":8080"
//addr := ":6667" # https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers 6665-6669 is for IRC ...
fmt.Printf("listen on %s\n", addr)
var root http.FileSystem
switch fs {
case "default":
root = http.Dir("./assets")
case "local":
root = noodle.NewLocal("./assets")
default:
panic("unknown fs")
}
http.ListenAndServe(addr, http.FileServer(root))

}
15 changes: 15 additions & 0 deletions noodle/doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Noodle

## TODO

- generate go file based on assets (like most bind-data ish library does)
- figure out how to create and append zipfile, without relying on external zip binary

## Ref

- https://github.com/GeertJohan/go.rice 1.6k star, used to use it for Ayi
- https://github.com/shurcooL/vfsgen 236 star listed most packages in alternative section
- https://github.com/shurcooL/vfsgen#alternatives
- https://www.ueber.net/who/mjl/blog/p/assets-in-go-binaries/ describes how to append zip
- https://github.com/cookieo9/resources-go pretty like go.rice
- [golang-nuts/including html contents of a file at compile time](https://groups.google.com/forum/#!topic/golang-nuts/9QUjmDED96E)
24 changes: 24 additions & 0 deletions noodle/fs_local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package noodle

import (
"net/http"
)

var _ http.FileSystem = (*LocalFs)(nil)

type LocalFs struct {
root string
dir http.Dir
}

func NewLocal(root string) *LocalFs {
return &LocalFs{root: root, dir: http.Dir(root)}
}

func (fs *LocalFs) Open(name string) (http.File, error) {
//return os.Open(filepath.Join(fs.root, name))
// NOTE: http.Dir has extra error handling, https://github.com/golang/go/issues/18984
// some operation are mapped to 404 instead of 500
// TODO: but it supports list dir and can't be disabled ...
return fs.dir.Open(name)
}
3 changes: 3 additions & 0 deletions noodle/fs_multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package noodle

// A file system that fall back to another if file not found
1 change: 1 addition & 0 deletions noodle/fs_zip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package noodle
3 changes: 3 additions & 0 deletions noodle/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package noodle

// generate assets file with flag
6 changes: 6 additions & 0 deletions noodle/pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package noodle

import "github.com/dyweb/gommon/util/logutil"

var log = logutil.NewPackageLogger()

1 change: 1 addition & 0 deletions noodle/zip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package noodle
2 changes: 2 additions & 0 deletions requests/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/pkg/errors"
)

// TODO: it is already in net/http https://golang.org/pkg/net/http/#Request.BasicAuth

const AuthorizationHeader = "Authorization"

func GenerateBasicAuth(username string, password string) string {
Expand Down

0 comments on commit 617843c

Please sign in to comment.