mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
d9fb844dc8
Former-commit-id: cb10c6fcbd1aa6bf92d06cd2d8665887f5c2018b [formerly b5c933531b7b50a9cc39d462c975f280fd500518] [formerly 3fedbc58ecf2e91794c22e48d667e53d0083a164 [formerly bc44aafffac795fe1186dc61dd8eca229dbd0581 [formerly2be7c500a7
]]] Former-commit-id: 9640ab56ba31f79aa603326ddf56aeee2bcbbfb0 [formerly 8a40c159a0eb7de236f7f4600e9678bef91d907c] Former-commit-id: f27fb19a55aa7e7c88886c47136826d917c1aeb0 Former-commit-id:b94c81da30
204 lines
3.6 KiB
Go
Executable File
204 lines
3.6 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/boltdb/bolt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
var db *bolt.DB
|
|
var open bool
|
|
var ExternalIP string
|
|
|
|
func Open() error {
|
|
var err error
|
|
_, filename, _, _ := runtime.Caller(0) // get full path of this file
|
|
dbfile := path.Join(path.Dir(filename), "data.db")
|
|
config := &bolt.Options{Timeout: 30 * time.Second}
|
|
db, err = bolt.Open(dbfile, 0600, config)
|
|
if err != nil {
|
|
fmt.Println("Opening BoltDB timed out")
|
|
log.Fatal(err)
|
|
}
|
|
open = true
|
|
return nil
|
|
}
|
|
|
|
func Close() {
|
|
open = false
|
|
db.Close()
|
|
}
|
|
|
|
type CowyoData struct {
|
|
Title string
|
|
Text string
|
|
}
|
|
|
|
// Database functions
|
|
|
|
func (p *CowyoData) load() error {
|
|
if !open {
|
|
return fmt.Errorf("db must be opened before saving!")
|
|
}
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
var err error
|
|
b := tx.Bucket([]byte("datas"))
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
k := []byte(p.Title)
|
|
val := b.Get(k)
|
|
if val == nil {
|
|
return nil
|
|
}
|
|
err = p.decode(val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("Could not get CowyoData: %s", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *CowyoData) save() error {
|
|
if !open {
|
|
return fmt.Errorf("db must be opened before saving!")
|
|
}
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
|
bucket, err := tx.CreateBucketIfNotExists([]byte("datas"))
|
|
if err != nil {
|
|
return fmt.Errorf("create bucket: %s", err)
|
|
}
|
|
enc, err := p.encode()
|
|
if err != nil {
|
|
return fmt.Errorf("could not encode CowyoData: %s", err)
|
|
}
|
|
err = bucket.Put([]byte(p.Title), enc)
|
|
return err
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (p *CowyoData) encode() ([]byte, error) {
|
|
enc, err := json.Marshal(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return enc, nil
|
|
}
|
|
|
|
func (p *CowyoData) decode(data []byte) error {
|
|
err := json.Unmarshal(data, &p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) == 1 {
|
|
log.Fatal("You need to specify the external IP address")
|
|
}
|
|
ExternalIP = os.Args[1]
|
|
Open()
|
|
defer Close()
|
|
r := gin.Default()
|
|
r.LoadHTMLGlob("templates/*")
|
|
|
|
r.GET("/", func(c *gin.Context) {
|
|
title := randomAlliterateCombo()
|
|
c.Redirect(302, "/"+title)
|
|
|
|
})
|
|
r.GET("/:title", func(c *gin.Context) {
|
|
title := c.Param("title")
|
|
if title == "ws" {
|
|
wshandler(c.Writer, c.Request)
|
|
} else {
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
"Title": title,
|
|
"ExternalIP": ExternalIP,
|
|
})
|
|
|
|
}
|
|
})
|
|
r.GET("/:title/*option", func(c *gin.Context) {
|
|
title := c.Param("title")
|
|
option := c.Param("option")
|
|
fmt.Println(title, "["+option+"]")
|
|
if option == "/view" {
|
|
} else {
|
|
c.Redirect(302, "/"+title)
|
|
}
|
|
})
|
|
|
|
r.Run(":12312")
|
|
}
|
|
|
|
var wsupgrader = websocket.Upgrader{
|
|
ReadBufferSize: 1024,
|
|
WriteBufferSize: 1024,
|
|
}
|
|
|
|
func wshandler(w http.ResponseWriter, r *http.Request) {
|
|
conn, err := wsupgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
fmt.Println("Failed to set websocket upgrade: %+v", err)
|
|
return
|
|
}
|
|
|
|
for {
|
|
t, msg, err := conn.ReadMessage()
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
type Message struct {
|
|
TextData string
|
|
Title string
|
|
UpdateServer bool
|
|
UpdateClient bool
|
|
}
|
|
|
|
var m Message
|
|
err = json.Unmarshal(msg, &m)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if m.UpdateServer {
|
|
p := CowyoData{m.Title, m.TextData}
|
|
err := p.save()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
if m.UpdateClient {
|
|
p := CowyoData{m.Title, ""}
|
|
err := p.load()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
m.TextData = p.Text
|
|
}
|
|
newMsg, err := json.Marshal(m)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
conn.WriteMessage(t, newMsg)
|
|
}
|
|
}
|