TalkingJson
TalkingJson

Reputation: 101

Golang htaccess configure without Nginx or Apache

I've created web app and analyzed it with Google site analyzer.

In most cases I need to configure htaccess file. As I understand this file can be used only on Nginx or Apache server, but I don't want to use any of these.

I want to configure htaccess only with golang tools. Currently my app running on VPS server.

Upvotes: 1

Views: 1768

Answers (3)

Carson
Carson

Reputation: 8098

If you want to do some constraints, then you may reference the following.

package main

import (
    "github.com/gorilla/mux"
    "io/fs"
    "net/http"
    "path/filepath"
)

type TxtFileSystem struct {
    http.FileSystem
}

func (txtFS TxtFileSystem) Open(path string) (http.File, error) {
    if filepath.Ext(path) != ".txt" {
        return nil, &fs.PathError{Op: "open", Path: path, Err: fs.ErrNotExist}
    }
    return txtFS.FileSystem.Open(path)
}

func main() {
    m := mux.NewRouter()
    m.PathPrefix("/doc/").Handler(http.FileServer(TxtFileSystem{http.Dir("./doc")}))
}

That will only allow you to visit the file extension is .txt

Upvotes: 0

Scott Smith
Scott Smith

Reputation: 1020

This project allows you to support the http auth standard with GO, zero apache code.

You can even use a password file created with the Apache htpasswd (bad) or htdigest (good) commands:

https://github.com/abbot/go-http-auth

Upvotes: 2

Zippo
Zippo

Reputation: 16430

You don't need .htaccess as it's only meant for Apache:

http://httpd.apache.org/docs/2.2/howto/htaccess.html

If you use Apache, external services like Google Site Analyzer can't see .htaccess since it's not served by Apache. It's kept private.

Everything Apache can do with .htaccess, Go can do with net/http or with a 3rd package like Gorilla to help.

Upvotes: 1

Related Questions