Split into separate files, add authentication
This commit is contained in:
parent
a2e3f7c845
commit
f7b775cb38
|
@ -0,0 +1,48 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
|
func initializeDatabase() {
|
||||||
|
dbo, err := sql.Open("sqlite3", "content")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("couldn’t open database. exiting...")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
db = dbo
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeToDB(post BlogPost) {
|
||||||
|
stmt, _ := db.Prepare("INSERT INTO blogposts(author, title, content, time) VALUES (?, ?, ?, ?)")
|
||||||
|
stmt.Exec(post.Author, post.Title, post.Content, time.Now().Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
func readBlogpostsFromDB() []BlogPost {
|
||||||
|
res, err := db.Query("SELECT content, author, title, time from blogposts")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error reading blogposts")
|
||||||
|
return make([]BlogPost, 0)
|
||||||
|
} else {
|
||||||
|
return resultToBlogposts(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resultToBlogposts(res *sql.Rows) []BlogPost {
|
||||||
|
defer res.Close()
|
||||||
|
entries := make([]BlogPost, 0)
|
||||||
|
for res.Next() {
|
||||||
|
var content, author, title string
|
||||||
|
var timeInt int64
|
||||||
|
res.Scan(&content, &author, &title, &timeInt)
|
||||||
|
post := BlogPost{Content:content, Author:author, Title:title, Time:time.Unix(timeInt, 0)}
|
||||||
|
entries = append(entries, post)
|
||||||
|
}
|
||||||
|
return entries
|
||||||
|
}
|
55
main.go
55
main.go
|
@ -1,69 +1,24 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
//"fmt"
|
|
||||||
"encoding/json"
|
|
||||||
"database/sql"
|
|
||||||
"time"
|
"time"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
//"fmt"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var messages []*string
|
var messages []*string
|
||||||
var db *sql.DB
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dbo, err := sql.Open("sqlite3", "content")
|
initializeDatabase()
|
||||||
if err != nil {
|
startServer()
|
||||||
log.Fatal("couldn’t open database. exiting...")
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
db = dbo
|
|
||||||
}
|
|
||||||
//listener, err = net.Listen("socket", "/tmp/
|
//listener, err = net.Listen("socket", "/tmp/
|
||||||
http.HandleFunc("/", get)
|
|
||||||
http.HandleFunc("/add", post)
|
|
||||||
log.Fatal(http.ListenAndServe(":12345", nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeToDB(post BlogPost) {
|
|
||||||
stmt, _ := db.Prepare("INSERT INTO blogposts(author, title, content, date) VALUES (?, ?, ?, ?)")
|
|
||||||
stmt.Exec(post.Author, post.Title, post.Content, time.Now().Format("2006-01-02"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BlogPost struct {
|
type BlogPost struct {
|
||||||
|
Id int64 `"id"`
|
||||||
Content string `"content"`
|
Content string `"content"`
|
||||||
Title string `"title"`
|
Title string `"title"`
|
||||||
Author string `"author"`
|
Author string `"author"`
|
||||||
Secret string `"secret"`
|
Secret string `"secret"`
|
||||||
|
Time time.Time `"time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func get(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("response:\n"))
|
|
||||||
//stmt, _ := db.Prepare("SELECT content from blogposts")
|
|
||||||
//res, _ := stmt.Exec()
|
|
||||||
res, _ := db.Query("SELECT content from blogposts")
|
|
||||||
for _, e := range(resultToStrings(res)) {
|
|
||||||
w.Write([]byte(e + "\n"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func post(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var post BlogPost
|
|
||||||
json.NewDecoder(r.Body).Decode(&post)
|
|
||||||
messages = append(messages, &post.Content)
|
|
||||||
writeToDB(post)
|
|
||||||
}
|
|
||||||
|
|
||||||
func resultToStrings(res *sql.Rows) []string {
|
|
||||||
defer res.Close()
|
|
||||||
entries := make([]string, 0)
|
|
||||||
for res.Next() {
|
|
||||||
var e string
|
|
||||||
res.Scan(&e)
|
|
||||||
entries = append(entries, e)
|
|
||||||
}
|
|
||||||
return entries
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"encoding/hex"
|
||||||
|
"crypto/sha512"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func startServer() {
|
||||||
|
http.HandleFunc("/", get)
|
||||||
|
http.HandleFunc("/add", post)
|
||||||
|
log.Fatal(http.ListenAndServe(":12345", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func get(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("response:\n"))
|
||||||
|
for _, post := range readBlogpostsFromDB() {
|
||||||
|
w.Write([]byte(post.Content))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func post(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var post BlogPost
|
||||||
|
json.NewDecoder(r.Body).Decode(&post)
|
||||||
|
if verifyPassword(post.Secret) {
|
||||||
|
messages = append(messages, &post.Content)
|
||||||
|
writeToDB(post)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hash(pass string) string {
|
||||||
|
h := sha512.New()
|
||||||
|
io.WriteString(h, pass)
|
||||||
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifyPassword(pass string) bool {
|
||||||
|
return hash(pass) == "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429080fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1"
|
||||||
|
}
|
2
test.sh
2
test.sh
|
@ -5,5 +5,5 @@ else
|
||||||
msg="$1"
|
msg="$1"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
curl localhost:12345/add -d "{\"content\": \"$msg\", \"Title\": \"title\", \"Secret\": \"asdawdwd\", \"author\": \"me\"}" -H "Content-Type: application/json" -v
|
curl localhost:12345/add -d "{\"content\": \"$msg\", \"Title\": \"title\", \"Secret\": \"asdf\", \"author\": \"me\"}" -H "Content-Type: application/json" -v
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user