1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-16 07:56:24 -07:00
gochan/src/server.go

134 lines
3.4 KiB
Go
Raw Normal View History

2013-02-02 15:01:01 -08:00
package main
import (
"fmt"
"io/ioutil"
2013-02-02 15:01:01 -08:00
"net"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
2013-02-02 15:01:01 -08:00
)
var (
form url.Values
header http.Header
cookies []*http.Cookie
writer http.ResponseWriter
request http.Request
exit_error bool
2013-02-02 15:01:01 -08:00
)
func initServer() {
if config.Port == 0 {
config.Port = 80
2013-02-02 15:01:01 -08:00
}
listener,err := net.Listen("tcp", config.Domain+":"+strconv.Itoa(config.Port))
2013-02-02 15:01:01 -08:00
if(err != nil) {
error_log.Write(err.Error())
fmt.Printf("Failed listening on "+config.Domain+":%d, see log for details",config.Port)
2013-02-02 15:01:01 -08:00
os.Exit(2)
}
http.Handle("/", makeHandler(fileHandle))
http.Handle("/manage",makeHandler(callManageFunction))
http.Handle("/post",makeHandler(makePost))
//http.Handle("/util",makeHandler(utilHandler))
2013-02-02 15:01:01 -08:00
http.Serve(listener, nil)
}
func fileHandle(w http.ResponseWriter, r *http.Request) {
request = *r
writer = w
cookies = request.Cookies()
request.ParseForm()
form = request.Form
request_url := request.URL.Path
filepath := path.Join(config.DocumentRoot, request_url)
results,err := os.Stat(filepath)
restricted := false // if true, user doesn't have permission to view the file, because read-banned, etc
2013-02-02 15:01:01 -08:00
if err == nil {
//the file exists, or there is a folder here
if results.IsDir() {
found_index := false
newpath := ""
//check to see if one of the specified index pages exists
for i := 0; i < len(config.FirstPage); i++ {
newpath = path.Join(filepath,config.FirstPage[i])
_,err := os.Stat(newpath)
if err == nil {
serveFile(w, newpath)
found_index = true
break
2013-02-02 15:01:01 -08:00
}
}
if !found_index {
error404()
}
2013-02-02 15:01:01 -08:00
} else {
//the file exists, and is not a folder
//writer.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", 500))
if request_url == path.Join(config.SiteWebfolder+"javascript/manage.js") {
if getStaffRank() == 0 {
// we aren't logged in and tried to access manage.js
restricted = true
}
}
if !restricted {
serveFile(w, filepath)
}
2013-02-02 15:01:01 -08:00
}
} else {
//there is nothing at the requested address
error404()
2013-02-02 15:01:01 -08:00
}
}
func makeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
//defer serverError()
if !exit_error {
fn(w, r)
exit_error = false
} else {
exit_error = false
}
2013-02-02 15:01:01 -08:00
}
}
func exitWithErrorPage(w http.ResponseWriter, err string) {
error_page_bytes,_ := ioutil.ReadFile("templates/error.html")
error_page := string(error_page_bytes)
error_page = strings.Replace(error_page,"{ERRORTEXT}", err,-1)
fmt.Fprintf(w,error_page)
exit_error = true
}
func redirect(location string) {
http.Redirect(writer,&request,location,http.StatusFound)
}
func error404() {
http.ServeFile(writer, &request, path.Join(config.DocumentRoot, "/error/404.html"))
error_log.Write("Error: 404 Not Found from " + request.RemoteAddr + " @ " + request.RequestURI)
}
func serverError() {
if _, ok := recover().(error); ok {
//something went wrong, now we need to throw a 500
http.ServeFile(writer,&request, path.Join(config.DocumentRoot, "/error/500.html"))
error_log.Write("Error: 500 Internal Server error from " + request.RemoteAddr + " @ " + request.RequestURI)
return
2013-02-02 15:01:01 -08:00
}
}
func serveFile(w http.ResponseWriter, filepath string) {
http.ServeFile(w, &request, filepath)
access_log.Write("Success: 200 from " + request.RemoteAddr + " @ " + request.RequestURI)
2013-06-04 18:04:56 -07:00
}