-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
70 lines (59 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"database/sql"
"net"
"net/http"
"os"
"path/filepath"
"github.com/julienschmidt/httprouter"
_ "github.com/mattn/go-sqlite3"
"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/zserge/webview"
)
var home, _ = homedir.Dir()
var annotationsDirectory = filepath.Join(home, ".annotations")
var annotationsDB = filepath.Join(annotationsDirectory, "annotations.db")
var db *sql.DB
func init() {
if _, err := os.Stat(annotationsDirectory); os.IsNotExist(err) {
os.Mkdir(annotationsDirectory, 0755)
}
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
db = InitDB(annotationsDB)
router := httprouter.New()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal().Err(err).Msg("server failed to start")
}
defer ln.Close()
go func() {
router.GET("/annotation/get/:id", Get)
router.POST("/annotation/update/:id", Update)
router.POST("/annotation/delete/:id", Delete)
router.POST("/annotation/create", Create)
router.GET("/annotation/list", List)
fileServer := http.FileServer(assetFS())
router.GET("/static/*filepath", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
r.URL.Path = ps.ByName("filepath")
fileServer.ServeHTTP(w, r)
})
log.Info().Str("address", "http://"+ln.Addr().String()).Msg("server started")
if err := http.Serve(ln, router); err != nil {
log.Fatal().Err(err).Msg("server failed to start")
}
}()
w := webview.New(webview.Settings{
Width: 1000,
Height: 700,
Title: "IIIF Annotation Studio",
Resizable: true,
URL: "http://" + ln.Addr().String() + "/static/index.html",
Debug: true,
})
defer w.Exit()
w.Run()
}