大猩猩mux不包括扩展请求

时间:2017-01-28 13:23:04

标签: go gorilla

尝试使用 func main() { r := mux.NewRouter() r.HandleFunc("/{path:^.*([!js|jpg|png|gif])$}", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "dist/index.html") }) r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("dist")))) http.Handle("/", r) http.ListenAndServe(":8000", nil) } 配置转发服务器路由,以使用index.html响应所有请求,但排除扩展名为.jpg | .js | .png

的请求

由于扩展而排除的静态文件将路由到FileServer。配置。

尝试失败

InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fci);
    getWindow().setBackgroundDrawable(null);

    final ImageButton btn_pas   = (ImageButton) findViewById(R.id.btn_pas);
    Glide.with(getApplicationContext()).load(R.drawable.fci_2).placeholder(R.color.white).into(btn_pas);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-...");

    requestNewInterstitial();
}

@Override
public void onBackPressed() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
        super.onBackPressed();
    } else {
        super.onBackPressed();
    }
    requestNewInterstitial();
}

private final void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}

public final void pas(View view) {
    Intent intent = new Intent(Activity.this, pas.class);
    startActivity(intent);
}

2 个答案:

答案 0 :(得分:2)

更好的接近欢迎,希望使用正则表达式,这样的东西保持完整,没有疯狂的if / else条件

func main() {
    r := mux.NewRouter()

    r.HandleFunc("/{path:.*}", func(w http.ResponseWriter, r *http.Request) {
        if HasSuffix(r.URL.Path, []string{"js", "css", "gif", "jpeg", "woff2", "woff", "ttf"}) == false {
            fmt.Println("serving index")
            http.ServeFile(w, r, "dist/index.html")
        } else {
            http.ServeFile(w, r, "dist/"+r.URL.Path)
        }
    })

    //r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("dist"))))

    http.Handle("/", r)
    http.ListenAndServe(":8000", nil)
}

//HasSuffix check if url has suffix
func HasSuffix(path string, parts []string) bool {
    for _, part := range parts {
        fmt.Println("checking if part:" + part + " exists in path:" + path)
        if strings.HasSuffix(path, part) == true {
            return true
        }
    }
    return false
}

答案 1 :(得分:1)

对于没有.jpg | .js | .png

的匹配字符串,此"^.*([!js|jpg|png|gif])$}"不是有效的常规尝试

但是,出于技术原因,不支持golang Negative lookahead ,特别是因为它与库的O(n)-time保证冲突。

我建议你以其他方式去做,即为png,js,css文件等添加处理程序以便为这些文件提供服务