aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xmain.go44
1 files changed, 26 insertions, 18 deletions
diff --git a/main.go b/main.go
index 29eed83..16c5ca6 100755
--- a/main.go
+++ b/main.go
@@ -1,6 +1,6 @@
1/* 1/*
2 2
3 This file is part of StaticWeb (https://github.com/Pacien/StaticWeb). 3 This file is part of StaticWeb <https://github.com/Pacien/StaticWeb>.
4 4
5 StaticWeb is free software: you can redistribute it and/or modify 5 StaticWeb is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by 6 it under the terms of the GNU Affero General Public License as published by
@@ -21,6 +21,7 @@ package main
21 21
22import ( 22import (
23 "flag" 23 "flag"
24 "fmt"
24 "log" 25 "log"
25 "net/http" 26 "net/http"
26 "os" 27 "os"
@@ -32,26 +33,31 @@ var params struct {
32} 33}
33 34
34func defaultHandler(w http.ResponseWriter, r *http.Request) { 35func defaultHandler(w http.ResponseWriter, r *http.Request) {
35 host := strings.Split(*&r.Host, ":") 36 host := strings.Split(r.Host, ":")
37
36 if host[0] == "" { 38 if host[0] == "" {
37 log.Println("No host") 39 log.Println("Undefined host")
38 http.Error(w, "404 page not found", http.StatusNotFound) 40 http.NotFound(w, r)
39 return 41 return
40 } 42 }
43
41 request := r.URL.Path[1:] 44 request := r.URL.Path[1:]
42 requestedFile := *&params.dir + "/" + *&host[0] + "/" + *&request 45 requestedFile := params.dir + "/" + host[0] + "/" + request
43 log.Println(requestedFile) 46 log.Println(requestedFile)
47
44 file, err := os.Stat(requestedFile) 48 file, err := os.Stat(requestedFile)
45 if err != nil { 49 if err != nil {
46 log.Println(err) 50 log.Println(err)
47 http.Error(w, "404 page not found", http.StatusNotFound) 51 http.NotFound(w, r)
48 return 52 return
49 } 53 }
54
50 if file.IsDir() && !strings.HasSuffix(requestedFile, "/") { 55 if file.IsDir() && !strings.HasSuffix(requestedFile, "/") {
51 http.Redirect(w, r, r.URL.Path+"/", http.StatusFound) 56 http.Redirect(w, r, r.URL.Path+"/", http.StatusFound)
52 return 57 return
53 } 58 }
54 http.ServeFile(w, r, *&requestedFile) 59
60 http.ServeFile(w, r, requestedFile)
55} 61}
56 62
57func init() { 63func init() {
@@ -63,29 +69,31 @@ func init() {
63} 69}
64 70
65func main() { 71func main() {
72 fmt.Println("StaticWeb <https://github.com/Pacien/StaticWeb>")
73
66 if params.log != "" { 74 if params.log != "" {
67 logFile, err := os.OpenFile(*&params.log, os.O_WRONLY, 0666) 75 logFile, err := os.OpenFile(params.log, os.O_WRONLY, 0666)
68 if os.IsNotExist(*&err) { 76 if os.IsNotExist(err) {
69 log.Println("Log file not found, creating a new log file:", *&err) 77 log.Println("Log file not found, creating a new log file:", err)
70 logFile, err = os.Create(*&params.log) 78 logFile, err = os.Create(params.log)
71 if err != nil { 79 if err != nil {
72 log.Println("Cannot create log file:", *&err) 80 log.Println("Cannot create log file:", err)
73 return 81 return
74 } 82 }
75 } else if *&err != nil { 83 } else if err != nil {
76 log.Println("Cannot open log file:", *&err) 84 log.Println("Cannot open log file:", err)
77 return 85 return
78 } 86 }
79 defer logFile.Close() 87 defer logFile.Close()
80 log.SetOutput(*&logFile) 88 log.SetOutput(logFile)
81 } 89 }
82 90
83 log.Println("Starting StaticWeb on " + *&params.addr + ":" + *&params.port) 91 log.Println("Listening on " + params.addr + ":" + params.port)
84 92
85 http.HandleFunc("/", defaultHandler) 93 http.HandleFunc("/", defaultHandler)
86 err := http.ListenAndServe(*&params.addr+":"+*&params.port, nil) 94 err := http.ListenAndServe(params.addr+":"+params.port, nil)
87 if err != nil { 95 if err != nil {
88 log.Println(*&err) 96 log.Println(err)
89 return 97 return
90 } 98 }
91} 99}