Skip to content

Commit

Permalink
Dev (#11)
Browse files Browse the repository at this point in the history
* feat:generate certificate using yaml

* refactor:package name

* fix:template bind

* feat:add home template

* refactor:package name
  • Loading branch information
nsavinda authored Jun 20, 2024
1 parent 345ec6f commit 170142d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Elixir-Craft/serveIt
module github.com/Elixir-Craft/servefiles

go 1.22.3

Expand Down
26 changes: 23 additions & 3 deletions serveIt/main.go → servefiles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"path/filepath"
"strings"

"github.com/Elixir-Craft/serveIt/localip"
"github.com/Elixir-Craft/servefiles/localip"

"github.com/Elixir-Craft/serveIt/certgen"
"github.com/Elixir-Craft/servefiles/certgen"

"github.com/Elixir-Craft/serveIt/webtemplates"
"github.com/Elixir-Craft/servefiles/webtemplates"
)

var (
Expand All @@ -25,6 +25,12 @@ var (
IndexTemplate = template.Must(template.New("index.html").Parse(webtemplates.Index))
AuthTemplate = template.Must(template.New("").Parse(webtemplates.Auth))

CertFilePath = "cert/cert.pem"
KeyFilePath = "cert/key.pem"
HomeTemplate = template.Must(template.New("home.html").Parse(webtemplates.Home))
IndexTemplate = template.Must(template.New("index.html").Parse(webtemplates.Index))
AuthTemplate = template.Must(template.New("").Parse(webtemplates.Auth))

)

type FileInfo struct {
Expand Down Expand Up @@ -57,6 +63,11 @@ func httpRequestHandler(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

err := HomeTemplate.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

}

func fileHandler(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -100,6 +111,8 @@ func fileHandler(w http.ResponseWriter, req *http.Request) {

err = IndexTemplate.ExecuteTemplate(w, "index.html", data)

err = IndexTemplate.ExecuteTemplate(w, "index.html", data)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down Expand Up @@ -153,13 +166,17 @@ func fileSize(size int64) string {
func passwordProtected(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Check for a session or a simple cookie to verify if the password was entered correctly
if cookie, err := r.Cookie("password"); err != nil || !checkPassword(cookie.Value) {
if cookie, err := r.Cookie("password"); err != nil || !checkPassword(cookie.Value) {
if r.Method == "POST" && checkPassword(r.FormValue("password")) {
// Set a simple cookie for demonstration purposes (not secure for production)
http.SetCookie(w, &http.Cookie{
Name: "password",
Value: r.FormValue("password"),

Name: "password",
Value: r.FormValue("password"),

Path: "/",
MaxAge: 300, // Expires after 300 seconds
})
Expand All @@ -177,12 +194,15 @@ func servePasswordPrompt(w http.ResponseWriter, r *http.Request) {

// read the html file

err := AuthTemplate.Execute(w, nil)
err := AuthTemplate.Execute(w, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError)
}



}

func checkPassword(enteredPassword string) bool {
Expand Down

0 comments on commit 170142d

Please sign in to comment.