Add config
This commit is contained in:
parent
13a199afee
commit
73063316aa
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
notes.sqlite
|
||||
NoteServer
|
||||
config.json
|
||||
|
81
main.go
81
main.go
@ -6,15 +6,23 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
const configPath = "config.json"
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
type Note struct {
|
||||
type config struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
type note struct {
|
||||
Id int64 `json:"id"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
@ -53,14 +61,14 @@ func getNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
||||
|
||||
defer res.Close()
|
||||
|
||||
var notes []Note = []Note{}
|
||||
var notes []note = []note{}
|
||||
|
||||
for res.Next() {
|
||||
if httpError(res.Err(), http.StatusInternalServerError, "Failed to load notes", w) {
|
||||
return
|
||||
}
|
||||
|
||||
var note Note
|
||||
var note note
|
||||
res.Scan(¬e.Id, ¬e.Content)
|
||||
notes = append(notes, note)
|
||||
}
|
||||
@ -80,7 +88,7 @@ func postNotes(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
||||
return
|
||||
}
|
||||
|
||||
var note Note
|
||||
var note note
|
||||
err = json.Unmarshal(data, ¬e)
|
||||
if httpError(err, http.StatusBadRequest, "Invalid request", w) {
|
||||
return
|
||||
@ -129,7 +137,7 @@ func getNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
|
||||
|
||||
res := stmt.QueryRow(id)
|
||||
|
||||
var note Note = Note{Id: id}
|
||||
var note note = note{Id: id}
|
||||
|
||||
err = res.Scan(¬e.Content)
|
||||
if err == sql.ErrNoRows {
|
||||
@ -161,7 +169,7 @@ func putNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
|
||||
return
|
||||
}
|
||||
|
||||
var note Note
|
||||
var note note
|
||||
err = json.Unmarshal(data, ¬e)
|
||||
if httpError(err, http.StatusBadRequest, "Invalid request", w) {
|
||||
return
|
||||
@ -204,12 +212,67 @@ func putNote(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
|
||||
w.Write(json)
|
||||
}
|
||||
|
||||
func fileExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
func loadConfig(path string) (config, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return config{}, err
|
||||
}
|
||||
|
||||
var cfg config = config{}
|
||||
err = json.Unmarshal(raw, &cfg)
|
||||
if err != nil {
|
||||
return config{}, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println("Loading config")
|
||||
|
||||
exists, err := fileExists(configPath)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
// Config etc
|
||||
json, err := json.MarshalIndent(config{
|
||||
Host: "localhost",
|
||||
Port: 8080,
|
||||
}, "", "\t")
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
os.WriteFile(configPath, json, 0o664)
|
||||
log.Println("Created config.json, edit accordingly")
|
||||
return
|
||||
}
|
||||
|
||||
config, err := loadConfig(configPath)
|
||||
if err != nil {
|
||||
log.Fatalln(config)
|
||||
}
|
||||
|
||||
log.Println("Starting NoteServer")
|
||||
|
||||
log.Println("Loading database")
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("sqlite3", "./notes.sqlite")
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to load db:", err)
|
||||
@ -226,8 +289,8 @@ func main() {
|
||||
router := httprouter.New()
|
||||
router.GET("/api/notes", getNotes)
|
||||
router.POST("/api/notes", postNotes)
|
||||
router.GET("/api/note/:id", getNote)
|
||||
router.PUT("/api/note/:id", putNote)
|
||||
router.GET("/api/notes/:id", getNote)
|
||||
router.PUT("/api/notes/:id", putNote)
|
||||
|
||||
http.ListenAndServe(":8080", router) // TODO: configure host, port, TLS
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user