Skip to content

Instantly share code, notes, and snippets.

@mio991
Last active July 10, 2016 14:27
Show Gist options
  • Save mio991/6e59bc14a9f127283bbfba934000a860 to your computer and use it in GitHub Desktop.
Save mio991/6e59bc14a9f127283bbfba934000a860 to your computer and use it in GitHub Desktop.
package main
import(
"encoding/json"
"path/filepath"
"log"
"os"
"time"
"net/http"
"fmt"
)
func main() {
var err error
var conf *Config
var data *Data
conf = OpenOrCreateConfig()
data = OpenOrCreateData(conf.DataFile)
var server *http.Server
server = &http.Server{
Addr: fmt.Sprintf(":%v", conf.Port),
Handler: data,
}
for err == nil {
err = server.ListenAndServe()
}
}
// Config
type Config struct {
Port int
DataFile string
Path string `json:"-"`
}
func NewConfig() *Config {
return &Config{
Port: 8080,
DataFile: "./data.json",
}
}
func (conf *Config) Save() {
var err error
var confFile *os.File
var absPath string
absPath , err = filepath.Abs(conf.Path)
if err != nil {
log.Fatal(err)
}
confFile, err = os.OpenFile(absPath, os.O_CREATE | os.O_RDWR, 0666)
defer confFile.Close()
enc := json.NewEncoder(confFile)
err = enc.Encode(conf)
if err != nil {
log.Fatal(err)
}
}
func OpenOrCreateConfig() (conf *Config) {
conf = NewConfig()
var err error
var confPath string
if len(os.Args) > 1 {
confPath = os.Args[1]
} else {
confPath = "./conf.json"
}
confPath, err = filepath.Abs(confPath)
if err != nil {
log.Fatal(err);
}
var confFile *os.File
confFile, err = os.Open(confPath)
defer confFile.Close()
if os.IsNotExist(err) {
conf.Path = confPath;
conf.Save()
return
}
if err != nil {
log.Fatal(err)
}
dec := json.NewDecoder(confFile)
dec.Decode(conf)
return
}
// Data
type Data struct {
LastReset time.Time
PageLayout string
Path string `json:"-"`
}
func NewData() *Data {
return &Data{
LastReset: time.Now(),
PageLayout: "<html><head></head><body><h1>%v</h1></body></html>",
}
}
func (data *Data) Save() {
var err error
var dataFile *os.File
var absPath string
absPath , err = filepath.Abs(data.Path)
if err != nil {
log.Fatal(err)
}
dataFile, err = os.OpenFile(absPath, os.O_CREATE | os.O_RDWR, 0666)
defer dataFile.Close()
enc := json.NewEncoder(dataFile)
err = enc.Encode(data)
if err != nil {
log.Fatal(err)
}
}
func OpenOrCreateData(dataPath string) (data *Data) {
data = NewData()
var err error
dataPath, err = filepath.Abs(dataPath)
if err != nil {
log.Fatal(err);
}
var dataFile *os.File
dataFile, err = os.Open(dataPath)
defer dataFile.Close()
data.Path = dataPath
if os.IsNotExist(err) {
data.Save()
return
}
if err != nil {
log.Fatal(err)
}
dec := json.NewDecoder(dataFile)
dec.Decode(data)
return
}
func (data *Data) ResetTimestamp() {
data.LastReset = time.Now()
data.Save()
}
func (data *Data) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
fmt.Fprintf(rw, data.PageLayout, time.Since(data.LastReset).String())
} else {
data.ResetTimestamp()
http.Redirect(rw, req, "/", 303)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment