<commit_before>package main

import (
	"fmt"
	"io"
	"log"
	"mime"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/vbatts/go-httplog"
	"github.com/vbatts/imgsrv/assets"
	"github.com/vbatts/imgsrv/config"
	"github.com/vbatts/imgsrv/dbutil"
	"github.com/vbatts/imgsrv/hash"
	"github.com/vbatts/imgsrv/types"
	"github.com/vbatts/imgsrv/util"
	"labix.org/v2/mgo"
)

var (
	defaultPageLimit int = 25
	serverConfig     config.Config

	mongo_session *mgo.Session  // FIXME make this not global
	images_db     *mgo.Database // FIXME make this not global
	du            dbutil.Util
)

/*
Run as the file/image server
*/
func runServer(c *config.Config) {
	serverConfig = *c

	du = dbutil.Util{
		Seed:   serverConfig.MongoHost,
		User:   serverConfig.MongoUsername,
		Pass:   serverConfig.MongoPassword,
		DbName: serverConfig.MongoDbName,
	}
  err := du.Init()
  if err != nil {
    log.Fatal(err)
  }
	defer du.Close() // TODO this ought to catch a signal to cleanup

	http.HandleFunc("/", routeRoot)
	http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
		httplog.DefaultFavIcon.ServeHTTP(w, r)
	})
	http.HandleFunc("/assets/", routeAssets)
	http.HandleFunc("/upload", routeUpload)
	http.HandleFunc("/urlie", routeGetFromUrl)
	http.HandleFunc("/all", routeAll)
	http.HandleFunc("/f/", routeFiles)
	http.HandleFunc("/v/", routeViews)
	http.HandleFunc("/k/", routeKeywords)
	http.HandleFunc("/md5/", routeMD5s)
	http.HandleFunc("/ext/", routeExt)
	http.HandleFunc("/ip/", routeIPs)

	addr := fmt.Sprintf("%s:%s", c.Ip, c.Port)
	log.Printf("Serving on %s ...", addr)
	log.Fatal(http.ListenAndServe(addr, nil))
}

func initMongo() {
	mongo_session, err := mgo.Dial(serverConfig.MongoHost)
	if err != nil {
		log.Panic(err)
	}
	images_db = mongo_session.DB(serverConfig.MongoDbName)
	if len(serverConfig.MongoUsername) > 0 && len(serverConfig.MongoPassword) > 0 {
		err = images_db.Login(serverConfig.MongoUsername, serverConfig.MongoPassword)
		if err != nil {
			log.Panic(err)
		}
	}
	du.Gfs = images_db.GridFS("fs")
}

func serverErr(w http.ResponseWriter, r *http.Request, e error) {
	httplog.LogRequest(r, 503)
	log.Printf("Error: %s", e)
	w.WriteHeader(503)
	//ErrorPage(w, err)
	return
}

/* return a <a href/> for a given filename
   and root is the relavtive base of the explicit link.
*/
func linkToFile(root string, filename string) (html string) {
	return fmt.Sprintf("<a href='%s/f/%s'>%s</a>",
		root,<commit_msg>removing unused init<commit_after>package main

import (
	"fmt"
	"io"
	"log"
	"mime"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/vbatts/go-httplog"
	"github.com/vbatts/imgsrv/assets"
	"github.com/vbatts/imgsrv/config"
	"github.com/vbatts/imgsrv/dbutil"
	"github.com/vbatts/imgsrv/hash"
	"github.com/vbatts/imgsrv/types"
	"github.com/vbatts/imgsrv/util"
	"labix.org/v2/mgo"
)

var (
	defaultPageLimit int = 25
	serverConfig     config.Config

	mongo_session *mgo.Session  // FIXME make this not global
	images_db     *mgo.Database // FIXME make this not global
	du            dbutil.Util
)

/*
Run as the file/image server
*/
func runServer(c *config.Config) {
	serverConfig = *c

	du = dbutil.Util{
		Seed:   serverConfig.MongoHost,
		User:   serverConfig.MongoUsername,
		Pass:   <PASSWORD>Config.MongoPassword,
		DbName: serverConfig.MongoDbName,
	}

  err := du.Init()
  if err != nil {
    log.Fatal(err)
  }
	defer du.Close() // TODO this ought to catch a signal to cleanup

	http.HandleFunc("/", routeRoot)
	http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
		httplog.DefaultFavIcon.ServeHTTP(w, r)
	})
	http.HandleFunc("/assets/", routeAssets)
	http.HandleFunc("/upload", routeUpload)
	http.HandleFunc("/urlie", routeGetFromUrl)
	http.HandleFunc("/all", routeAll)
	http.HandleFunc("/f/", routeFiles)
	http.HandleFunc("/v/", routeViews)
	http.HandleFunc("/k/", routeKeywords)
	http.HandleFunc("/md5/", routeMD5s)
	http.HandleFunc("/ext/", routeExt)
	http.HandleFunc("/ip/", routeIPs)

	addr := fmt.Sprintf("%s:%s", c.Ip, c.Port)
	log.Printf("Serving on %s ...", addr)
	log.Fatal(http.ListenAndServe(addr, nil))
}

func serverErr(w http.ResponseWriter, r *http.Request, e error) {
	httplog.LogRequest(r, 503)
	log.Printf("Error: %s", e)
	w.WriteHeader(503)
	//ErrorPage(w, err)
	return
}

/* return a <a href/> for a given filename
   and root is the relavtive base of the explicit link.
*/
func linkToFile(root string, filename string) (html string) {
	return fmt.Sprintf("<a href='%s/f/%s'>%s</a>",
		root,
{'fraction_non_alphanumeric': 0.15812720848056538, 'fraction_numerical': 0.00530035335689046, 'mean_word_length': 3.998896247240618, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '849186', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1763, 'n_words': 352}