This repository was archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (103 loc) · 3.32 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"net/http"
"strconv"
"github.com/kataras/muxie"
"github.com/lucat1/o2/pkg/auth"
"github.com/lucat1/o2/pkg/images"
"github.com/lucat1/o2/pkg/middleware"
"github.com/lucat1/o2/pkg/models"
"github.com/lucat1/o2/pkg/store"
"github.com/lucat1/o2/routes"
"github.com/lucat1/o2/routes/git"
"github.com/lucat1/o2/routes/repository"
"github.com/lucat1/o2/routes/settings"
"github.com/lucat1/o2/routes/shared"
"github.com/lucat1/quercia"
"github.com/markbates/pkger"
"github.com/lucat1/o2/pkg/log"
)
var (
port *int
host *string
)
var dir pkger.Dir
func init() {
port = flag.Int("port", 3000, "Sets the web server port")
host = flag.String("host", "0.0.0.0", "Sets the web server host")
flag.Parse()
// initialize the logger, store and database
store.InitConfig()
store.InitLogs()
store.InitDatabase()
store.InitHooks()
models.Init()
images.Init()
// noop to include the default config file
pkger.Include("/data/o2.ini")
// instantiate the http directory for the static files
dir = pkger.Dir("/__quercia")
quercia.SetDir(dir)
}
func main() {
mux := muxie.NewMux()
//mux.Use(auth.Middleware)
mux.Handle("/__quercia/*", http.StripPrefix("/__quercia/", http.FileServer(dir)))
// log requests during debug
mux.Use(middleware.Scheme)
mux.Use(middleware.Debug)
mux.Use(auth.With)
mux.HandleFunc("/", routes.Feed)
mux.HandleFunc("/feed/:page", routes.Feed)
mux.HandleFunc("/favicon.ico", shared.NotFound)
mux.HandleFunc("/picture/:hash", routes.Picture)
mux.HandleFunc("/register", routes.Register)
mux.HandleFunc("/login", routes.Login)
mux.HandleFunc("/logout", auth.Must(routes.Logout))
mux.HandleFunc("/new", auth.Must(routes.New))
mux.HandleFunc("/settings", auth.Must(settings.Settings))
mux.HandleFunc("/settings/privacy", auth.Must(settings.Privacy))
mux.HandleFunc("/:name", routes.Profile)
repo := mux.Of("/:name/:repo")
repo.Use(middleware.WithRepo(shared.NotFound))
// generate the resource value based on the :name/:repo
repo.Use(middleware.WithResource(middleware.RepositoryResource))
repo.Use(middleware.MustPex([]string{"repo:pull"}, shared.NotFound))
repo.HandleFunc("/", repository.Repository)
repo.Handle(
"/settings",
middleware.MustPex(
[]string{"repo:push"},
shared.NotFound,
)(http.HandlerFunc(repository.Settings)),
)
repo.HandleFunc("/tree/:branch", repository.Tree)
repo.HandleFunc("/tree/:branch/*path", repository.Tree)
repo.HandleFunc("/blob/:branch/*path", repository.Blob)
repo.HandleFunc("/commits/:branch", repository.Commits)
repo.HandleFunc("/commits/:branch/:page", repository.Commits)
repo.HandleFunc("/commit/:sha", repository.Commit)
repo.HandleFunc("/issues", repository.Issues)
repo.HandleFunc("/issues/new", auth.Must(repository.NewIssue))
repo.HandleFunc("/issue/:id", repository.Issue)
// git smart http protocol
repo.HandleFunc("/info/refs", git.InfoRefs)
repo.HandleFunc("/git-upload-pack", git.RPC("upload-pack"))
repo.Handle(
"/git-receive-pack",
middleware.MustPex(
[]string{"repo:push"},
shared.NotFound,
)(http.HandlerFunc(git.RPC("receive-pack"))),
)
// 404 handler
mux.HandleFunc("/*path", shared.NotFound)
log.Info().
Str("host", *host).
Int("port", *port).
Msg("Starting web server")
log.Fatal().
Err(http.ListenAndServe(*host+":"+strconv.Itoa(*port), mux)).
Msg("Could not listen on given port")
}